打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
最简单的SpringBoot整合MyBatis教程

前面两篇文章和读者聊了Spring Boot中最简单的数据持久化方案JdbcTemplate,JdbcTemplate虽然简单,但是用的并不多,因为它没有MyBatis方便,在Spring+SpringMVC中整合MyBatis步骤还是有点复杂的,要配置多个Bean,Spring Boot中对此做了进一步的简化,使MyBatis基本上可以做到开箱即用,本文就来看看在Spring Boot中MyBatis要如何使用。

工程创建

首先创建一个基本的Spring Boot工程,添加Web依赖,MyBatis依赖以及MySQL驱动依赖,如下:

创建成功后,添加Druid依赖,并且锁定MySQL驱动版本,完整的依赖如下:

  1. <dependency>

  2. <groupId>org.springframework.boot</groupId>

  3. <artifactId>spring-boot-starter-web</artifactId>

  4. </dependency>

  5. <dependency>

  6. <groupId>org.mybatis.spring.boot</groupId>

  7. <artifactId>mybatis-spring-boot-starter</artifactId>

  8. <version>2.0.0</version>

  9. </dependency>

  10. <dependency>

  11. <groupId>com.alibaba</groupId>

  12. <artifactId>druid-spring-boot-starter</artifactId>

  13. <version>1.1.10</version>

  14. </dependency>

  15. <dependency>

  16. <groupId>mysql</groupId>

  17. <artifactId>mysql-connector-java</artifactId>

  18. <version>5.1.28</version>

  19. <scope>runtime</scope>

  20. </dependency>

如此,工程就算是创建成功了。读者注意,MyBatis和Druid依赖的命名和其他库的命名不太一样,是属于xxx-spring-boot-stater模式的,这表示该starter是由第三方提供的。

基本用法

MyBatis的使用和JdbcTemplate基本一致,首先也是在application.properties中配置数据库的基本信息:

  1. spring.datasource.url=jdbc:mysql:///test01?useUnicode=true&characterEncoding=utf-8

  2. spring.datasource.username=root

  3. spring.datasource.password=root

  4. spring.datasource.type=com.alibaba.druid.pool.DruidDataSource

配置完成后,MyBatis就可以创建Mapper来使用了,例如我这里直接创建一个UserMapper2,如下:

  1. public interface UserMapper2 {

  2. @Select('select * from user')

  3. List<User> getAllUsers();

  4. @Results({

  5. @Result(property = 'id', column = 'id'),

  6. @Result(property = 'username', column = 'u'),

  7. @Result(property = 'address', column = 'a')

  8. })

  9. @Select('select username as u,address as a,id as id from user where id=#{id}')

  10. User getUserById(Long id);

  11. @Select('select * from user where username like concat('%',#{name},'%')')

  12. List<User> getUsersByName(String name);

  13. @Insert({'insert into user(username,address) values(#{username},#{address})'})

  14. @SelectKey(statement = 'select last_insert_id()', keyProperty = 'id', before = false, resultType = Integer.class)

  15. Integer addUser(User user);

  16. @Update('update user set username=#{username},address=#{address} where id=#{id}')

  17. Integer updateUserById(User user);

  18. @Delete('delete from user where id=#{id}')

  19. Integer deleteUserById(Integer id);

  20. }

这里是通过全注解的方式来写SQL,不写XML文件,@Select、@Insert、@Update以及@Delete四个注解分别对应XML中的select、insert、update以及delete标签,@Results注解类似于XML中的ResultMap映射文件(getUserById方法给查询结果的字段取别名主要是向小伙伴们演示下 @Results注解的用法),另外使用@SelectKey注解可以实现主键回填的功能,即当数据插入成功后,插入成功的数据id会赋值到user对象的id属性上。

UserMapper2创建好之后,还要配置mapper扫描,有两种方式,一种是直接在UserMapper2上面添加 @Mapper注解,这种方式有一个弊端就是所有的Mapper都要手动添加,要是落下一个就会报错,还有一个一劳永逸的办法就是直接在启动类上添加Mapper扫描,如下:

  1. @SpringBootApplication

  2. @MapperScan(basePackages = 'org.sang.mybatis.mapper')

  3. public class MybatisApplication {

  4. public static void main(String[] args) {

  5. SpringApplication.run(MybatisApplication.class, args);

  6. }

  7. }

好了,做完这些工作就可以去测试Mapper的使用了。

mapper映射

当然,开发者也可以在XML中写SQL,例如创建一个UserMapper,如下:

  1. public interface UserMapper {

  2. List<User> getAllUser();

  3. Integer addUser(User user);

  4. Integer updateUserById(User user);

  5. Integer deleteUserById(Integer id);

  6. }

