SpringBoot-Mybatis整合

Mybatis两种整合方法

1. 配置模式

  1. 导入依赖

    1
    2
    3
    4
    5
    6
    7
    <!--myBatis,注意是非spring-boot官方的依赖-->
    <!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
    <dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.1.1</version>
    </dependency>
  2. 配置数据库连接信息

    1
    2
    3
    4
    5
    6
    7
    8
    9
    spring.datasource.username=root
    spring.datasource.password=zzz
    spring.datasource.url=jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
    spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

    # 整合mybatis
    mybatis.type-aliases-package=zone.yiqing.pojo
    mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
    mybatis.configuration.map-underscore-to-camel-case=true
  3. 测试连接是否成功

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    @SpringBootTest
    class Springboot05MybatisApplicationTests {

    @Autowired
    DataSource dataSource;
    @Test
    void contextLoads() throws Exception {
    System.out.println(dataSource.getClass());
    System.out.println(dataSource.getConnection());
    }
    }
  4. 创建实体类

    1
    2
    3
    4
    5
    6
    7
    8
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public class User {
    private int id;
    private String name;
    private String pwd;
    }
  5. 创建mapper目录以及mapper接口文件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    @Mapper // 这个注解标识这个类是mybatis的mapper类
    @Repository // 将类的实现类交给spring管理
    public interface UserMapper {
    List<User> queryUserList();

    User queryUserById(int id);

    int addUser(User user);

    int updateUser(User user);

    int deleteUser(int id);
    }
  6. 编写对应的Mapper.xml

    1. 注意: 写在resources下面 (resources/mybatis/mapper/UserMapper.xml)

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      <?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="zone.yiqing.mapper.UserMapper">

      <select id="queryUserList" resultType="User">
      select * from user
      </select>

      <select id="queryUserById" resultType="User">
      select * from user where id = #{id}
      </select>

      <insert id="addUser" parameterType="User">
      insert into user(id,name,pwd) values(#{id},#{name},#{pwd})
      </insert>

      <update id="updateUser" parameterType="User">
      update user set name=#{name},pwd=#{pwd} where id = #{id}
      </update>

      <delete id="deleteUser" parameterType="int">
      delete from user where id = #{id}
      </delete>

      </mapper>
  7. 如果有资源过滤问题注意配置maven资源过滤问题

  8. 编写controller

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    @RestController
    public class UserController {

    @Autowired
    private UserMapper userMapper;

    @GetMapping("/queryUserList")
    public List<User> queryUserList(){
    List<User> userList = userMapper.queryUserList();
    return userList;
    }
    }

2. 注解模式

直接在mapper接口上添加注解

1
2
3
4
5
6
7
@Mapper
public interface CityMapper {

@Select("select * from city where id=#{id}")
public City getById(Long id);

}

3. 混合模式

1
2
3
4
5
6
7
8
9
@Mapper
public interface CityMapper {

@Select("select * from city where id=#{id}")
public City getById(Long id);

public void insert(City city);

}

最佳方法

  1. 引入mybatis-starter

  2. 配置application.yaml中,指定mapper-location位置即可

  3. 编写Mapper接口并标注@Mapper注解

  4. 简单方法直接注解方式

  5. 复杂方法编写mapper.xml进行绑定映射

  6. @MapperScan(“com.atguigu.admin.mapper”) 简化,其他的接口就可以不用标注@Mapper注解


Mybatis-Plus

  1. pom引入starter

    1
    2
    3
    4
    5
    <dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.4.2</version>
    </dependency>
  2. 自动配置原理

    1. MybatisPlusAutoConfiguration. 其中MybatisPlusProperties配置项绑定了,所以mybatis-plus:xxx就是对mybatis-plus的定制
    2. SqlSessionFactory自动配置.底层是我们自己配置 的的数据源
    3. mapperLocations 自动配置. 默认值: classpath*:/mapper/*/.xml,即任意包的类路径下的所有mapper文件夹下的所有xml都是sql映射文件
      1. 约定大于配置
      2. 建议所有sql映射文件,都放在mapper下
    4. 容器中也自动配置好了SqlSessionTemplate
    5. @Mapper标注的接口也会被自动扫描
      1. 直接在主启动类上面标注@MapperScan("zone.yiqing.mapper")就可以批量扫描,就不用标注@Mapper
  3. 优点

    1. 只需要我们的Mapper继承BaseMapper.简单CURD都有了
  4. 注意

    1. 实体类所有的属性都应该在数据库中
    2. 如果不在,使用@TableField(exist=flase)标注属性