QueryWrapper

wrapper介绍

  • Wrapper : 条件构造抽象类,最顶端父类

    • AbstractWrapper : 用于查询条件封装,生成 sql 的 where 条件

      • QueryWrapper : Entity 对象封装操作类,不是用lambda语法
      • UpdateWrapper : Update 条件封装,用于Entity对象更新操作
      • AbstractLambdaWrapper : Lambda 语法使用 Wrapper统一处理解析 lambda 获取 column。
        • LambdaQueryWrapper :看名称也能明白就是用于Lambda语法使用的查询Wrapper
        • LambdaUpdateWrapper : Lambda 更新封装Wrapper
  • 如果想进行复杂条件查询,那么需要使用条件构造器 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相关推荐

  1. MybatisPlus QueryWrapper(简称 QW,MP 封装的一个查询条件构造器)的使用和简单认识

    上一篇讲的是MybatisPlus的MP注解用法和简单介绍 传送门 (跟上一篇是同一个项目)先来创一个Springboot测试项目 创建一个数据库 -- 创建表 CREATE TABLE t_empl ...

  2. mybatis-plus QueryWrapper 添加limit

    其实是在sql的最后加上 limit 相关的语句. QueryWrapper wrapper = new QueryWrapper<>(); wrapper.last("limi ...

  3. java自定义sql查询条件_mybatis-plus QueryWrapper自定义查询条件的实现

    mybatis-plus框架功能很强大,把很多功能都集成了,比如自动生成代码结构,mybatis crud封装,分页,动态数据源等等,附上官网链接https://mp.baomidou.com/,gi ...

  4. wrapper 并集如何使用

    https://blog.csdn.net/u011229848/article/details/81902398 MybatisPlus QueryWrapper and or 连用 cnsu-cm ...

  5. 关于MybatisPlus的QueryWrapper定义查询条件的and()和or()方法连用问题

    MyBatis-Plus(简称 MP)是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发.提高效率而生.它提供了QueryWrapper自定义查询对象,可以无 ...

  6. Mybatis-plus中QueryWrapper的使用

    一.QueryWrapper是什么? QueryWrapper就是在使用Mybatis-plus中真实用到的一种技术,也叫作构造器,能简化sql的操作. 二.常用方法总结 1.单表操作 代码如下(示例 ...

  7. MybatisPlus的使用和Wrapper(QueryWrapper and UpdateWrapper)

    一.MyBatis-Plus 1.简介 MyBatis-Plus 是一个 Mybatis 增强版工具,在 MyBatis 上扩充了其他功能没有改变其基本功能,为了简化开发提交效率而存在. 官网文档地址 ...

  8. mybatis-plus 将查询参数 进行封装 QueryWrapper条件的实现另一种方式

    通过java反射机制获取并进行封装操作QueryWrapper 之前用的一种 封装的方式(见QueryWrapper自定义查询),那现在通过另一种方法来处理前端伟来的参数进行封装QueryWrappe ...

  9. mybatis-plus 中 queryWrapper and与or嵌套

    and ( ... or ... or...) 格式 queryWrapper.and(wrapper -> wrapper.like("project_name", key ...

  10. 【Mybatisplus】创建spring boot工程QueryWrapper的使用

    目录 QueryWrapper 组装查询条件(查询用户名包含"仙",年龄在30-40之间,邮箱信息不为空信息) 组装排序条件(查询用户信息,先按照年龄的降序排序,若年龄相同,则按照 ...

最新文章

  1. tga文件怎么打开_六安软件定制文件打开的时候乱码怎么解决?
  2. java 气泡_Java气泡提示功能实现
  3. SAP Revenue Cloud业务综述
  4. 第四十期:九个对Web开发者最有用的Python包,掌握这些,工资至少能涨涨
  5. 【CodeForces - 227C】Flying Saucer Segments (思维)
  6. 拜登公布价值1.8万亿美元“美国家庭计划”
  7. Golang math/rand 源码剖析避坑指南
  8. 《推荐系统实践》协同过滤算法源代码
  9. 玩转你的AlphaGo(MAC OS)
  10. IPtables 扩展模块 Multiport/IPRange
  11. 制作QQ背景音乐链接
  12. iptables匹配statistic
  13. 【第一组】第八次冲刺例会纪要
  14. 【积水成渊-逐步定制自己的Emacs神器】5:回到最初,重新开始
  15. 破解帐户后,黑客重现如何榨干用户的最终价值
  16. Ubuntu18.04.5-server网络配置介绍
  17. FPGA学习—串口通信
  18. 页面嵌入 微软播放器-播放控件
  19. PCB焊盘、过孔、走线、去耦技术
  20. WebRTC -- Windows平台编译

热门文章

  1. 混沌工程之ChaosMesh使用之模拟POD网络延迟
  2. linux切换用户su自动输入密码,Linux切换用户出现【su:命令鉴定故障】问题修改的密码位数必须大于8位的问题...
  3. 基于LLVM13 Enzyme 安装
  4. Command ‘cnpm‘ not found解决方法
  5. 油烟净化器不能正常启动?别着急,四种方法教你排除故障!
  6. LayaAir已支持OPPO小游戏适配与IDE内一键发布!
  7. Windchill 业务笔记
  8. 信息安全体系建设☞通过防火墙实现内部网络的微隔离
  9. 大厂面试打起12万分小心?3轮技术面过,你也可能挂在HR手上!
  10. String详解(intern、StringBuffer、StringBuilder)