然后创建UserMapper.xml文件,如下:

  1. <?xml version='1.0' encoding='UTF-8' ?>

  2. <!DOCTYPE mapper

  3. PUBLIC '-//mybatis.org//DTD Mapper 3.0//EN'

  4. 'http://mybatis.org/dtd/mybatis-3-mapper.dtd'>

  5. <mapper namespace='org.sang.mybatis.mapper.UserMapper'>

  6. <select id='getAllUser' resultType='org.sang.mybatis.model.User'>

  7. select * from t_user;

  8. </select>

  9. <insert id='addUser' parameterType='org.sang.mybatis.model.User'>

  10. insert into user (username,address) values (#{username},#{address});

  11. </insert>

  12. <update id='updateUserById' parameterType='org.sang.mybatis.model.User'>

  13. update user set username=#{username},address=#{address} where id=#{id}

  14. </update>

  15. <delete id='deleteUserById'>

  16. delete from user where id=#{id}

  17. </delete>

  18. </mapper>

将接口中方法对应的SQL直接写在XML文件中。

那么这个UserMapper.xml到底放在哪里呢?有两个位置可以放,第一个是直接放在UserMapper所在的包下面:

放在这里的UserMapper.xml会被自动扫描到,但是有另外一个Maven带来的问题,就是java目录下的xml资源在项目打包时会被忽略掉,所以,如果UserMapper.xml放在包下,需要在pom.xml文件中再添加如下配置,避免打包时java目录下的XML文件被自动忽略掉:

  1. <build>

  2. <resources>

  3. <resource>

  4. <directory>src/main/java</directory>

  5. <includes>

  6. <include>**/*.xml</include>

  7. </includes>

  8. </resource>

  9. <resource>

  10. <directory>src/main/resources</directory>

  11. </resource>

  12. </resources>

  13. </build>

当然,UserMapper.xml也可以直接放在resources目录下,这样就不用担心打包时被忽略了,但是放在resources目录下,又不能自动被扫描到,需要添加额外配置。例如我在resources目录下创建mapper目录用来放mapper文件,如下:

此时在application.properties中告诉mybatis去哪里扫描mapper:

  1. mybatis.mapper-locations=classpath:mapper/*.xml

如此配置之后,mapper就可以正常使用了。注意第二种方式不需要在pom.xml文件中配置文件过滤。

原理分析

在SSM整合中,开发者需要自己提供两个Bean,一个SqlSessionFactoryBean,还有一个是MapperScannerConfigurer,在Spring Boot中,这两个东西虽然不用开发者自己提供了,但是并不意味着这两个Bean不需要了,在 org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration类中,我们可以看到Spring Boot提供了这两个Bean,部分源码如下:

  1. @org.springframework.context.annotation.Configuration

  2. @ConditionalOnClass({ SqlSessionFactory.class, SqlSessionFactoryBean.class })

  3. @ConditionalOnSingleCandidate(DataSource.class)

  4. @EnableConfigurationProperties(MybatisProperties.class)

  5. @AutoConfigureAfter(DataSourceAutoConfiguration.class)

  6. public class MybatisAutoConfiguration implements InitializingBean {

  7. @Bean

  8. @ConditionalOnMissingBean

  9. public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {

  10. SqlSessionFactoryBean factory = new SqlSessionFactoryBean();

  11. factory.setDataSource(dataSource);

  12. return factory.getObject();

  13. }

  14. @Bean

  15. @ConditionalOnMissingBean

  16. public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {

  17. ExecutorType executorType = this.properties.getExecutorType();

  18. if (executorType != null) {

  19. return new SqlSessionTemplate(sqlSessionFactory, executorType);

  20. } else {

  21. return new SqlSessionTemplate(sqlSessionFactory);

  22. }

  23. }

  24. @org.springframework.context.annotation.Configuration

  25. @Import({ AutoConfiguredMapperScannerRegistrar.class })

  26. @ConditionalOnMissingBean(MapperFactoryBean.class)

  27. public static class MapperScannerRegistrarNotFoundConfiguration implements InitializingBean {

  28. @Override

  29. public void afterPropertiesSet() {

  30. logger.debug('No {} found.', MapperFactoryBean.class.getName());

  31. }

  32. }

  33. }

从类上的注解可以看出,当当前类路径下存在SqlSessionFactory、 SqlSessionFactoryBean以及DataSource时,这里的配置才会生效,SqlSessionFactory和SqlTemplate都被提供了。为什么要看这段代码呢?下篇文章,松哥和大伙分享Spring Boot中MyBatis多数据源的配置时,这里将是一个重要的参考。

好了,本文就先说到这里,关于在Spring Boot中整合MyBatis,这里还有一个小小的视频教程,加入我的星球即可免费观看:

关于我的星球【Java达摩院】,大伙可以参考这篇文章推荐一个技术圈子,Java技能提升就靠它了.

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
MyBatis学习
spring 3 和mybatis 3集成,并用junit4进行测试
Spring整合MyBatis
spring与mybatis三种整合方法
Mybatis中配置Mapper的方法
(三)MyBatis从入门到入土——使用详解
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服