MyBatis-Plus条件构造器之condition参数的使用
程序员文章站
2022-03-27 09:27:21
首先创建一个数据库表,如下图所示:然后创建一个spring boot项目,pom.xml和配置如下:
首先创建一个数据库表,如下图所示:
然后创建一个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参数内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!