12-SpringBoot之数据库(三)——Mybatis

1. 添加pom依赖

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.3.2</version>
</dependency>

2. Mybatis配置

在application.yml文件中添加Mybatis配置,如下:

 #mybatis配置
mybatis:
  mapper-locations: classpath:mapper/*.xml
  type-aliases-package: com.springboot.web.model

项目启动类添加@MapperScan

@MapperScan("com.springboot.web.dao")

3. 案例开发

3.1 创建实体类

import com.alibaba.fastjson.annotation.JSONField;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Date;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
    private String id;
    private String name;
    private Integer age;

    @JSONField(format="yyyy-MM-dd")
    private Date birthday;

    public User(String id, String name, Integer age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }
}

3.2 DAO

DAO接口:

import com.springboot.web.model.User;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface UserMybatisDao {
    Integer insert(User user);
    List<User> getAllUserList();
}

mapper文件:在resources\mapper文件下创建UserMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.springboot.web.dao.UserMybatisDao">
    <sql id="BASE_TABLE">
      user
    </sql>

    <sql id="BASE_COLUMN">
      id,name,age,birthday
    </sql>

    <insert id="insert" parameterType="com.springboot.web.model.User">
        INSERT INTO
        <include refid="BASE_TABLE"/>
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <include refid="BASE_COLUMN"/>
        </trim>
        <trim prefix="VALUES(" suffix=")" suffixOverrides=",">
            #{id, jdbcType=VARCHAR},#{name, jdbcType=VARCHAR},
            #{age, jdbcType=INTEGER}, #{birthday, jdbcType=DATE},

        </trim>
    </insert>

    <select id="getAllUserList" resultType="com.springboot.web.model.User">
        SELECT
        <include refid="BASE_COLUMN"/>
        FROM
        <include refid="BASE_TABLE"/>
    </select>
    
</mapper>

3.3 Service

service接口:

import com.springboot.web.model.User;

import java.util.List;

public interface UserMybatisService {
    Integer insert(User user);
    List<User> getAllUserList();
}

service实现:

import com.springboot.web.dao.UserMybatisDao;
import com.springboot.web.model.User;
import com.springboot.web.service.UserMybatisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserMybatisServiceImpl implements UserMybatisService {


    @Autowired
    private UserMybatisDao userMybatisDao;

    @Override
    public Integer insert(User user) {
        return userMybatisDao.insert(user);
    }

    @Override
    public List<User> getAllUserList() {
        return userMybatisDao.getAllUserList();
    }
}

3.4 Controller

import com.springboot.web.model.User;
import com.springboot.web.service.UserMybatisService;
import com.springboot.web.utils.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("/mybatis")
public class UserMybatisController {

    @Autowired
    private UserMybatisService userMybatisService;

    @GetMapping("/getAllUserList")
    public Object getAllUserList() {
        return userMybatisService.getAllUserList();
    }

    @PostMapping("/createUser")
    public void createUser(@RequestParam("name") String name, @RequestParam("age") Integer age) {
        User user = new User(UUIDUtil.getUUID(), name, age, new Date());
        userMybatisService.insert(user);
    }
}

3.5 测试

使用postman进行测试

测试createUser:12-SpringBoot之数据库(三)——Mybatis
12-SpringBoot之数据库(三)——Mybatis

测试getAllUserList:12-SpringBoot之数据库(三)——Mybatis

4. 源码下载

源码下载地址:https://download.****.net/download/huangjun0210/10791197