1. 程式人生 > >springboot系列十三 mybatis 註解和xml2種方式

springboot系列十三 mybatis 註解和xml2種方式

依賴

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

使用方式 註解和xml

註解方式

配置

#mysql
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8
    username: root
    password: root
#mybatis
mybatis:
  type-aliases-package: com.yimingkeji.mybatis.entity

mapper介面

public interface UserMapper {

    @Select("select * from user")
    @Results({
            @Result(property = "phoneNumber", column = "phone_number"),
            @Result(property = "createTime", column = "create_time", javaType = Date.class)
    })
    List<User> findAll();

    @Select("select * from user where id = #{id}")
    User findOne(int id);

    @Insert("insert into user(name, age, phone_number, available, create_time) values (#{name}, #{age}, #{phoneNumber}, #{available}, #{createTime})")
    int add(User user);

    @Update("update user set name=#{name} where id = #{id}")
    int update(User user);

    @Delete("delete from user where id = #{id}")
    int delete(int id);
}

mapper掃描配置

在啟動類中新增@MapperScan註解

@SpringBootApplication
@MapperScan(basePackages = {"com.yimingkeji.mybatis.mapper"})//或者每個mapper介面上加 @Mapper 註解
public class MybatisApp {

    public static void main(String[] args) {
        SpringApplication.run(MybatisApp.class, args);
    }
}

測試介面

@RestController
public class UserController1 {

    @Autowired private UserMapper userMapper;

    @GetMapping("user")
    public List<User> list(){
        return userMapper.findAll();
    }

    @GetMapping("user/{id}")
    public User one(@PathVariable int id){
        return userMapper.findOne(id);
    }

    @PostMapping("user")
    public int add(@RequestBody User user){
        return userMapper.add(user);
    }

    @PutMapping("user")
    public int update(@RequestBody User user){
        return userMapper.update(user);
    }

    @DeleteMapping("user/{id}")
    public int del(@PathVariable int id){
        return userMapper.delete(id);
    }
}

xml方式

配置

#mybatis
mybatis:
  type-aliases-package: com.yimingkeji.mybatis.entity
  config-location: classpath:mybatis/config/mybatisConfig.xml
  mapper-locations: classpath:mybatis/mapper/*.xml

啟動類mapper掃描

@SpringBootApplication
@MapperScan(basePackages = {"com.yimingkeji.mybatis.mapper", "com.yimingkeji.mybatis.mapper2"})//或者每個mapper介面上加 @Mapper 註解
public class MybatisApp {

    public static void main(String[] args) {
        SpringApplication.run(MybatisApp.class, args);
    }
}

mapper介面和xml配置

public interface UserMapper2 {
    List<User> findAll();
}
<?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.yimingkeji.mybatis.mapper2.UserMapper2">

    <resultMap id="BaseResultMap" type="com.yimingkeji.mybatis.entity.User">
        <id column="id" property="id" />
        <result column="name" property="name"  />
        <result column="age" property="age" />
        <result column="phone_number" property="phoneNumber" />
        <result column="available" property="available" />
        <result column="create_time" property="createTime" />
    </resultMap>

    <select id="findAll" resultMap="BaseResultMap">
        select * from user
    </select>
</mapper>

測試介面

@Autowired private UserMapper2 userMapper2;

@GetMapping("user2")
public List<User> list(){
    return userMapper2.findAll();
}

原始碼地址

https://gitee.com/yimingkeji/springboot/tree/master/mybatis