MyBatis-Plus allEq()的用法
推荐:MyBatis Plus汇总
MyBatis-Plus allEq()的用法
首先创建一个数据库表,如下图所示:
然后创建一个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-plusdatasource:driver-class-name: com.mysql.jdbc.Driverusername: rootpassword: ITkaven@123url: jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf-8&useSSL=falseserver:port: 8085logging:level:root: warncom.kaven.mybatisplus.dao: tracepattern: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 {@TableIdprivate 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")
这个一定要加上。
我们先在数据库中添加几行数据,方便演示。
我们首先来看一看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 执行条件* @param params map 类型的参数, key 是字段名, value 是字段值* @param null2IsNull 是否参数为 null 自动执行 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);}/*** 字段过滤接口,传入多参数时允许对参数进行过滤** @param condition 执行条件* @param filter 返回 true 来允许字段传入比对条件中* @param params map 类型的参数, key 是字段名, value 是字段值* @param null2IsNull 是否参数为 null 自动执行 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
这四个参数。
condition
:是否将该sql
语句(像in()
、like()
,以及这里的allEq()
)加在总sql
语句上,也就是执行条件。filter
: 过滤函数,是否允许字段传入比对条件中。params
:key
为数据库字段名,value
为字段值。null2IsNull
: 为true
时,则在map
的value
为null
时调用isNull
方法;为false
时,则忽略map
的value
为null
的键值对。
先来演示一下 Map<R, V> params
参数的作用。
- 查询
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 {@Autowiredprivate UserMapper userMapper;@Testpublic 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);}
}
结果如下:
查询结果是正确的。
再来演示一下 boolean null2IsNull
参数的作用,它默认会是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 {@Autowiredprivate UserMapper userMapper;@Testpublic 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);}
}
结果如下:
结果也是正确的,因为没有这样的数据,大家看一看上图的sql
语句就知道了,null2IsNull
为true
时,则在map
的value
为null
时调用 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 {@Autowiredprivate UserMapper userMapper;@Testpublic 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);}
}
当null2IsNull
为false
时,其实上面代码就是查询所有user
数据,因为键值对都被忽略掉了。如果此时null2IsNull
为true
,则会没有一个匹配的数据。
结果是正确的,当null2IsNull
为false
时,则忽略map
中value
为null
的情况。
再来演示一下BiPredicate<R, V> filter
参数的作用。
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 {@Autowiredprivate UserMapper userMapper;@Testpublic 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
参数我传了一个lambda
表达式,意思是key
为password
的键值对在查询时会被忽略掉。
结果如下:
结果很显然也是正确的。
boolean condition
参数的作用我在另一篇博客有介绍过,这里就不再赘述了。
MyBatis-Plus 条件构造器之condition参数
写博客是博主记录自己的学习过程,如果有错误,请指正,谢谢!
MyBatis-Plus allEq()的用法相关推荐
- mybatis第三话 - mybatis的高端用法你会吗?
前面了解了springboot + mybatis的单数据源和多数据源的集成已经使用,本篇文章来聊聊mybatis的高端用法吧 1.环境搭配 1.1 pom依赖 基于springboot 2.5.6, ...
- Mybatis中的foreach用法
Mybatis中的foreach用法 目录 Mybatis中的foreach用法 元素属性 List对象集合查询 根据数组中的Id删除 update修改 元素属性 item: 集合中元素迭代时的别名, ...
- 关于mybatis中selectKey的用法
在使用MyBatis插入数据进入数据库的时候会用到sequence序列来生成自增的id 这时可以使用selectKey就可以得到sequence的值,同时也会将值返回.不过对于不同的数据库有不同的操作 ...
- Mybatis中parameterType的用法
在mybatis映射接口的配置中,有select,insert,update,delete等元素都提到了parameterType的用法,parameterType为输入参数,在配置的时候,配置相应的 ...
- Mybatis中的association用法
这篇文章我们将来学习一些 association 用法 表结构 DROP TABLE IF EXISTS `student`; CREATE TABLE `student` ( `id` int(11 ...
- MyBatis中foreach元素用法解析
(尊重劳动成果,转载请注明出处:http://blog.csdn.net/qq_25827845/article/details/70946761冷血之心的博客) 动态SQL是MyB ...
- mybatis的selectKey的用法
Mybatis 示例之 SelectKey SelectKey在Mybatis中是为了解决Insert数据时不支持主键自动生成的问题,他可以很随意的设置生成主键的方式. 不管SelectKey有多好, ...
- MyBatis循环Map(高级用法)
MyBatis循环Map 今天遇到一个比较特殊的业务,需要对传入的Map数据在映射文件中进行遍历,在之前的学习中,我们也知道MyBatis有默认对集合的操作list 和 array ,但是没有默认的 ...
- Mybatis中Example的用法
Example简单介绍 1.example是Mybatis数据层框架中的一个工具,可以帮我们完成sql语句中where条件句的书写,相当于where后面的部分,我们可以根据不同的条件来查询和操作数据库 ...
最新文章
- 网友:Java岗,自学一个月跳槽计算机视觉,其实入门很简单
- 深度学习中将类别标签映射到one_hot向量
- 关于GitHub推送时发生Permission denied (publickey)的问题
- client高性能组件化框架React简单介绍、特点、环境搭建及经常使用语法
- 使用OpenCV创建视频
- python while循环if_详解python基础之while循环及if判断
- 一个类可以实现多个接口吗_java中接口的概念
- 从 C10K 到 C500K
- 蓝桥杯.历届试题: 核桃数量
- HDOJ(HDU) 1563 Find your present!(异或)
- Oracle查询执行计划
- Unity3D 脚本模板插件
- matlab双闭环绘图,matlab双闭环直流调速系统设计及仿真+电路图
- 推荐一个阅读代码、文档的利器:屏幕贴图工具
- pandas GUI 神器 D-Tale,可视化操作自动转代码
- 北漂小斌和你分享宋代建筑模数斗拱材分制的学习方法以及CAD案例图纸
- 韩寒做错了(update 4 12)。
- Peekaboo——代码规范、冲刺任务与计划
- 根据userAgent获取浏览器/操作系统/设备类型等信息
- turtle绘制皮卡丘
热门文章
- 基于JUNGO STACK的USB下载流程
- 算法学习(六)--股票最大利润
- 使用MATLAB 将EASE-Grid 2.0投影坐标系下 的NC文件转换为相同坐标系下的geotiff文件
- 五分钟精通正则表达式,如果没精通,那就再加两分钟
- 量化为技艺,策略为根本,相辅相成,运用之妙,存乎一心
- 惠普打印机墨盒更换教程_惠普彩色打印机如何换墨盒 惠普打印机墨盒更换方法【介绍】...
- 操作系统学习笔记——北京大学陈向群老师课后及习题答案(9)
- vmwaretools是灰色的 典型安装
- selenium获取京东前三页奶瓶信息
- 蓝桥杯校内模拟赛_C++组