正文 【Spring Boot】4. 整合MyBatis(TK.Mybatis) 拾年之璐 V管理员 /2020年 /645 阅读 0904 [TOC] ### 0、配置前: 直接创建一个Spring Boot项目即可。 详情可参考上一篇文章。 ### 1、整合Druid #### 1.1 关于Druid Druid 是阿里巴巴开源平台上的一个项目,整个项目由数据库连接池、插件框架和 SQL 解析器组成。该项目主要是为了扩展 JDBC 的一些限制,可以让程序员实现一些特殊的需求,比如向密钥服务请求凭证、统计 SQL 信息、SQL 性能收集、SQL 注入检查、SQL 翻译等,程序员可以通过定制来实现自己需要的功能。 Druid 是目前最好的数据库连接池,在功能、性能、扩展性方面,都超过其他数据库连接池,包括 DBCP、C3P0、BoneCP、Proxool、JBoss DataSource。Druid 已经在阿里巴巴部署了超过 600 个应用,经过多年生产环境大规模部署的严苛考验。Druid 是阿里巴巴开发的号称为监控而生的数据库连接池! #### 1.2 引入依赖 首先在**pom.xml**文件中引入**druid-spring-boot-starter**依赖: ```html com.alibaba druid-spring-boot-starter 1.1.10 ``` 然后引入数据库连接依赖: ```html mysql mysql-connector-java runtime ``` #### 1.3 配置application 将**application.properties**修改为**application.yml**,然后在其中配置数据库连接信息: ``` spring: datasource: druid: url: jdbc:mysql://127.0.0.1:3306/yourdb?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC username: root password: 123456 initial-size: 1 min-idle: 1 max-active: 20 test-on-borrow: true # MySQL 8.x: com.mysql.cj.jdbc.Driver # MySQL 5.x: com.mysql.jdbc.Driver driver-class-name: com.mysql.cj.jdbc.Driver ``` 这里需要**注意**,如果是**MySQL8.X**,连接URL中需要追加:**serverTimezone=UTC** 来设置时区,否则报错; 同时,不同数据库版本,连接类名**driver-class-name**也不同,均在上述源码中体现。 ### 2、整合tk.mybatis #### 2.1 关于tk.mybatis tk.mybatis 是在 MyBatis 框架的基础上提供了很多工具,让开发更加高效。通俗讲,是MyBatis的增强版,其帮你实现了众多SQL语句,让你远离SQL。 #### 2.2 引入依赖 ```html tk.mybatis mapper-spring-boot-starter 2.0.2 ``` #### 2.3 配置application 在**application.yml**中追加以下内容: ``` mybatis: type-aliases-package: com.cxhit.hello.spring.boot.mybatis.entity #实体类的存放路径,如:com.funtl.hello.spring.boot.entity mapper-locations: classpath:mapper/*.xml #生成的xml文件放置路径(resources下) ``` 其中第一个**type-aliases-package**,就是存放**实体类**的路径 一般是:**com.yourdomain.yourprojectpackage.entity** 或**yom.yourdomain.yourprojectpackage.bean** 第二个**mapper-locations**,就是生成的**Mapper**文件存放的路径,一般在**resources下的mapper**文件夹中。 以上两个文件均**不需要手动添加**!!! #### 2.4 创建一个通用的父级接口 该接口的主要作用是让 DAO 层的接口继承该接口,以达到使用 tk.mybatis 的目的。 这里注意:**这个接口不能被扫描到,即不能和Main函数在相同的包(以及子包)中。** **(这是因为约定大于配置的原因,使得Spring默认扫描路径为Application所在包及其子包**) 可以在**src/main/java**下新建:**tk.mybatis**包,将**MyMapper.java**放置其中。 直接**右击java→new→file**,输入:**tk.mybatis.MyMapper.java** ,回车,即可。 该接口源码如下: ```java package tk.mybatis; import tk.mybatis.mapper.common.Mapper; import tk.mybatis.mapper.common.MySqlMapper; /** * 创建一个通用的父级接口 * 主要作用是让 DAO 层的接口继承该接口,以达到使用 tk.mybatis 的目的 */ /** * 自己的 Mapper * 特别注意,该接口不能被扫描到,否则会出错 * 【约定大于配置:Spring 默认扫描路径为Application所在包,即com.cxhit.hello.spring.boot.mybatis】 * Title: MyMapper * Description: * */ public interface MyMapper extends Mapper, MySqlMapper { } ``` ### 3、整合PageHelper #### 3.1 关于PageHelper PageHelper 是 Mybatis 的分页插件,支持多数据库、多数据源。可以简化数据库的分页查询操作,整合过程也极其简单,只需引入依赖即可。 #### 3.2 注入依赖 在 **`pom.xml`** 文件中引入 **`pagehelper-spring-boot-starter`** 依赖 ```html com.github.pagehelper pagehelper-spring-boot-starter 1.2.5 ``` ### 4、【关键】使用MyBatis的Maven插件生成代码 我们无需手动编写 实体类、DAO、XML 配置文件,只需要使用 MyBatis 提供的一个 Maven 插件就可以自动生成所需的各种文件便能够满足基本的业务需求,如果业务比较复杂只需要修改相关文件即可。 #### 4.1 配置插件 首先在**pom.xml**文件中的** …… ** 中添加 **mybatis-generator-maven-plugin** 插件 具体源码如下: ```html org.mybatis.generator mybatis-generator-maven-plugin 1.3.5 ${basedir}/src/main/resources/generator/generatorConfig.xml true true mysql mysql-connector-java ${mysql.version} tk.mybatis mapper 3.4.4 ``` 如上**TODO**位置处,需要自行创建**/src/main/resources/generator/generatorConfig.xml** 文件。 #### 4.2 配置generatorConfig.xml文件 如上创建**generatorConfig.xml**文件后,在其中写入如下代码: ```html ``` 如上,第一处 **TODO** 位置,需要在**resources**文件夹下创建**jdbc.properties** 文件; 如上,第二处 **TODO** 位置,需要修改为自己项目中的**MyMapper通用父级接口**的位置,即***2.4***位置处所讲述的接口文件; 如上,第三个 **TODO** 位置,需要修改为自己的**实体类包entity**(部分习惯用**Bean**)计划保存路径(无需手动创建,自动生成); 如上,第四个 **TODO** 位置,基本不需要修改; 如上,第五个 **TODO** 位置,需要修改为自己的**DAO**计划保存路径(无需手动创建,系统自动生成); 如上,第六个 **TODO** 位置,修改为自己的数据库名称。 #### 4.3 配置数据源 在 **src/main/resources** 下创建 **jdbc.properties**(对应上文**第一处** **TODO** ) **jdbc.properties**中源码如下: ``` # MySQL 5.x: com.mysql.jdbc.Driver # MySQL 8.x: com.mysql.cj.jdbc.Driver jdbc.driverClass=com.mysql.cj.jdbc.Driver jdbc.connectionURL=jdbc:mysql://127.0.0.1:3306/mz_book_v2?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC jdbc.username=root jdbc.password=123456 ``` 至此,项目基本上配置完毕,初始状态如下所示:  #### 4.4 执行自动生成命令 打开 **idea** 页面侧边栏(最右边)的 **Maven Projects** ,找到 **Plugins/mybatis-generator** ,双击 **mybatis-generator:genetage** ,即可**自动完成entity、DAO、Mapper的代码**生成。  双击打开任意一个entity,如本项目中的book实体,即可看到所有的属性均已经定义完毕。  注意:这里需要将**所有实体**的 **@Table(name = "mz_book_v2..book")** 中的**数据库名字加多余的两个点**删除 如上,修改为:  因为**MySQL不支持Catalog** 。详见:https://blog.csdn.net/ddfdjffd/article/details/90712080 如果不修改,则自动执行的SQL语句为:  报错。 ### 5、测试MyBatis操作数据库 这里使用的数据库是一个书籍的数据库,两个表,一个表是图书分类表,另一个是书籍详情表。可以使用任意数据库测试。 #### 5.1 修改入口类 需要使用 **`@MapperScan`** 注解来指定 **Mapper** 接口的路径 **PS:** 注意这里的 **`@MapperScan`** 注解是 `**tk.mybatis.spring.annotation.MapperScan**;` 包下的,如下所示:   #### 5.2 创建测试类: 在 **test** 文件夹下的 **HelloSpringBootMybatisApplicationTests** 类中创建测试类,进行测试即可。 如下是测试 **select** 和 **分页查询** 的源码,以及运行结果: ```java package com.cxhit.hello.spring.boot.mybatis; import com.cxhit.hello.spring.boot.mybatis.dao.BookMapper; import com.cxhit.hello.spring.boot.mybatis.dao.CategoryMapper; import com.cxhit.hello.spring.boot.mybatis.entity.Book; import com.cxhit.hello.spring.boot.mybatis.entity.Category; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.annotation.Rollback; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.transaction.annotation.Transactional; import tk.mybatis.mapper.entity.Example; import java.util.List; @RunWith(SpringRunner.class) @SpringBootTest(classes = HelloSpringBootMybatisApplication.class) @Transactional @Rollback public class HelloSpringBootMybatisApplicationTests { /** * 注入数据查询接口 */ @Autowired private CategoryMapper categoryMapper; /** * 注入数据查询接口 */ @Autowired private BookMapper bookMapper; @Test public void testSelect() { List categories = categoryMapper.selectAll(); for (Category category : categories) { System.out.println(category.getCatName()); } } @Test public void testPage() { PageHelper.startPage(2, 20); Example example = new Example(Book.class); PageInfo pageInfo = new PageInfo<>(bookMapper.selectByExample(example)); List list = pageInfo.getList(); for (Book book : list) { System.out.println(book.getBookName()); } } } ``` 测试 select 运行结果:  测试 分页查询 运行结果:  ### 6、附录:完整源码(从上到下)  src/main/java/com.cxhit.hello.spring.boot.mybatis.**HelloSpringBootMybatisApplication.java**: ```java package com.cxhit.hello.spring.boot.mybatis; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import tk.mybatis.spring.annotation.MapperScan; @SpringBootApplication @MapperScan(basePackages = "com.cxhit.hello.spring.boot.mybatis.dao") public class HelloSpringBootMybatisApplication { public static void main(String[] args) { SpringApplication.run(HelloSpringBootMybatisApplication.class, args); } } ``` tk.mybatis.**MyMapper.java**: ```java package tk.mybatis; import tk.mybatis.mapper.common.Mapper; import tk.mybatis.mapper.common.MySqlMapper; /** * 创建一个通用的父级接口 * 主要作用是让 DAO 层的接口继承该接口,以达到使用 tk.mybatis 的目的 */ /** * 自己的 Mapper * 特别注意,该接口不能被扫描到,否则会出错 * 【约定大于配置:Spring 默认扫描路径为Application所在包,即com.cxhit.hello.spring.boot.mybatis】 */ public interface MyMapper extends Mapper, MySqlMapper { } ``` resources.generator.**generatorConfig.xml**: ```html ``` resources.**application.yml**: ``` spring: datasource: druid: url: jdbc:mysql://127.0.0.1:3306/mz_book_v2?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC username: root password: 123456 initial-size: 1 min-idle: 1 max-active: 20 test-on-borrow: true # MySQL 8.x: com.mysql.cj.jdbc.Driver # MySQL 5.x: com.mysql.jdbc.Driver driver-class-name: com.mysql.cj.jdbc.Driver mybatis: type-aliases-package: com.cxhit.hello.spring.boot.mybatis.entity #实体类的存放路径,如:com.funtl.hello.spring.boot.entity mapper-locations: classpath:mapper/*.xml #生成的xml文件放置路径(resources下) ``` resources.**jdbc.properties**: ``` # MySQL 8.x: com.mysql.cj.jdbc.Driver jdbc.driverClass=com.mysql.cj.jdbc.Driver jdbc.connectionURL=jdbc:mysql://127.0.0.1:3306/mz_book_v2?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC jdbc.username=root jdbc.password=123456 ``` src/main/java/com.cxhit.hello.spring.boot.mybatis.**HelloSpringBootMybatisApplicationTests.java**: ```java package com.cxhit.hello.spring.boot.mybatis; import com.cxhit.hello.spring.boot.mybatis.dao.BookMapper; import com.cxhit.hello.spring.boot.mybatis.dao.CategoryMapper; import com.cxhit.hello.spring.boot.mybatis.entity.Book; import com.cxhit.hello.spring.boot.mybatis.entity.Category; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.annotation.Rollback; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.transaction.annotation.Transactional; import tk.mybatis.mapper.entity.Example; import java.util.List; @RunWith(SpringRunner.class) @SpringBootTest(classes = HelloSpringBootMybatisApplication.class) @Transactional @Rollback public class HelloSpringBootMybatisApplicationTests { /** * 注入数据查询接口 */ @Autowired private CategoryMapper categoryMapper; /** * 注入数据查询接口 */ @Autowired private BookMapper bookMapper; @Test public void testSelect() { List categories = categoryMapper.selectAll(); for (Category category : categories) { System.out.println(category.getCatName()); } } @Test public void testPage() { PageHelper.startPage(2, 5); Example example = new Example(Book.class); PageInfo pageInfo = new PageInfo<>(bookMapper.selectByExample(example)); List list = pageInfo.getList(); for (Book book : list) { System.out.println(book.getBookName()); } } } ``` **pom.xml**: ```html 4.0.0 org.springframework.boot spring-boot-starter-parent 2.1.13.RELEASE com.cxhit hello-spring-boot-mybatis 1.0.0-SNAPSHOT hello-spring-boot-mybatis Demo project for Spring Boot 1.8 org.springframework.boot spring-boot-starter-thymeleaf org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test com.alibaba druid-spring-boot-starter 1.1.10 mysql mysql-connector-java runtime tk.mybatis mapper-spring-boot-starter 2.0.2 com.github.pagehelper pagehelper-spring-boot-starter 1.2.5 org.springframework.boot spring-boot-maven-plugin org.mybatis.generator mybatis-generator-maven-plugin 1.3.5 ${basedir}/src/main/resources/generator/generatorConfig.xml true true mysql mysql-connector-java ${mysql.version} tk.mybatis mapper 3.4.4 ``` ### 7、补充: 如果测试类中创建的接口有红色下划线(波浪线),如下图所示:  只需要将 **DAO** 接口加上注解 **@Repository** 即可。  该文件为系统自动生成,默认是没有该注解的。 本文采用创作共用版权 CC BY-NC-SA 3.0 CN 许可协议,转载或复制请注明出处! -- 展开阅读全文 --