提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
前言
提示:这里可以添加本文要记录的大概内容:
本文主要针对 queryWrapper 包含内容 + 常用示例给大家做举例
提示:以下是本篇文章正文内容,下面案例可供参考
一、queryWrapper 手册?
queryWrapper.lt()——小于
queryWrapper.le()——小于等于
queryWrapper.gt()——大于
queryWrapper.ge()——大于等于
queryWrapper.eq()——等于
queryWrapper.ne()——不等于
queryWrapper.betweeen(“age”,10,20)——age在值10到20之间
queryWrapper.notBetweeen(“age”,10,20)——age不在值10到20之间
queryWrapper.like(“属性”,“值”)——模糊查询匹配值‘%值%’
queryWrapper.notLike(“属性”,“值”)——模糊查询不匹配值‘%值%’
queryWrapper.likeLeft(“属性”,“值”)——模糊查询匹配最后一位值‘%值’
queryWrapper.likeRight(“属性”,“值”)——模糊查询匹配第一位值‘值%’
queryWrapper.isNull()——值为空或null
queryWrapper.isNotNull()——值不为空或null
queryWrapper.in(“属性”,条件,条件 )——符合多个条件的值
queryWrapper.notIn(“属性”,条件,条件 )——不符合多个条件的值
queryWrapper.or()——或者
queryWrapper.and()——和
queryWrapper.orderByAsc(“属性”)——根据属性升序排序
queryWrapper.orderByDesc(“属性”)——根据属性降序排序
queryWrapper.inSql(“sql语句”)——符合sql语句的值
queryWrapper.notSql(“sql语句”)——不符合SQL语句的值
queryWrapper.esists(“SQL语句”)——查询符合SQL语句的值
queryWrapper.notEsists(“SQL语句”)——查询不符合SQL语句的值
二、关键字匹配多字段?
参数示例:keword = “121212”
mysql示例:
->and (Mobile= "121212" or CurrentCompany = "121212")
//关键字
if(ObjectUtil.isNotEmpty(getDocumentListRequest.getKeyword())) {
//名称 手机号 当前公司 当前职位
queryWrapper.and(
wrapper -> wrapper.like(Documents::getUname, getDocumentListRequest.getKeyword()).or()
.like(Documents::getMobile, getDocumentListRequest.getKeyword()).or()
.like(Documents::getCurrentCompany, getDocumentListRequest.getKeyword()).or()
.like(Documents::getCurrentPost, getDocumentListRequest.getKeyword())
);
}
三、字符匹配示例
参数示例:"大专,本科 ,研究生 "
Education : “1,2,3”
mysql示例:
->and (edu = "大专" or edu = "本科")
//学历 1:大专 2:本科 3:研究生 4:博士
if(ObjectUtil.isNotEmpty(getDocumentListRequest.getEducation())){
//获取当前城市下的所有子城市
List<String> edus = Arrays.asList(getDocumentListRequest.getEducation().split(","));
queryWrapper.and(
query->{
//这里自己可以替换根据自己的情况优化
for (String edu : edus){
switch (edu){
case "1":
edu = "大专";
break;
case "2":
edu = "本科";
break;
case "3":
edu = "研究生";
break;
case "4":
edu = "博士";
break;
default:
break;
}
String finalEdu = edu;
query.or(
querys->{
querys.like(Documents::getEducation, finalEdu);
}
);
}
}
);
}
四、【0,200)区间搜索示例
age年龄参数示例:
小于25:[0,25]
25-30:[25,30]
30-35:[30,35]
mysql示例:
->and ((age >= 0 and age <25) or(age >= 25 and age <30))
//年龄
if(ObjectUtil.isNotEmpty(getDocumentListRequest.getAge())){
queryWrapper.and(
query->{
for (AgeDto age : getDocumentListRequest.getAge()){
query.or(
querys->{
//大于等于最小年龄
querys.ge(Documents::getAge,age.getAgeMin());
//小于最大年龄
querys.lt(Documents::getAge,age.getAgeMax());
}
);
}
}
);
}
五、FIND_IN_SET 城市示例
参数示例 CityLis : [1,2,3,4]
mysql 示例:
FIND_IN_SET(id,"1,2,3,4")
不了解FIND_IN_SET可以自行百度取了解
//城市
if(ObjectUtil.isNotEmpty(getDocumentListRequest.getCityList())){
//获取当前城市下的所有子城市
List<City> cityListVo = iCityService.getCityList(getDocumentListRequest.getCityList());
List<Integer> cityIds = cityListVo.stream().map(City::getId).collect(Collectors.toList());
if(ObjectUtil.isNotEmpty(cityIds)){
queryWrapper.and(
query->{
for (Integer id : cityIds){
query.apply(id != null,"FIND_IN_SET ("+id+",expect_city)").or();
}
}
);
}
}
六、排序示例
mysql 示例 :
order by top,toptime,updatetime desc
示例
//排序 排序 置顶 - 置顶时间 - 更新时间
第一种方式:
queryWrapper.orderByDesc(Documents::getTop);
queryWrapper.orderByDesc(Documents::getTopTime);
queryWrapper.orderByDesc(Documents::getUpdateTime);
第二种方式:
queryWrapper.orderByDesc(Documents::getTop)orderByDesc(Documents::getTopTime)orderByDesc(Documents::getUpdateTime);
总结
以上总结了五种常见的情况,如果遇到其他语句问题,欢迎留言!