MyBatis-Plus条件构造器之condition参数的使用
首先创建一个数据库表,如下图所示:
然后创建一个Spring Boot项目,pom.xml和配置如下:
<?xml version="1.0" encoding="UTF-8"?> <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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.kaven</groupId> <artifactId>mybatis-plus</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.4.RELEASE</version> <relativePath/> </parent> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webflux</artifactId> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.0</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.49</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
spring: application: name: mybatis-plus datasource: driver-class-name: com.mysql.jdbc.Driver username: root password: ITkaven@123 url: jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf-8&useSSL=false server: port: 8085 logging: level: root: warn com.kaven.mybatisplus.dao: trace pattern: console: '%p%m%n' mybatis-plus: mapper-locations: classpath:mappers/*.xml
实体类User:
package com.kaven.mybatisplus.entity; import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; import lombok.Data; @TableName("user") @Data public class User { @TableId private String id; @TableField("username") private String username; @TableField("password") private String password; @TableField("age") private Integer age; /** * 使用 @TableField(exist = false) ,表示该字段在数据库中不存在 ,所以不会插入数据库中 * 使用 transient 、 static 修饰属性也不会插入数据库中 */ @TableField(exist = false) private String phone; }
Mapper接口UserMapper:
package com.kaven.mybatisplus.dao; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.kaven.mybatisplus.entity.User; import org.springframework.stereotype.Component; @Component public interface UserMapper extends BaseMapper<User> {}
启动类:
package com.kaven.mybatisplus; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @MapperScan(basePackages = "com.kaven.mybatisplus.dao") public class AppRun { public static void main(String[] args) { SpringApplication.run(AppRun.class , args); } }
@MapperScan(basePackages = "com.kaven.mybatisplus.dao")
这个一定要加上。
我们先在数据库中添加几行数据,方便演示。
我之前介绍的条件构造器都没有使用过condition
参数。从AbstractWrapper<T, String, QueryWrapper<T>>
的源码可以看到很多方法都有condition
参数,它是一个布尔型的参数,意思就是是否将该sql
语句(像in()
、like()
)加在总sql
语句上,如下图所示。
首先我们自己来实现一个和condition
参数一样功能的方法。
查询username
包含字符k
,并且age
属于[22 , 40 , 30 ]。
package com.kaven.mybatisplus.dao; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.kaven.mybatisplus.entity.User; 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.context.junit4.SpringRunner; import org.springframework.util.CollectionUtils; import org.springframework.util.StringUtils; import java.util.Arrays; import java.util.List; @RunWith(SpringRunner.class) @SpringBootTest public class UserMapperConditionTest { @Autowired private UserMapper userMapper; @Test public void selectList(){ String username = "k"; List<Integer> ageList = Arrays.asList(22 , 40 , 30); List<User> userList = userMapper.selectList(condition(username , ageList)); userList.forEach(System.out::println); } public QueryWrapper<User> condition(String username , List<Integer> ageList){ QueryWrapper<User> userQueryWrapper = new QueryWrapper<>(); if(!StringUtils.isEmpty(username)){ userQueryWrapper.like("username" , username); } if(!CollectionUtils.isEmpty(ageList)){ userQueryWrapper.in("age" , ageList); } return userQueryWrapper; } }
结果如下:
结果是正确的。
我们使用condition
参数来实现一下。
package com.kaven.mybatisplus.dao; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.kaven.mybatisplus.entity.User; 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.context.junit4.SpringRunner; import org.springframework.util.CollectionUtils; import org.springframework.util.StringUtils; import java.util.Arrays; import java.util.List; @RunWith(SpringRunner.class) @SpringBootTest public class UserMapperConditionTest { @Autowired private UserMapper userMapper; @Test public void selectList(){ String username = "k"; List<Integer> ageList = Arrays.asList(22 , 40 , 30); List<User> userList = userMapper.selectList(condition(username , ageList)); userList.forEach(System.out::println); } public QueryWrapper<User> condition(String username , List<Integer> ageList){ QueryWrapper<User> userQueryWrapper = new QueryWrapper<>(); // if(!StringUtils.isEmpty(username)){ // userQueryWrapper.like("username" , username); // } // if(!CollectionUtils.isEmpty(ageList)){ // userQueryWrapper.in("age" , ageList); // } userQueryWrapper.like(!StringUtils.isEmpty(username) , "username" , username) .in(!CollectionUtils.isEmpty(ageList) , "age" , ageList); return userQueryWrapper; } }
结果如下:
结果也是正确的。
到此这篇关于MyBatis-Plus条件构造器之condition参数的使用的文章就介绍到这了,更多相关MyBatis-Plus condition参数内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!
赞 (0)