举个栗子
mybatis-plus中集成了import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
下面举例一个条件查询
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.6.2version>
<relativePath/>
parent>
<groupId>cc.soufflegroupId>
<artifactId>mybatis_plus_caseartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>mybatis_plus_casename>
<properties>
<java.version>1.8java.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
<version>3.4.2version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<scope>runtimescope>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
exclude>
excludes>
configuration>
plugin>
plugins>
build>
project>
根据mybatis-plus自动生成的三层架构进行实现
,在分页的操作中, 因为不同的数据库的拼接语句不一样所有需要指定数据库类型
@Configuration
public class MpConfig {
/**
* 配置MP 分页插件MybatisPlusInterceptor
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
//指定数据库类型, 这样配置之后, 会自动拼接limit这个关键字语句 ; 因为不同的数据看的拼接语句不一样所有需要指定数据库类型
PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor(DbType.MYSQL);
mybatisPlusInterceptor.addInnerInterceptor(paginationInnerInterceptor);
return mybatisPlusInterceptor;
}
}
//条件查询:根据姓名、性别、出生年月组合条件进行查询 select * from user where user_name like "%周%" and user_sex=0 and user_birthday between ? and ?
//思考:根据条件查询,那么查询的list对象是null还是元素为size=0
@Test
void selectByQueryWrapper() {
//姓名
String userName = "周";
//性别
Integer userSex = null;
//出生年月最小值
LocalDate userBirthDayMin = null;
//出生年月最大值
LocalDate userBirthDayMax = null;
//根据以上4个变量来动态拼接SQL, 定义查询条件
QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
//这里的Right 表示的是 百分号% 位置, 右边就是指定为放在右边数据的右边, 即 周%
userQueryWrapper.likeLeft(StringUtils.isNotBlank(userName),"user_name","周");
userQueryWrapper.eq(userSex != null,"user_sex",0);
userQueryWrapper.between(((userBirthDayMin !=null ) && (userBirthDayMax != null)),"user_birthday","2020-01-01","2010-11-05");
// 第一个参数current: 表示需要第几页的数据, 第二个参数size, 表示一页多少个数据
Page<User> userPage = new Page<>(1,4);
Page<User> userPageData = userMapper.selectPage(userPage, userQueryWrapper);
//数据需要进行单独的获取
List<User> records = userPageData.getRecords();
System.out.println(records);
records.forEach(user -> System.out.println("user = " + user));
}
— 文章完 —