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

然后創(chuàng)建一個(gè)Spring Boot項(xiàng)目,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
實(shí)體類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ù)庫(kù)中不存在 ,所以不會(huì)插入數(shù)據(jù)庫(kù)中
* 使用 transient 、 static 修飾屬性也不會(huì)插入數(shù)據(jù)庫(kù)中
*/
@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> {}
啟動(dòng)類:
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")這個(gè)一定要加上。
我們先在數(shù)據(jù)庫(kù)中添加幾行數(shù)據(jù),方便演示。

我們首先來(lái)看一看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 自動(dòng)執(zhí)行 isNull 方法, false 則忽略這個(gè)字段\
* @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);
}
/**
* 字段過(guò)濾接口,傳入多參數(shù)時(shí)允許對(duì)參數(shù)進(jìn)行過(guò)濾
*
* @param condition 執(zhí)行條件
* @param filter 返回 true 來(lái)允許字段傳入比對(duì)條件中
* @param params map 類型的參數(shù), key 是字段名, value 是字段值
* @param null2IsNull 是否參數(shù)為 null 自動(dòng)執(zhí)行 isNull 方法, false 則忽略這個(gè)字段
* @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這四個(gè)參數(shù)。
condition:是否將該sql語(yǔ)句(像in()、like(),以及這里的allEq())加在總sql語(yǔ)句上,也就是執(zhí)行條件。filter: 過(guò)濾函數(shù),是否允許字段傳入比對(duì)條件中。params:key為數(shù)據(jù)庫(kù)字段名,value為字段值。null2IsNull: 為true時(shí),則在map的value為null時(shí)調(diào)用isNull方法;為false時(shí),則忽略map的value為null的鍵值對(duì)。
先來(lái)演示一下 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é)果是正確的。
再來(lái)演示一下 boolean null2IsNull參數(shù)的作用,它默認(rèn)會(huì)是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é)果也是正確的,因?yàn)闆](méi)有這樣的數(shù)據(jù),大家看一看上圖的sql語(yǔ)句就知道了,null2IsNull為true時(shí),則在map的value為null時(shí)調(diào)用 isNull 方法。
再來(lái)演示一下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時(shí),其實(shí)上面代碼就是查詢所有user數(shù)據(jù),因?yàn)殒I值對(duì)都被忽略掉了。如果此時(shí)null2IsNull為true,則會(huì)沒(méi)有一個(gè)匹配的數(shù)據(jù)。

結(jié)果是正確的,當(dāng)null2IsNull為false時(shí),則忽略map中value為null的情況。
再來(lái)演示一下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ù)我傳了一個(gè)lambda表達(dá)式,意思是key為password的鍵值對(duì)在查詢時(shí)會(huì)被忽略掉。
結(jié)果如下:

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

