SpringBoot集合(二):SpringBoot集成MyBatis-Plus
一、MyBatis-Plus简介:#
虽然mybatis可以直接在xml中通过SQL语句操作数据库,很是灵活。但正其操作都要通过SQL语句进行,就必须写大量的xml文件,很是麻烦。mybatis-plus就很好的解决了这个问题。
MyBatis-Plus(简称 MP)是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
关于mybatis-plus的更多介绍及特性,可以参考mybatis-plus官网。
二、SpringBoot集成MyBatis-Plus:#
1、首先要创建一个SpringBoot项目。如何创建请查看《创建一个SpringBoot项目》。#
2、加入相关的依赖。#
正如官方所说,mybatis-plus在mybatis的基础上只做增强不做改变,因此其与spring的整合亦非常简单。只需把mybatis的依赖换成mybatis-plus的依赖,再把sqlSessionFactory换成mybatis-plus的即可。如下:

1 <dependencies>
2 <dependency>
3 <groupId>org.springframework.boot</groupId>
4 <artifactId>spring-boot-starter-web</artifactId>
5 </dependency>
6
7 <dependency>
8 <groupId>org.springframework.boot</groupId>
9 <artifactId>spring-boot-starter-test</artifactId>
10 <scope>test</scope>
11 <exclusions>
12 <exclusion>
13 <groupId>org.junit.vintage</groupId>
14 <artifactId>junit-vintage-engine</artifactId>
15 </exclusion>
16 </exclusions>
17 </dependency>
18 <!-- 这是mysql的依赖 -->
19 <dependency>
20 <groupId>mysql</groupId>
21 <artifactId>mysql-connector-java</artifactId>
22 <scope>runtime</scope>
23 </dependency>
24 <!-- 这是lombok的依赖 -->
25 <dependency>
26 <groupId>org.projectlombok</groupId>
27 <artifactId>lombok</artifactId>
28 <optional>true</optional>
29 </dependency>
30 <!-- 这是mybatis-plus依赖 -->
31 <dependency>
32 <groupId>com.baomidou</groupId>
33 <artifactId>mybatis-plus-boot-starter</artifactId>
34 <version>3.1.1</version>
35 </dependency>
36 <dependency>
37 <groupId>com.baomidou</groupId>
38 <artifactId>mybatis-plus</artifactId>
39 <version>3.0.5</version>
40 </dependency>
41 <dependency>
42 <groupId>org.springframework.boot</groupId>
43 <artifactId>spring-boot-starter-jdbc</artifactId>
44 </dependency>
45 <!-- 这是mybatis-plus的代码自动生成器 -->
46 <dependency>
47 <groupId>com.baomidou</groupId>
48 <artifactId>mybatis-plus-generator</artifactId>
49 <version>3.1.1</version>
50 </dependency>
51 <!-- 这是模板引擎依赖 -->
52 <dependency>
53 <groupId>org.freemarker</groupId>
54 <artifactId>freemarker</artifactId>
55 </dependency>
56 <!--热部署 每次修改代码springboot自动重启服务-->
57 <dependency>
58 <groupId>org.springframework.boot</groupId>
59 <artifactId>spring-boot-devtools</artifactId>
60 <optional>true</optional>
61 </dependency>
62 <!--数据库连接池 -->
63 <dependency>
64 <groupId>com.alibaba</groupId>
65 <artifactId>druid</artifactId>
66 <version>1.1.21</version>
67 </dependency>
68 <!--单元测试 -->
69 <dependency>
70 <groupId>junit</groupId>
71 <artifactId>junit</artifactId>
72 </dependency>
73 </dependencies>
3、加入相关的配置。#

