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參數內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!
推薦閱讀:
- MyBatis-Plus 查詢指定字段的實現
- MyBatis-Plus allEq()的用法詳解
- MyBatis-Plus 之selectMaps、selectObjs、selectCount、selectOne的使用
- mybaties plus selectMaps和selectList的區別說明
- mybatis-plus 查詢傳入參數Map,返回List<Map>方式