最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

MyBatis-Plus allEq()的用法詳解

 更新時(shí)間:2020年12月23日 14:20:22   作者:ITKaven  
這篇文章主要介紹了MyBatis-Plus allEq()的用法詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

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ù),方便演示。

在這里插入圖片描述

我們首先來看一看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);
 }

 /**
  * 字段過濾接口,傳入多參數(shù)時(shí)允許對(duì)參數(shù)進(jìn)行過濾
  *
  * @param condition 執(zhí)行條件
  * @param filter  返回 true 來允許字段傳入比對(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 : 過濾函數(shù),是否允許字段傳入比對(duì)條件中。
  • params : key為數(shù)據(jù)庫(kù)字段名,value為字段值。
  • null2IsNull : 為true時(shí),則在mapvaluenull時(shí)調(diào)用 isNull 方法;為false時(shí),則忽略mapvaluenull的鍵值對(duì)。

先來演示一下 Map<R, V> params參數(shù)的作用。

查詢usernamekaven,age22。

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ù)的作用,它默認(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)闆]有這樣的數(shù)據(jù),大家看一看上圖的sql語(yǔ)句就知道了,null2IsNulltrue時(shí),則在mapvaluenull時(shí)調(diào)用 isNull 方法。

再來演示一下null2IsNullfalse的情況。

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)null2IsNullfalse時(shí),其實(shí)上面代碼就是查詢所有user數(shù)據(jù),因?yàn)殒I值對(duì)都被忽略掉了。如果此時(shí)null2IsNulltrue,則會(huì)沒有一個(gè)匹配的數(shù)據(jù)。

在這里插入圖片描述

結(jié)果是正確的,當(dāng)null2IsNullfalse時(shí),則忽略mapvaluenull的情況。

再來演示一下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á)式,意思是keypassword的鍵值對(duì)在查詢時(shí)會(huì)被忽略掉。

結(jié)果如下:

在這里插入圖片描述

結(jié)果很顯然也是正確的。

boolean condition參數(shù)的作用我在另一篇博客有介紹過,這里就不再贅述了。
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)文章

  • Maven 配置文件 生命周期 常用命令詳解

    Maven 配置文件 生命周期 常用命令詳解

    Maven是圍繞著構(gòu)建生命周期的核心概念為原型,整個(gè)項(xiàng)目的創(chuàng)建和部署都是圍繞著生命周期展開的,一個(gè)生命周期由若干個(gè)生命周期階段組成。下面通過本文給大家介紹Maven 配置文件 生命周期 常用命令詳解,一起看看吧
    2017-11-11
  • Java實(shí)現(xiàn)常用的三種加密算法詳解

    Java實(shí)現(xiàn)常用的三種加密算法詳解

    編程中常見的加密算法有以下幾種:信息摘要算法、對(duì)稱加密算法以及非對(duì)稱加密算法。本文將利用Java實(shí)現(xiàn)這幾種常見的加密算法,需要的可以參考一下
    2022-03-03
  • SpringBoot自定義線程池,執(zhí)行定時(shí)任務(wù)方式

    SpringBoot自定義線程池,執(zhí)行定時(shí)任務(wù)方式

    這篇文章主要介紹了SpringBoot自定義線程池,執(zhí)行定時(shí)任務(wù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-04-04
  • Intellij搭建springmvc常見問題解決方案

    Intellij搭建springmvc常見問題解決方案

    這篇文章主要介紹了Intellij搭建springmvc常見問題解決方案,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-10-10
  • java實(shí)現(xiàn)163郵箱發(fā)送郵件到qq郵箱成功案例

    java實(shí)現(xiàn)163郵箱發(fā)送郵件到qq郵箱成功案例

    這篇文章主要為大家分享了java實(shí)現(xiàn)163郵箱發(fā)送郵件到qq郵箱成功案例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-05-05
  • Java模擬實(shí)現(xiàn)機(jī)場(chǎng)過安檢處理流程

    Java模擬實(shí)現(xiàn)機(jī)場(chǎng)過安檢處理流程

    這篇文章主要為大家詳細(xì)介紹了用Java模擬實(shí)現(xiàn)機(jī)場(chǎng)安全檢查流程的案例,涉及線程的相關(guān)知識(shí),由子線程不斷的檢查通過旅客,感興趣的小伙伴們可以參考一下
    2022-04-04
  • SpringBoot單機(jī)限流的實(shí)現(xiàn)

    SpringBoot單機(jī)限流的實(shí)現(xiàn)

    在系統(tǒng)運(yùn)維中, 有時(shí)候?yàn)榱吮苊庥脩舻膼阂馑⒔涌? 會(huì)加入一定規(guī)則的限流,本文主要介紹了SpringBoot單機(jī)限流的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-08-08
  • IDEA使用學(xué)生郵箱無法注冊(cè)問題:JetBrains Account connection error: 拒絕連接

    IDEA使用學(xué)生郵箱無法注冊(cè)問題:JetBrains Account connection error: 拒絕連接

    這篇文章主要介紹了IDEA使用學(xué)生郵箱無法注冊(cè)問題:JetBrains Account connection error: 拒絕連接,文中通過圖文及示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • Java通過jersey實(shí)現(xiàn)客戶端圖片上傳示例

    Java通過jersey實(shí)現(xiàn)客戶端圖片上傳示例

    本篇文章主要介紹了Java通過jersey實(shí)現(xiàn)客戶端圖片上傳示例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。
    2017-03-03
  • spring-redis-session 自定義 key 和過期時(shí)間

    spring-redis-session 自定義 key 和過期時(shí)間

    這篇文章主要介紹了spring-redis-session 自定義 key 和過期時(shí)間,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-12-12

最新評(píng)論

韩城市| 东阿县| 竹山县| 五大连池市| 伊宁市| 浦江县| 徐汇区| 金寨县| 萍乡市| 莆田市| 敦化市| 昔阳县| 尖扎县| 清远市| 东阳市| 西乌珠穆沁旗| 洪洞县| 南投县| 连南| 杨浦区| 安图县| 华安县| 安国市| 贡觉县| 衡南县| 郑州市| 滦南县| 益阳市| 阳原县| 陇川县| 铅山县| 庆城县| 东明县| 阿鲁科尔沁旗| 邹平县| 宣汉县| 义马市| 汽车| 固始县| 屏东市| 五原县|