官网地址
Mybatis Plus
条件构造器LambdaQueryWrapper
目前数据库数据情况,User表
id | user_name | birthday | sex | address |
---|---|---|---|---|
1 | 张1 | 2023-08-10 | 男 | 123@163.com |
2 | 李1 | 2023-08-10 | 女 | 222@163.com |
3 | 张2 | 2023-08-10 | 女 | 999@163.com |
4 | 张3 | 2023-08-10 | 男 | 9994@qq.com |
如何使用各种场景
方法 | 说明 | 案例 |
---|---|---|
eq | 等值操作 | lambdaQueryWrapper.eq(UserDomain::getUserName,"张1"); |
allEq | 全等于 | Map<SFunction<UserDomain,?>,Object> sFunctionObjectMap = new HashMap(16); sFunctionObjectMap.put(UserDomain::getId,1); sFunctionObjectMap.put(UserDomain::getUserName,"张1"); lambdaQueryWrapper.allEq(sFunctionObjectMap); |
ne | 不等于 | lambdaQueryWrapper.ne(UserDomain::getUserName,null); |
gt | 大于 | lambdaQueryWrapper.gt(UserDomain::getId,1); |
ge | 大于等于 | lambdaQueryWrapper.ge(UserDomain::getId,2); |
lt | 小于 | lambdaQueryWrapper.lt(UserDomain::getId,2); |
le | 小于等于 | lambdaQueryWrapper.le(UserDomain::getId,2); |
between | 在。。之间 | lambdaQueryWrapper.between(UserDomain::getId,1,2); |
notBetween | 不在。。之间 | lambdaQueryWrapper.notBetween(UserDomain::getId,1,2); |
like | 模糊查询 | lambdaQueryWrapper.like(UserDomain::getUserName,"张"); |
likeRight | 右模糊 | lambdaQueryWrapper.likeRight(UserDomain::getUserName,"1"); |
likeLeft | 左模糊 | lambdaQueryWrapper.likeLeft(UserDomain::getUserName,"张"); |
isNull | 为空判断 | lambdaQueryWrapper.isNull(UserDomain::getUserName); |
isNotNull | 不为空判断 | lambdaQueryWrapper.isNotNull(UserDomain::getUserName); |
in | 在。。之内 | lambdaQueryWrapper.in(UserDomain::getId,1,2); |
notIn | 不在。。之内 | lambdaQueryWrapper.notIn(UserDomain::getId,1,2); |
groupBy | 分组 | lambdaQueryWrapper.select(UserDomain::getSex).groupBy(UserDomain::getSex); |
orderBy | 排序 | lambdaQueryWrapper.orderBy(true,true,UserDomain::getId,UserDomain::getBirthday); |
等值操作(eq,allEq)
LambdaQueryWrapper<UserDomain> lambdaQueryWrapper = new QueryWrapper<UserDomain>().lambda();
log.info("等值【eq】《=》user_name = '张1' ");
lambdaQueryWrapper.eq(UserDomain::getUserName,"张1");
List<UserDomain> eqList = userDao.selectList(lambdaQueryWrapper);
log.info("eqList : [{}]" , eqList);log.info("多字段等值【allEq】 (user_name = '张1' AND id = 1) ");
Map<SFunction<UserDomain,?>,Object> sFunctionObjectMap = new HashMap(16);
sFunctionObjectMap.put(UserDomain::getId,1);
sFunctionObjectMap.put(UserDomain::getUserName,"张1");
lambdaQueryWrapper.allEq(sFunctionObjectMap);
List<UserDomain> sFunctionMapList = userDao.selectList(lambdaQueryWrapper);
log.info("sFunctionMapList : [{}]" , sFunctionMapList);
SELECT id,user_name,birthday,sex,address FROM user WHERE (user_name = ‘张1’)
SELECT id,user_name,birthday,sex,address FROM user WHERE (id = 1 AND user_name = ‘张1’)
模糊查询(like,likeLeft,likeRight)
LambdaQueryWrapper<UserDomain> lambdaQueryWrapper = new QueryWrapper<UserDomain>().lambda();
lambdaQueryWrapper.like(UserDomain::getUserName,"张");
List<UserDomain> likeList = userDao.selectList(lambdaQueryWrapper);
log.info("likeList : [{}]" , likeList);
SELECT id,user_name,birthday,sex,address FROM user WHERE (user_name LIKE ‘%张%’)
LambdaQueryWrapper<UserDomain> lambdaQueryWrapper = new QueryWrapper<UserDomain>().lambda();
lambdaQueryWrapper.likeLeft(UserDomain::getUserName,"张");
List<UserDomain> leftLikeList = userDao.selectList(lambdaQueryWrapper);
log.info("leftLikeList : [{}]" , leftLikeList);
SELECT id,user_name,birthday,sex,address FROM user WHERE (user_name LIKE ‘%张’)
LambdaQueryWrapper<UserDomain> lambdaQueryWrapper = new QueryWrapper<UserDomain>().lambda();
lambdaQueryWrapper.likeRight(UserDomain::getUserName,"1");
List<UserDomain> rightLikeList = userDao.selectList(lambdaQueryWrapper);
log.info("rightLikeList : [{}]" , rightLikeList);
SELECT id,user_name,birthday,sex,address FROM user WHERE (user_name LIKE ‘1%’)
查询某几个字段(select)
log.info("查询某几个字段 《=》user_name,address");
lambdaQueryWrapper.select(UserDomain::getUserName,UserDomain::getAddress);
List<UserDomain> multipleColumnList = userDao.selectList(lambdaQueryWrapper);
log.info("multipleColumnList : [{}]" , multipleColumnList);
SELECT user_name,address FROM user
大于,大于等于,小于,小于等于
log.info("小于等于 【gt】id>2");
lambdaQueryWrapper.gt(UserDomain::getId,1);
List<UserDomain> gtList = userDao.selectList(lambdaQueryWrapper);
log.info("gtList : [{}]" , gtList);
SELECT id,user_name,birthday,sex,address FROM user WHERE (id > 1)
log.info("小于等于 【ge】id>=2");
lambdaQueryWrapper.ge(UserDomain::getId,2);
List<UserDomain> geList = userDao.selectList(lambdaQueryWrapper);
SELECT id,user_name,birthday,sex,address FROM user WHERE (id >= 2)
log.info("小于等于 【lt】id<2");
lambdaQueryWrapper.lt(UserDomain::getId,2);
List<UserDomain> ltList = userDao.selectList(lambdaQueryWrapper);
SELECT id,user_name,birthday,sex,address FROM user WHERE (id < 2)
log.info("小于等于 【le】《=》id <= 2");
lambdaQueryWrapper.le(UserDomain::getId,2);
List<UserDomain> leList = userDao.selectList(lambdaQueryWrapper);
SELECT id,user_name,birthday,sex,address FROM user WHERE (id <= 2)
between,notBetween
log.info("在。。。之间【between】《=》(id BETWEEN 1 AND 2)");
lambdaQueryWrapper.between(UserDomain::getId,1,2);
List<UserDomain> betweenList = userDao.selectList(lambdaQueryWrapper);
log.info("betweenList : [{}]" , betweenList);
SELECT id,user_name,birthday,sex,address FROM user WHERE (id BETWEEN 1 AND 2)
log.info("在。。。之间【between】《=》(id NOT BETWEEN 1 AND 2)");
lambdaQueryWrapper.notBetween(UserDomain::getId,1,2);
List<UserDomain> notBetweenList = userDao.selectList(lambdaQueryWrapper);
log.info("notBetweenList : [{}]" , notBetweenList);
SELECT id,user_name,birthday,sex,address FROM user WHERE (id NOT BETWEEN 1 AND 2)
非空处理(isNull,isNotNull)
log.info("为空处理:【isNull】《=》user_name IS NULL");
lambdaQueryWrapper.isNull(UserDomain::getUserName);
List<UserDomain> isNullList = userDao.selectList(lambdaQueryWrapper);
log.info("isNullList : [{}]" , isNullList);
SELECT id,user_name,birthday,sex,address FROM user WHERE (user_name IS NULL)
log.info("不为空处理:【isNotNull】《=》user_name IS NOT NULL");
lambdaQueryWrapper.isNotNull(UserDomain::getUserName);
List<UserDomain> isNotNullList = userDao.selectList(lambdaQueryWrapper);
log.info("isNotNullList : [{}]" , isNotNullList);
SELECT id,user_name,birthday,sex,address FROM user WHERE (user_name IS NOT NULL)
in,notIn
log.info("在。。之内:【in】《=》id in (1,2)");
lambdaQueryWrapper.in(UserDomain::getId, 1, 2);
List<UserDomain> inList = userDao.selectList(lambdaQueryWrapper);
log.info("inList : [{}]", inList);
SELECT id,user_name,birthday,sex,address FROM user WHERE (id IN (1,2))
log.info("不在。。之内:【in】《=》id not in (1,2)");
lambdaQueryWrapper.notIn(UserDomain::getId,1,2);
List<UserDomain> notInlist = userDao.selectList(lambdaQueryWrapper);
log.info("notInlist : [{}]" , notInlist);
SELECT id,user_name,birthday,sex,address FROM user WHERE (id NOT IN (1,2))
排序
log.info("排序:【order by 】《=》order by id desc");
lambdaQueryWrapper.orderByDesc(UserDomain::getId);
List<UserDomain> orderByDescList = userDao.selectList(lambdaQueryWrapper);
log.info("orderByDescList : [{}]" , orderByDescList);
SELECT id,user_name,birthday,sex,address FROM user order by id desc
log.info("排序:【order by 】《=》order by id desc,birthday desc");
lambdaQueryWrapper.orderBy(true,false,UserDomain::getId,UserDomain::getBirthday);
List<UserDomain> orderByDescList = userDao.selectList(lambdaQueryWrapper);
log.info("orderByDescList : [{}]" , orderByDescList);
SELECT id,user_name,birthday,sex,address FROM user ORDER BY id DESC,birthday DESC
分组
log.info("分组:【groupBy】《=》group by sex ");
lambdaQueryWrapper.select(UserDomain::getSex).groupBy(UserDomain::getSex);
List<UserDomain> groupByList = userDao.selectList(lambdaQueryWrapper);
log.info("groupByList : [{}]" , groupByList);
SELECT sex FROM user GROUP BY sex