1 #端口号
2 server:
3 port: 8088
4 #数据库的配置信息
5 spring:
6 datasource: #数据源配置
7 url: jdbc:mysql://rm-hp3qgn64g7i3s4yujco.mysql.huhehaote.rds.aliyuncs.com:3306/app?characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&autoReconnect=true&failOverReadOnly=false&zeroDateTimeBehavior=convertToNull&useSSL=false&allowMultiQueries=true
8 driver-class-name: com.mysql.cj.jdbc.Driver
9 username:
10 password:
11 platform: mysql
12 type: com.alibaba.druid.pool.DruidDataSource
13 druid:
14 initial-size: 5 #初始化时建立物理连接的个数
15 min-idle: 5 #最小连接池数量
16 max-active: 200 #最大连接池数量
17 max-wait: 1200 #获取连接时最大等待时间,单位毫秒
18 pool-prepared-statements: false #PSCache对支持游标的数据库性能提升巨大,比如说oracle。在mysql下建议关闭
19 validation-query: SELECT 1
20 validation-query-timeout: 3
21 test-while-idle: true
22 test-on-borrow: true
23 test-on-return: true
24 time-between-eviction-runs-millis: 60000 #test-while-idle的判断依据
25 min-evictable-idle-time-millis: 30000 #连接保持空闲而不被驱逐的最小时间
26 async-close-connection-enable: true
27 filters: stat,wall,slf4j
28 filter:
29 stat:
30 db-type: mysql
31 log-slow-sql: true
32 slow-sql-millis: 2000
33 wall:
34 enabled: true
35 db-type: mysql
36
37 redis:
38 host: 39.104.60.156
39 port: 6379
40 password:
41 database: 10
42 lettuce:
43 pool:
44 max-idle: 8
45 min-idle: 0
46 max-active: 8
47 max-wait: -1
48 mybatis:
49 #开启驼峰命名法
50 configuration:
51 map-underscore-to-camel-case: true
52
53 mybatis-plus:
54 #mybatis
55 #Mapper路径
56 # 如果是放在src/main/java目录下 classpath:/com/yourpackage/*/mapper/*Mapper.xml
57 # 如果是放在resource目录 classpath:/mapper/*Mapper.xml
58 mapper-locations: classpath:mappers/*Mapper.xml
59 # 实体扫描,多个package用逗号或者分号分隔
60 type-aliases-package: com.example.demo.entity #自己的实体类地址
61 configuration:
62 # 这个配置会将执行的sql打印出来,在开发或测试的时候可以用
63 log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
64 #数据库字段与数据对象字段的映射策略
65 map-underscore-to-camel-case: true
66 cache-enabled: false
67
68 global-config:
69 db-config:
70 # #主键类型 0:"数据库ID自增", 1:"用户输入ID",2:"全局唯一ID (数字类型唯一ID)", 3:"全局唯一ID UUID";
71 # id-type: id_worker
72 # #字段策略 0:"忽略判断",1:"非 NULL 判断"),2:"非空判断"
73 # field-strategy: not_empty
74 # #驼峰下划线转换
75 # column-underline: true
76 # #数据库大写下划线转换
77 # #capital-mode: true
78 # #逻辑删除配置
79 # logic-delete-value: Y
80 # logic-not-delete-value: N
81 #数据库类型。支持主流的数据库
82 db-type: mysql
83 #刷新mapper 调试神器
84 refresh: true
85 # sql-injector: com.baomidou.mybatisplus.extension.injector.LogicSqlInjector
86 #自定义填充策略接口实现
87 #meta-object-handler: com.baomidou.springboot.xxx
88 #自定义SQL注入器
89 #sql-injector: com.baomidou.springboot.xxx
ps:此处自行配置数据库的相关信息。
4、MyBatis-Plus配置类。#

1 package com.example.demo.config;
2
3 import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
4 import com.baomidou.mybatisplus.extension.plugins.PerformanceInterceptor;
5 import org.springframework.context.annotation.Bean;
6 import org.springframework.context.annotation.Configuration;
7
8 @Configuration
9 public class MybatisPlusConfig {
10
11 /***
12 * Description:plus 的性能优化
13 * @author yang
14 * @since 2020-04-02
15 */
16 @Bean
17 public PerformanceInterceptor performanceInterceptor() {
18 PerformanceInterceptor performanceInterceptor = new PerformanceInterceptor();
19 /*<!-- SQL 执行性能分析,开发环境使用,线上不推荐。 maxTime 指的是 sql 最大执行时长 -->*/
20 performanceInterceptor.setMaxTime(1000);
21 /*<!--SQL是否格式化 默认false-->*/
22 performanceInterceptor.setFormat(true);
23 return performanceInterceptor;
24 }
25
26 /**
27 * @Description : mybatis-plus分页插件
28 * @author yang
29 * @since 2020-04-02
30 */
31 @Bean
32 public PaginationInterceptor paginationInterceptor() {
33 return new PaginationInterceptor();
34 }
35
36 }
5、项目结构。#
三、MyBatis-Plus代码生成器:#
代码生成器可以按照自己的配置,生成数据库中表相对应的Entity,Controller,Mapper,Service,ServiceImpl以及Mapper.xml。
具体代码已上传至GitHub。
代码生成器的详细功能及配置请参照mybatis-plus官网。
四、一些注意事项。#
1、配置完成后,注意在启动类中加入 @MapperScan("com.example.demo.mapper") MyBatis的包扫描路径。
2、实体类中,MyBatis-Plus只把id作为主键字典,非id的字段,需要@tableId来标注。其他注解参照官网。
3、mybatis-plus的核心jar包中已集成了mybatis和mybatis-spring,所以为避免冲突,请勿再次引用这两个jar包。
#
分类:
SpringBoot
, MyBatis-Puls
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现