MybatisPlus--QueryWrapper
QueryWrapper
wrapper介绍
- Wrapper : 条件构造抽象类,最顶端父类
- AbstractWrapper : 用于查询条件封装,生成 sql 的 where 条件
- QueryWrapper : Entity 对象封装操作类,不是用lambda语法
- UpdateWrapper : Update 条件封装,用于Entity对象更新操作
- AbstractLambdaWrapper : Lambda 语法使用 Wrapper统一处理解析 lambda 获取 column。
- LambdaQueryWrapper :看名称也能明白就是用于Lambda语法使用的查询Wrapper
- LambdaUpdateWrapper : Lambda 更新封装Wrapper
- AbstractWrapper : 用于查询条件封装,生成 sql 的 where 条件
- 如果想进行复杂条件查询,那么需要使用条件构造器 Wapper,涉及到如下方法
- 拼凑条件相关关键字
条件查询 - 基本多条件查询
@Testpublic void testQueryWrapper(){// todo 拼凑条件QueryWrapper queryWrapper = new QueryWrapper();// todo 等值查询queryWrapper.eq("password","1234");// todo 模糊查询queryWrapper.like("cname","o");// todo 范围查询 大于等于queryWrapper.ge("money",800);// todo 范围查询 小于等于queryWrapper.le("money",1000);//todo 条件查询List<Customer> list = customerMapper.selectList(queryWrapper);list.forEach(System.out::println);}
- 条件判断
@Testpublic void findCondition2() {Customer customer = new Customer();customer.setPassword("777");customer.setCname("888");customer.setIdList(Arrays.asList(2,3,4));customer.setCid(3);//条件查询QueryWrapper<Customer> queryWrapper = new QueryWrapper<>();// 1) 等值查询queryWrapper.eq( customer.getPassword()!=null ,"password", customer.getPassword());// 2) 模糊查询queryWrapper.like(customer.getCname() != null , "cname",customer.getCname());// 3) in语句queryWrapper.in(customer.getIdList() != null , "cid",customer.getIdList());// 4) 大于等于queryWrapper.ge(customer.getCid() != null , "cid" , customer.getCid());//查询List<Customer> list = customerMapper.selectList(queryWrapper);//list.forEach(customer-> System.out.println(customer));list.forEach(System.out::println);}
- 条件更新
@Testpublic void testUpdateByQueryMapper(){//1.todo 更新数据Customer customer = new Customer();customer.setVersion(1);//2.todo 更新条件UpdateWrapper<Customer> updateWrapper = new UpdateWrapper<>();updateWrapper.in("cid",1,2,3);//3.todo 更新int update = customerMapper.update(customer, updateWrapper);System.out.println(update);}
分页
主体插件: MybatisPlusInterceptor,该插件内部插件集:
- 分页插件: PaginationInnerInterceptor
- 多租户插件: TenantLineInnerInterceptor
- 动态表名插件: DynamicTableNameInnerInterceptor
- 乐观锁插件: OptimisticLockerInnerInterceptor
- sql性能规范插件: IllegalSQLInnerInterceptor
- 防止全表更新与删除插件: BlockAttackInnerInterceptor
配置类
package com.czxy.mp.config;import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class MybatisPlusConfig {/*** 配置插件* @return*/@Beanpublic MybatisPlusInterceptor mybatisPlusInterceptor(){MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();// 分页插件mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));return mybatisPlusInterceptor;}/*** 新的分页插件,一缓和二缓遵循mybatis的规则,需要设置 MybatisConfiguration#useDeprecatedExecutor = false 避免缓存出现问题(该属性会在旧插件移除后一同移除)* @return*/@Beanpublic ConfigurationCustomizer configurationCustomizer() {return configuration -> configuration.setUseDeprecatedExecutor(false);}
}
分页
@Testpublic void testPage(){// 分页数据int pageNum = 1;int pageSize = 3;Page<Customer> page = new Page<>(pageNum , pageSize);page.setSearchCount(true);// 查询customerMapper.selectPage(page, null);// 分页数据System.err.println("当前页码:" + page.getCurrent());System.err.println("每页显示记录数:" + page.getSize());System.err.println("总页数:" + page.getPages());System.err.println("总记录数:" + page.getTotal());System.err.println("是否有下一页:" + page.hasNext());System.err.println("是否有上一页:" + page.hasPrevious());// 分页数据列表page.getRecords().forEach(System.err::println);}
常见注解
表名注解:@TableName
主键注解:@TableId
字段注解(非主键) : @TableField
常见配置
mybatis-plus:configuration:log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #输出日志map-underscore-to-camel-case: true #驼峰命名global-config:db-config:id-type: auto #全局配置,id自动增强table-prefix: tmp_ #表名前缀type-aliases-package: com.czxy.mp.domain #别名包扫描路径mapper-locations: classpath*:/mapper/**/*.xml #映射文件位置
MybatisPlus--QueryWrapper相关推荐
- MybatisPlus QueryWrapper(简称 QW,MP 封装的一个查询条件构造器)的使用和简单认识
上一篇讲的是MybatisPlus的MP注解用法和简单介绍 传送门 (跟上一篇是同一个项目)先来创一个Springboot测试项目 创建一个数据库 -- 创建表 CREATE TABLE t_empl ...
- mybatis-plus QueryWrapper 添加limit
其实是在sql的最后加上 limit 相关的语句. QueryWrapper wrapper = new QueryWrapper<>(); wrapper.last("limi ...
- java自定义sql查询条件_mybatis-plus QueryWrapper自定义查询条件的实现
mybatis-plus框架功能很强大,把很多功能都集成了,比如自动生成代码结构,mybatis crud封装,分页,动态数据源等等,附上官网链接https://mp.baomidou.com/,gi ...
- wrapper 并集如何使用
https://blog.csdn.net/u011229848/article/details/81902398 MybatisPlus QueryWrapper and or 连用 cnsu-cm ...
- 关于MybatisPlus的QueryWrapper定义查询条件的and()和or()方法连用问题
MyBatis-Plus(简称 MP)是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发.提高效率而生.它提供了QueryWrapper自定义查询对象,可以无 ...
- Mybatis-plus中QueryWrapper的使用
一.QueryWrapper是什么? QueryWrapper就是在使用Mybatis-plus中真实用到的一种技术,也叫作构造器,能简化sql的操作. 二.常用方法总结 1.单表操作 代码如下(示例 ...
- MybatisPlus的使用和Wrapper(QueryWrapper and UpdateWrapper)
一.MyBatis-Plus 1.简介 MyBatis-Plus 是一个 Mybatis 增强版工具,在 MyBatis 上扩充了其他功能没有改变其基本功能,为了简化开发提交效率而存在. 官网文档地址 ...
- mybatis-plus 将查询参数 进行封装 QueryWrapper条件的实现另一种方式
通过java反射机制获取并进行封装操作QueryWrapper 之前用的一种 封装的方式(见QueryWrapper自定义查询),那现在通过另一种方法来处理前端伟来的参数进行封装QueryWrappe ...
- mybatis-plus 中 queryWrapper and与or嵌套
and ( ... or ... or...) 格式 queryWrapper.and(wrapper -> wrapper.like("project_name", key ...
- 【Mybatisplus】创建spring boot工程QueryWrapper的使用
目录 QueryWrapper 组装查询条件(查询用户名包含"仙",年龄在30-40之间,邮箱信息不为空信息) 组装排序条件(查询用户信息,先按照年龄的降序排序,若年龄相同,则按照 ...
最新文章
- Centos7上安装Nginx两种方法
- 再见了,Windows AutoRun!
- linnux 流量控制模块tc_智能功率模块助力业界加速迈向基于碳化硅(SiC)的电动汽车...
- CentOS下yum操作gnome和kde桌面
- superoneclick 2.2_一季度食品监督抽检2.2%不合格:农兽药残留超标等系主因
- Go中error类型的nil值和nil
- 最全数据指标体系集合!覆盖9个行业4个业务场景,全是干货
- SpringBoot整合Shiro(Java安全框架)案例(含源码)
- 关于Net Core 多平台程序的Framework问题
- matlab批量储存变量_Matlab批量赋值
- QQ在线客服代码 网页qq咨询html代码
- 【lizhi125】比Nero更好用的免费小巧的光盘刻录软件——ImgBurn(中文版)
- dstat 命令详解
- 财务会计科目与预算会计科目关联对照表
- Latex图表设置中英文双标题(非ccaption宏包)
- win10 C盘满 清理终极大法
- 网页回拨(客服)的弊端
- 用WIN7装oracle10g的步骤
- 艺术画笔见乾坤—Matplotlib
- 百度被黑了,百度被真相曝光
热门文章
- Python批量计算Pearson相关系数和RMSE
- 【Redis 大量点赞问题】
- 中国女排最新一期集训大名单
- Linux下如何切换用户
- 千文详解:十分钟带你深入了解华为交换机的通信原理
- oracle账户密码忘记的解决办法(亲测可行)
- 《淘宝店铺设计装修一册通》一2.1 Photoshop界面
- eink 主题 android,einklauncher下载-E Ink Launcher 安卓版v0.1.8.3-PC6安卓网
- VIjos 晴天小猪历险记之Number (搜索+链表hash)
- 十行Python代码写一个聊天机器人