MyBatis-Plus allEq()的用法詳解
MyBatis-Plus allEq()的用法
首先創(chuàng)建一個數(shù)據(jù)庫表,如下圖所示:

然后創(chuàng)建一個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) ,表示該字段在數(shù)據(jù)庫中不存在 ,所以不會插入數(shù)據(jù)庫中
* 使用 transient 、 static 修飾屬性也不會插入數(shù)據(jù)庫中
*/
@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")這個一定要加上。
我們先在數(shù)據(jù)庫中添加幾行數(shù)據(jù),方便演示。

我們首先來看一看allEq的源碼:
/**
* ignore
*/
default <V> Children allEq(Map<R, V> params) {
return allEq(params, true);
}
/**
* ignore
*/
default <V> Children allEq(Map<R, V> params, boolean null2IsNull) {
return allEq(true, params, null2IsNull);
}
/**
* map 所有非空屬性等于 =
*
* @param condition 執(zhí)行條件
* @param params map 類型的參數(shù), key 是字段名, value 是字段值
* @param null2IsNull 是否參數(shù)為 null 自動執(zhí)行 isNull 方法, false 則忽略這個字段\
* @return children
*/
<V> Children allEq(boolean condition, Map<R, V> params, boolean null2IsNull);
/**
* ignore
*/
default <V> Children allEq(BiPredicate<R, V> filter, Map<R, V> params) {
return allEq(filter, params, true);
}
/**
* ignore
*/
default <V> Children allEq(BiPredicate<R, V> filter, Map<R, V> params, boolean null2IsNull) {
return allEq(true, filter, params, null2IsNull);
}
/**
* 字段過濾接口,傳入多參數(shù)時允許對參數(shù)進行過濾
*
* @param condition 執(zhí)行條件
* @param filter 返回 true 來允許字段傳入比對條件中
* @param params map 類型的參數(shù), key 是字段名, value 是字段值
* @param null2IsNull 是否參數(shù)為 null 自動執(zhí)行 isNull 方法, false 則忽略這個字段
* @return children
*/
<V> Children allEq(boolean condition, BiPredicate<R, V> filter, Map<R, V> params, boolean null2IsNull);
可以看到最多有boolean condition, BiPredicate<R, V> filter, Map<R, V> params, boolean null2IsNull這四個參數(shù)。
condition:是否將該sql語句(像in()、like(),以及這里的allEq())加在總sql語句上,也就是執(zhí)行條件。filter: 過濾函數(shù),是否允許字段傳入比對條件中。params:key為數(shù)據(jù)庫字段名,value為字段值。null2IsNull: 為true時,則在map的value為null時調(diào)用isNull方法;為false時,則忽略map的value為null的鍵值對。
先來演示一下 Map<R, V> params參數(shù)的作用。
查詢username為kaven,age為22。
package com.kaven.mybatisplus.dao;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
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 java.util.HashMap;
import java.util.List;
import java.util.Map;
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserMapperAllEqTest {
@Autowired
private UserMapper userMapper;
@Test
public void selectList(){
QueryWrapper<User> userQueryWrapper = Wrappers.query();
Map<String , Object> map = new HashMap<>();
map.put("username" , "kaven");
map.put("age" , 22);
userQueryWrapper.allEq(map);
List<User> userList = userMapper.selectList(userQueryWrapper);
}
}
結(jié)果如下:

查詢結(jié)果是正確的。
再來演示一下 boolean null2IsNull參數(shù)的作用,它默認會是true。
package com.kaven.mybatisplus.dao;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
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 java.util.HashMap;
import java.util.List;
import java.util.Map;
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserMapperAllEqTest {
@Autowired
private UserMapper userMapper;
@Test
public void selectList(){
QueryWrapper<User> userQueryWrapper = Wrappers.query();
Map<String , Object> map = new HashMap<>();
map.put("username" , "kaven");
map.put("age" , null);
userQueryWrapper.allEq(map , true);
List<User> userList = userMapper.selectList(userQueryWrapper);
}
}
結(jié)果如下:

結(jié)果也是正確的,因為沒有這樣的數(shù)據(jù),大家看一看上圖的sql語句就知道了,null2IsNull為true時,則在map的value為null時調(diào)用 isNull 方法。
再來演示一下null2IsNull為false的情況。
package com.kaven.mybatisplus.dao;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
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 java.util.HashMap;
import java.util.List;
import java.util.Map;
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserMapperAllEqTest {
@Autowired
private UserMapper userMapper;
@Test
public void selectList(){
QueryWrapper<User> userQueryWrapper = Wrappers.query();
Map<String , Object> map = new HashMap<>();
map.put("username" , null);
map.put("age" , null);
userQueryWrapper.allEq(map , false);
List<User> userList = userMapper.selectList(userQueryWrapper);
}
}
當(dāng)null2IsNull為false時,其實上面代碼就是查詢所有user數(shù)據(jù),因為鍵值對都被忽略掉了。如果此時null2IsNull為true,則會沒有一個匹配的數(shù)據(jù)。

結(jié)果是正確的,當(dāng)null2IsNull為false時,則忽略map中value為null的情況。
再來演示一下BiPredicate<R, V> filter參數(shù)的作用。
package com.kaven.mybatisplus.dao;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
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 java.util.HashMap;
import java.util.List;
import java.util.Map;
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserMapperAllEqTest {
@Autowired
private UserMapper userMapper;
@Test
public void selectList(){
QueryWrapper<User> userQueryWrapper = Wrappers.query();
Map<String , Object> map = new HashMap<>();
map.put("username" , null);
map.put("age" , 22);
map.put("password" , "1");
userQueryWrapper.allEq((k , v) -> !k.equals("password") , map , false);
List<User> userList = userMapper.selectList(userQueryWrapper);
}
}
看上面代碼很容易知道,filter參數(shù)我傳了一個lambda表達式,意思是key為password的鍵值對在查詢時會被忽略掉。
結(jié)果如下:

結(jié)果很顯然也是正確的。
boolean condition參數(shù)的作用我在另一篇博客有介紹過,這里就不再贅述了。
MyBatis-Plus 條件構(gòu)造器之condition參數(shù)
到此這篇關(guān)于MyBatis-Plus allEq()的用法詳解的文章就介紹到這了,更多相關(guān)MyBatis-Plus allEq()內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用kafka-console-consumer.sh不停報WARN的問題及解決
這篇文章主要介紹了使用kafka-console-consumer.sh不停報WARN的問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03
Spring boot2X Consul如何通過RestTemplate實現(xiàn)服務(wù)調(diào)用
這篇文章主要介紹了spring boot2X Consul如何通過RestTemplate實現(xiàn)服務(wù)調(diào)用,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-12-12
Spring Boot Async異步執(zhí)行任務(wù)過程詳解
這篇文章主要介紹了Spring Boot Async異步執(zhí)行任務(wù)過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-08-08
Java中如何將json字符串轉(zhuǎn)換成map/list
這篇文章主要介紹了Java中如何將json字符串轉(zhuǎn)換成map/list,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07
SpringCloud Feign傳遞HttpServletRequest對象流程
HttpServletRequest接口的對象代表客戶端的請求,當(dāng)客戶端通過HTTP協(xié)議訪問Tomcat服務(wù)器時,HTTP請求中的所有信息都封裝在HttpServletRequest接口的對象中,這篇文章介紹了Feign傳遞HttpServletRequest對象的流程,感興趣的同學(xué)可以參考下文2023-05-05

