Spring Boot整合MyBatis-plus
提示
本文以 MySQL 为例
# 依赖
MyBatis-Plus 版本:Maven Central: com.baomidou:mybatis-plus (opens new window)
MySQL JDBC 版本:Maven Central: mysql:mysql-connector-java (opens new window)
<!-- MyBatis-Plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.3.2</version>
</dependency>
<!--MySQL jdbc驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.33</version>
</dependency>
# 配置
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3340/code-example?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai
username: root
password: 123456
mybatis-plus:
configuration:
# 打印 sql
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
参考变量名:
SPRING_DATASOURCE_URL
SPRING_DATASOURCE_USERNAME
SPRING_DATASOURCE_PASSWORD
# 启动类
@MapperScan("cn.nipx.example.mapper")
# 扩展 PageHelper
笔者觉得 Mybatis-plus 的分页功能并不好用,因此笔者更常使用 PageHelper
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
笔记
无需复杂配置,导入 starter,开箱即用。
参考:[pagehelper/pagehelper-spring-boot: pagehelper-spring-boot](pagehelper/pagehelper-spring-boot: pagehelper-spring-boot )
# 扩展 原版 Mybatis 日志
logging:
level:
cn.nipx.example.mapper: DEBUG
上次更新: 2024/01/16, 18:00:00