MyBatis:条件构造器QueryWrapper方法详解
2021/9/21 23:10:41
本文主要是介绍MyBatis:条件构造器QueryWrapper方法详解,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
目录
- QueryWrapper
- 1. eq、ne
- 2. gt、ge、lt、le
- 3. between、notBetween
- 4. like、notLike、likeLeft、likeRight
- 5. isNull、isNotNull
- 6. in、notIn
- 7. or、and
- 8. orderByAsc、orderByDesc
- 9. inSql、notInSql (不常用)
- 10. exists、notExists (不常用)
正文
回到顶部QueryWrapper
说明:
继承自 AbstractWrapper ,自身的内部属性 entity 也用于生成 where 条件
及 LambdaQueryWrapper, 可以通过 new QueryWrapper().lambda() 方法获取.
测试用表:
回到顶部1. eq、ne
说明:
eq:等于,ne:不等于
测试:
@Test public void contextLoads(){ QueryWrapper<Employee> wrapper = new QueryWrapper<>(); //eq() 等于 wrapper.eq("last_name", "皮皮虾"); Employee one = employeeService.getOne(wrapper); System.out.println(one); }
@Test public void contextLoads(){ QueryWrapper<Employee> wrapper = new QueryWrapper<>(); //ne() 不等于 wrapper.ne("gender", 0); List<Employee> list = employeeService.list(wrapper); System.out.println(list); }回到顶部
2. gt、ge、lt、le
说明:
gt:大于,ge:大于等于,lt:小于,le:小于等于
测试:
@Test public void contextLoads(){ QueryWrapper<Employee> gtWrapper = new QueryWrapper<>(); //gt() 大于 gtWrapper.gt("age", 27); List<Employee> gtList = employeeService.list(gtWrapper); System.out.println(gtList); //********************** QueryWrapper<Employee> geWrapper = new QueryWrapper<>(); //ge() 大于等于 geWrapper.ge("age", 26); List<Employee> geList = employeeService.list(geWrapper); System.out.println(geList); }
@Test public void contextLoads(){ QueryWrapper<Employee> ltWrapper = new QueryWrapper<>(); //lt() 小于 ltWrapper.lt("age", 12); List<Employee> ltList = employeeService.list(ltWrapper); System.out.println(ltList); //********************** QueryWrapper<Employee> geWrapper = new QueryWrapper<>(); //le() 小于等于 leWrapper.le("age", 12); List<Employee> leList = employeeService.list(leWrapper); System.out.println(leList); }回到顶部
3. between、notBetween
说明:
between:在值1和值2之间,notBetween:不在值1和值2之间
测试:
@Test public void contextLoads(){ QueryWrapper<Employee> betweenWrapper = new QueryWrapper<>(); //between() 区间的值 betweenWrapper.between("age", 10, 20); List<Employee> betweenList = employeeService.list(betweenWrapper); System.out.println(betweenList ); //********************** QueryWrapper<Employee> notBetweenWrapper = new QueryWrapper<>(); //notBetween() 不在区间的值 notBetweenWrapper.notBetween("age", 10, 21); List<Employee> notBetweenList = employeeService.list(notBetweenWrapper ); System.out.println(notBetweenList ); }回到顶部
4. like、notLike、likeLeft、likeRight
说明:
like:’%值%’,notLike:’%值%’,likeLeft:’%值’,likeRight:'值%'
测试:
@Test public void contextLoads(){ QueryWrapper<Employee> likeWrapper = new QueryWrapper<>(); //like() 模糊查询匹配值 likeWrapper.like("last_name", "s"); List<Employee> likeList = employeeService.list(likeWrapper); System.out.println(likeList ); //********************** QueryWrapper<Employee> notLikeWrapper = new QueryWrapper<>(); //notLike() 模糊查询不匹配值 notLikeWrapper.notLike("last_name", "s"); List<Employee> notLikeList = employeeService.list(notLikeWrapper); System.out.println(notLikeList); }
@Test public void contextLoads(){ QueryWrapper<Employee> likeLeftWrapper = new QueryWrapper<>(); //likeLeft() 模糊查询匹配最后一位值 likeLeftWrapper.likeLeft("last_name", "s"); List<Employee> likeLeftList = employeeService.list(likeLeftWrapper); System.out.println(likeLeftList ); //********************** QueryWrapper<Employee> likeRightWrapper = new QueryWrapper<>(); //likeRight() 模糊查询匹配第一位值 likeRightWrapper.likeRight("last_name", "s"); List<Employee> likeRightList = employeeService.list(likeRightWrapper ); System.out.println(likeRightList ); }回到顶部
5. isNull、isNotNull
说明:
isNull:字段 IS NULL,isNotNull:字段 IS NOT NULL
测试:
@Test public void contextLoads(){ QueryWrapper<Employee> isNullWrapper = new QueryWrapper<>(); //isNull() 为空 isNullWrapper.isNull("email"); List<Employee> isNullList = employeeService.list(isNullWrapper); System.out.println(isNullList ); //********************** QueryWrapper<Employee> isNotNullWrapper = new QueryWrapper<>(); //isNotNull() 不为空 isNotNullWrapper.isNotNull("last_name", "s"); List<Employee> isNotNullList = employeeService.list(isNotNullWrapper ); System.out.println(isNotNullList ); }回到顶部
6. in、notIn
说明:
in:字段 IN (v0, v1, …),notIn:字段 NOT IN (value.get(0), value.get(1), …)
测试:
@Test public void contextLoads(){ QueryWrapper<Employee> inWrapper = new QueryWrapper<>(); //in() 符合多个条件的值 inWrapper.in("age", 8, 16, 26); List<Employee> inList = employeeService.list(inWrapper); System.out.println(inList ); //********************** QueryWrapper<Employee> notInWrapper= new QueryWrapper<>(); //notIn() 不符合多个条件的值 notInWrapper.notIn("age", 8, 16, 26); List<Employee> notInList = employeeService.list(notInWrapper); System.out.println(notInList ); }回到顶部
7. or、and
说明:
or:或者 ,and:和
注意事项:
主动调用or表示紧接着下一个方法不是用and连接!(不调用or则默认为使用and连接)
测试:
@Test public void contextLoads(){ QueryWrapper<Employee> orWrapper = new QueryWrapper<>(); //or() 查询age大于20 或者 gender等于1 的数据 orWrapper.gt("age", 20).or().eq("gender", 1); List<Employee> orList = employeeService.list(orWrapper); System.out.println(orList ); }
@Test public void contextLoads(){ QueryWrapper<Employee> andWrapper = new QueryWrapper<>(); //and() 查询last_name等于皮皮虾b 和 gender等于1 的数据 (不使用and() 默认就是and()) andWrapper.eq("last_name", "皮皮虾b").eq("gender", 1); List<Employee> andList = employeeService.list(andWrapper ); System.out.println(andList); }回到顶部
8. orderByAsc、orderByDesc
说明:
orderByAsc:升序:ORDER BY 字段, … ASC,orderByDesc:降序:ORDER BY 字段, … DESC
测试:
@Test public void contextLoads(){ QueryWrapper<Employee> Wrapper = new QueryWrapper<>(); //orderByAsc() 升序排序 Wrapper.orderByAsc("id"); List<Employee> list = employeeService.list(Wrapper); for(Employee e : list) { System.out.println(e); } }
@Test public void contextLoads(){ QueryWrapper<Employee> Wrapper = new QueryWrapper<>(); //orderByDesc() 降序排序 Wrapper.orderByDesc("id"); List<Employee> list = employeeService.list(Wrapper); for(Employee e : list) { System.out.println(e); } }回到顶部
9. inSql、notInSql (不常用)
说明:
inSql:字段 IN ( sql语句 ),notInSql:字段 NOT IN ( sql语句 )
测试:
@Test public void contextLoads(){ QueryWrapper<Employee> inSqlWrapper = new QueryWrapper<>(); //inSql() 符合sql语句的值 inSqlWrapper .inSql("select id from employee where id < 10"); List<Employee> inSqlList = employeeService.list(inSqlWrapper ); System.out.println(inSqlList ); //********************** QueryWrapper<Employee> notInSqlWrapper= new QueryWrapper<>(); //notInSql() 不符合sql语句的值 notInSqlWrapper.notInSql("select id from employee where id < 10"); List<Employee> notInSqlList = employeeService.list(notInSqlWrapper); System.out.println(notInSqlList ); }回到顶部
10. exists、notExists (不常用)
说明:
exists:拼接 EXISTS ( sql语句 ),notExists:拼接 NOT EXISTS ( sql语句 )
测试:
@Test public void contextLoads(){ QueryWrapper<Employee> existsWrapper = new QueryWrapper<>(); //exists() 查询符合sql语句的值 existsWrapper.exists("select last_name,gender from employee where id = 1"); List<Employee> existsList = employeeService.list(existsWrapper); System.out.println(existsList ); //********************** QueryWrapper<Employee> notExistsWrapper = new QueryWrapper<>(); //notExists() 查询不符合sql语句的值 notExistsWrapper.notExists("select last_name,gender from employee where id = 1"); List<Employee> notExistsList = employeeService.list(notExistsWrapper); System.out.println(notExistsList ); }
文章转载至:https://blog.csdn.net/llllllkkkkkooooo/article/details/108216957
这篇关于MyBatis:条件构造器QueryWrapper方法详解的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-26Mybatis官方生成器资料详解与应用教程
- 2024-11-26Mybatis一级缓存资料详解与实战教程
- 2024-11-26Mybatis一级缓存资料详解:新手快速入门
- 2024-11-26SpringBoot3+JDK17搭建后端资料详尽教程
- 2024-11-26Springboot单体架构搭建资料:新手入门教程
- 2024-11-26Springboot单体架构搭建资料详解与实战教程
- 2024-11-26Springboot框架资料:新手入门教程
- 2024-11-26Springboot企业级开发资料入门教程
- 2024-11-26SpringBoot企业级开发资料详解与实战教程
- 2024-11-26Springboot微服务资料:新手入门全攻略