MybatisPlus中and和or的使用
需求
最近自己玩发现MyBatisPlus还是挺好用的,但是忽然发现对于一个持久层框架来说支持拼接复杂的SQL也是一个优势,对一个持久层框架拼接SQL来说,or比and更难拼,所以此处用案例来实现MybatisPlus中or和and的简单使用。
代码下载(内含数据库)
ChaiRongD/Demooo - Gitee.com
and和or的使用
案例1:AandB
@GetMapping("/AandB")public Object AandB(){//SELECT id,name,age,sex FROM student WHERE (name = ? AND age = ?)List<Student> list = studentService.lambdaQuery().eq(Student::getName, "1").eq(Student::getAge, 1).list();return list;}
案例2:AorB
@GetMapping("/AorB")public Object AorB(){//SELECT id,name,age,sex FROM student WHERE (name = ? OR age = ?)List<Student> list = studentService.lambdaQuery().eq(Student::getName, "1").or().eq(Student::getAge, 12).list();return list;}
案例3:A or(C and D)
@GetMapping("/A_or_CandD")public Object A_or_CandD() {//SELECT id,name,age,sex FROM student WHERE (name = ? OR (name = ? AND age = ?)) List<Student> list =studentService.lambdaQuery().eq(Student::getName, "1").or(wp -> wp.eq(Student::getName, "1").eq(Student::getAge, 12)).list();return list;
案例4:(AandB)or(CandD)
@GetMapping("/AandB_or_CandD")public Object AandB_or_CandD() {// SELECT id,name,age,sex FROM student WHERE ((name = ? AND age = ?) OR (name = ? AND age = ?)) List<Student> list =studentService.lambdaQuery().and(wp -> wp.eq(Student::getName, "1").eq(Student::getAge, 12)).or(wp -> wp.eq(Student::getName, "1").eq(Student::getAge, 12)).list();return list;}
案例5:A or (B and ( C or D))
@GetMapping("/complex")public Object complex() {// SELECT * FROM student WHERE ((name <> 1) OR (name = 1 AND (age IS NULL OR age >= 11)))List<Student> list =studentService.lambdaQuery().and(wp -> wp.ne(Student::getName, "1")).or(wp ->wp.eq(Student::getName, "1").and(wpp -> wpp.isNull(Student::getAge).or().ge(Student::getAge, 11))).list();return list;}
总结
1 你可以让他打印SQL语句,这样你就知道知道的SQL了
2 我遇到的情况是不报错,不打印SQL,那只能DEBUG
3 手写SQL在mapper中也行
参考
springboot整合MybatisPlus_CBeann的博客-CSDN博客
MyBatis-Plus代码自动生成工具_CBeann的博客-CSDN博客_mybatisplus代码生成工具
MyBatis-Plus整合Spring Demo_CBeann的博客-CSDN博客_mybatis-plus spring
留言
如果有不会的SQL,可以留言,帮您解决问题,也顺便丰富文章的内容
MybatisPlus中and和or的使用相关推荐
- MyBatisPlus中开启了逻辑删除则更新逻辑字段不再管用
场景 MyBatisPlus中全局Sql注入器应用_逻辑删除使用MyBatisPlus中全局Sql注入器应用_逻辑删除使用: https://blog.csdn.net/BADAO_LIUMANG_Q ...
- MyBatisPlus中全局Sql注入器应用_逻辑删除使用
场景 项目搭建专栏: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/column/info/37194 MyBatisPlus中自定义全局操作流程: https: ...
- MybatisPlus中@TableField注解的使用
场景 项目搭建专栏: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/column/info/37194 基础搭建: https://blog.csdn.net/B ...
- mybatisPlus中的field-strategy(字段更新插入策略):null值插入和更新问题
目录 mybatisPlus中null值插入和更新问题 实际项目解决方法示例一 实际项目解决方法示例二 field-strategy字段更新插入策略介绍 枚举类FieldStrategy源码 枚举类字 ...
- mybatisPlus中getOne方法如何只取其中一条数据(Wrapper有多条数据时)
mybatis-plus中getOne方法只能取一条数据,如果取得多条数据会报错,要么换其他方法,要么只选择其中一条,在条件构造器Wrapper结尾加上.last("limit 1" ...
- Mybatis-plus中QueryWrapper的使用
一.QueryWrapper是什么? QueryWrapper就是在使用Mybatis-plus中真实用到的一种技术,也叫作构造器,能简化sql的操作. 二.常用方法总结 1.单表操作 代码如下(示例 ...
- Mybatis-Plus中的查(select)和 Wrapper条件构造器-详解 V2.0
V1.0 已更新为 V-2.0 传送门→ (V-2.0)Mybatis-Plus中的查(select)和QueryWrapper条件构造器 前言 本文介绍 mybatis-plus 中 ...
- MyBatis-Plus中分页插件IPage的使用
MyBatis-Plus中分页插件IPage的使用 使用步骤: 1.服务层的接口需要继承 IService<实体类> ,定义分页查询方法,其返回值类型是 IPage<实体类> ...
- MybatisPlus中的select操作
查询操作: 1.根据id查询一条记录 @Test void testSelectById() {// 查询单个用户,只根据一个id查询UserEntity userEntity = userMappe ...
最新文章
- PHP和java比较
- error LNK1104: 无法打开文件“ComService.lib”
- php cms word导入,phpcms V9文章编辑器不能按word文档复制过来的段头缩进显示
- Java8 - 使用工厂方法 supplyAsync创建 CompletableFuture
- Linux centos7安装Git及 Jenkins配置Git
- 《机器学习》 周志华学习笔记第六章 支持向量机(课后习题)python 实现
- linux top 命令的结果
- 配置Web.Config连接数据库
- java8函数式编程闭包_java8入门必备—函数式编程思维——函数式语言向语言和运行时让渡控制权的途径——迭代让位于高阶函数、闭包...
- SLAM学习笔记-------------(12)建图
- linux修复硬盘文件命令,fsck命令_Linux fsck 命令用法详解:检查并且试图修复文件系统中的错误...
- KL散度 JS散度 熵
- C++ VS2017 编译调用 gflags
- 数字签名 —— 哈希 + 私钥加密
- 大牛云集!清华大学2019年姚班及智班第一届AI本科生名单公布!
- php后缀加swp,当编辑文件出现swp的时候如何处理
- 户外蓝牙小音箱值得买
- 人脸和身份证不匹配_人脸识别身份信息不匹配是什么意思
- 2月14日机构龙虎榜和知名游资操作情况
- MySQL 8.0 全文检索功能 根据中文字符检索相关数据