- MyBatic-Plus 简介
- SpringBoot 集成MyBatis-Plus
- 分页查询
简介
MyBatis-Plus(简称 MP)是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。官方网址
核心功能: 代码生成器,CURD接口,条件构造器,分页插件,Sequence主键,自定义ID生成器
插件扩展: 热加载,逻辑删除,通用枚举,字段类型处理器,自动填充功能,Sql注入器,攻击SQL阻断解析器,性能分析插件,执行SQL分析打印,乐观锁插件,动态数据源,分布式事务,多租户SQL解析器,动态表名SQL解析器,MybatisX快速开发插件
SpringBoot 集成 MyBatis-Plus
- 创建一个Web项目,添加依赖
https://start.spring.io/,pom 文件如下:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.2.0</version>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
- 在application.yml 文件中配置mysql
# DataSource Config
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
username: root
password: root
- 编写实体类,Mapper类
@Data
public class User {
private Long id;
private String name;
private Integer age;
private String email;
}
public interface UserMapper extends BaseMapper<User> {
}
- 在 Spring Boot 启动类中添加 @MapperScan 注解,扫描 Mapper 文件夹:
@SpringBootApplication
@MapperScan("top.lconcise.mapper")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
目录结构如下:
- 测试:
@RunWith(SpringRunner.class)
@SpringBootTest
public class ApplicationTests {
@Autowired
private UserMapper userMapper;
@Test
public void contextLoads() {
System.out.println(("----- selectAll method test ------"));
List<User> userList = userMapper.selectList(null);
Assert.assertEquals(5, userList.size());
userList.forEach(System.out::println);
}
}
控制台输出:
User(id=1, name=Jone, age=18, email=test1@baomidou.com)
User(id=2, name=Jack, age=20, email=test2@baomidou.com)
User(id=3, name=Tom, age=28, email=test3@baomidou.com)
User(id=4, name=Sandy, age=21, email=test4@baomidou.com)
User(id=5, name=Billie, age=24, email=test5@baomidou.com)
通过以上几个简单的步骤,我们就实现了 User 表的 CRUD 功能,甚至连 XML 文件都不用编写!
分页查询
- 配置
@EnableTransactionManagement
@Configuration
@MapperScan("com.baomidou.cloud.service.*.mapper*")
public class MybatisPlusConfig {
@Bean
public PaginationInterceptor paginationInterceptor() {
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
// 设置请求的页面大于最大页后操作, true调回到首页,false 继续请求 默认false
// paginationInterceptor.setOverflow(false);
// 设置最大单页限制数量,默认 500 条,-1 不受限制
// paginationInterceptor.setLimit(500);
return paginationInterceptor;
}
}
- 编码
public interface UserMapper extends BaseMapper<User> {
IPage<User> selectPageVo(Page page, @Param("name") String name);
}
编写xml文件
<?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="top.lconcise.mapper.UserMapper">
<select id="selectPageVo" resultType="top.lconcise.domain.User">
SELECT id,`name` FROM user WHERE true
<if test="name != null and name !=''">AND `name` LIKE CONCAT('%',#{name},'%')</if>
</select>
</mapper>
- 对于IDEA系列编辑器,XML 文件是不能放在 java 文件夹中的,IDEA 默认不会编译源码文件夹中的 XML 文件,可以参照以下方式解决:
- 指定 POM 文件的 resource
<build>
<resources>
<resource>
<!-- xml放在java目录下-->
<directory>src/main/java</directory>
<includes>
<include>**/mapper/*.xml</include>
</includes>
</resource>
<!--指定资源的位置(xml放在resources下,可以不用指定)-->
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
<build>
- 测试:
@Test
public void contextLoads() {
IPage<User> userIPage = userMapper.selectPageVo(new Page(1, 5), "J");
userIPage.getRecords().forEach(System.out::println);
}
控制台输出:
User(id=1, name=Jone, age=null, email=null)
User(id=2, name=Jack, age=null, email=null)
源码地址:https://github.com/lbshold/springboot/tree/master/SpringBoot-MyBatisPlus