前言

记录下Mybatis-Plus中条件构造器Wrapper 的一些基本用法。

查询示例

  • 表结构
  1. CREATE TABLE `product` (
  2. `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  3. `title` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
  4. `create_time` datetime DEFAULT CURRENT_TIMESTAMP,
  5. PRIMARY KEY (`id`)
  6. ) ENGINE=InnoDB AUTO_INCREMENT=35 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
  7. CREATE TABLE `product_item` (
  8. `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  9. `title` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
  10. `product_id` int(10) unsigned NOT NULL,
  11. `create_time` datetime DEFAULT CURRENT_TIMESTAMP,
  12. PRIMARY KEY (`id`)
  13. ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
  • 实现需求:根据product - id查询product实例及其关联的product_item,如下:

基础代码

  • ProductController.java
  1. @GetMapping("/{id}")
  2. public ProductWithItemsVo getWithItems(@PathVariable Integer id) {
  3. return productService.getWithItems(id);
  4. }
  • ProductService.java
  1. public interface ProductService {
  2. ProductWithItemsVo getWithItems(Integer id);
  3. }
  • ProductServiceImpl.java
  1. @Service
  2. public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {
  3. @Autowired
  4. private ProductItemMapper productItemMapper;
  5. @Override
  6. public ProductWithItemsVo getWithItems(Integer id) {
  7. // 实现代码
  8. }
  9. }
  • mapper
  1. @Repository
  2. public interface ProductMapper extends BaseMapper<Product> {
  3. }
  4. @Repository
  5. public interface ProductItemMapper extends BaseMapper<ProductItem> {
  6. }
  • model
  1. @Getter
  2. @Setter
  3. @TableName("product")
  4. public class Product {
  5. private Integer id;
  6. private String title;
  7. @JsonIgnore
  8. private Date createTime;
  9. }
  10. @Getter
  11. @Setter
  12. @TableName("product_item")
  13. public class ProductItem {
  14. private Integer id;
  15. private Integer productId;
  16. private String title;
  17. @JsonIgnore
  18. private Date createTime;
  19. }
  • vo出参
  1. @Data
  2. @NoArgsConstructor
  3. public class ProductWithItemsVo {
  4. private Integer id;
  5. private String title;
  6. List<ProductItem> items;
  7. /**
  8. * 构造ProductWithItemsVo对象用于出参
  9. * @param product
  10. * @param items
  11. */
  12. public ProductWithItemsVo(Product product, List<ProductItem> items) {
  13. BeanUtils.copyProperties(product, this);
  14. this.setItems(items);
  15. }
  16. }

QueryWrapper 的基本使用

  1. @Override
  2. public ProductWithItemsVo getWithItems(Integer id) {
  3. Product product = this.getById(id);
  4. if (Objects.isNull(product)) {
  5. System.out.println("未查询到product");
  6. return null;
  7. }
  8. /**
  9. * wrapper.eq("banner_id", id)
  10. * banner_id 数据库字段
  11. * id 判断相等的值
  12. */
  13. QueryWrapper<ProductItem> wrapper = new QueryWrapper<>();
  14. wrapper.eq("product_id", id);
  15. List<ProductItem> productItems = productItemMapper.selectList(wrapper);
  16. return new ProductWithItemsVo(product, productItems);
  17. }
  • 如上代码,通过条件构造器QueryWrapper查询出当前product实例及其关联的product_item

QueryWrapper 的lambada写法

  1. @Override
  2. public ProductWithItemsVo getWithItems(Integer id) {
  3. Product product = this.getById(id);
  4. if (Objects.isNull(product)) {
  5. System.out.println("未查询到product");
  6. return null;
  7. }
  8. QueryWrapper<ProductItem> wrapper = new QueryWrapper<>();
  9. /**
  10. * lambda方法引用
  11. */
  12. wrapper.lambda().eq(ProductItem::getProductId, id);
  13. List<ProductItem> productItems = productItemMapper.selectList(wrapper);
  14. return new ProductWithItemsVo(product, productItems);
  15. }
  • 如上代码,通过条件构造器QueryWrapperlambda方法引用查询出当前product实例及其关联的product_item

LambadaQueryWrapper 的使用

  • LambadaQueryWrapper 用于Lambda语法使用的QueryWrapper
  • 构建LambadaQueryWrapper 的方式:
  1. /**
  2. * 方式一
  3. */
  4. LambdaQueryWrapper<ProductItem> wrapper1 = new QueryWrapper<ProductItem>().lambda();
  5. wrapper1.eq(ProductItem::getProductId, id);
  6. List<ProductItem> productItems1 = productItemMapper.selectList(wrapper1);
  7. /**
  8. * 方式二
  9. */
  10. LambdaQueryWrapper<ProductItem> wrapper2 = new LambdaQueryWrapper<>();
  11. wrapper2.eq(ProductItem::getProductId, id);
  12. List<ProductItem> productItems2 = productItemMapper.selectList(wrapper2);
  • 完整代码
  1. @Override
  2. public ProductWithItemsVo getWithItems(Integer id) {
  3. Product product = this.getById(id);
  4. if (Objects.isNull(product)) {
  5. System.out.println("未查询到product");
  6. return null;
  7. }
  8. LambdaQueryWrapper<ProductItem> wrapper = new LambdaQueryWrapper<>();
  9. wrapper.eq(ProductItem::getProductId, id);
  10. List<ProductItem> productItems = productItemMapper.selectList(wrapper);
  11. return new ProductWithItemsVo(product, productItems);
  12. }
  • 如上代码,通过条件构造器LambdaQueryWrapper查询出当前product实例及其关联的product_item

LambdaQueryChainWrapper 的链式调用

  1. @Override
  2. public ProductWithItemsVo getWithItems(Integer id) {
  3. Product product = this.getById(id);
  4. if (Objects.isNull(product)) {
  5. System.out.println("未查询到product");
  6. return null;
  7. }
  8. /**
  9. * 链式调用
  10. */
  11. List<ProductItem> productItems =
  12. new LambdaQueryChainWrapper<>(productItemMapper)
  13. .eq(ProductItem::getProductId, id)
  14. .list();
  15. return new ProductWithItemsVo(product, productItems);
  16. }
  • 如上代码,通过链式调用查询出当前product实例及其关联的product_item

- End -



梦想是咸鱼
关注一下吧

Mybatis-Plus - 条件构造器 QueryWrapper 的使用的更多相关文章

  1. MyBatis:条件构造器QueryWrapper方法详解

    QueryWrapper 说明:      继承自 AbstractWrapper ,自身的内部属性 entity 也用于生成 where 条件及 LambdaQueryWrapper, 可以通过 n ...

  2. mybatis plus的条件构造器

    我们在使用条件构造器的时候要使用QueryWrapper或者UpdateWrapper来充当条件语句来进行构造 QueryWrapper(LambdaQueryWrapper) 和 UpdateWra ...

  3. MyBatis:MyBatis-Plus条件构造器EntityWrapper

    EntityWrapper 简介 1. MybatisPlus 通过 EntityWrapper(简称 EW,MybatisPlus 封装的一个查询条件构造器)或者 Condition(与 EW 类似 ...

  4. 小书MybatisPlus第2篇-条件构造器的应用及总结

    一.条件构造器Wrapper Mybatis Plus为我们提供了如下的一些条件构造器,我们可以利用它们实现查询条件.删除条件.更新条件的构造. 条件构造器用于给如下的Mapper方法传参,通常情况下 ...

  5. MyBatisPlus性能分析插件,条件构造器,代码自动生成器详解

    性能分析插件 我们在平时的开发中,会遇到一些慢sql,测试,druid MP(MyBatisPlus)也提供性能分析插件,如果超过这个时间就停止 不过官方在3.2版本的时候取消了,原因如下 条件构造器 ...

  6. Mybatis-Plus 实战完整学习笔记(十一)------条件构造器删除,修改,conditon

    1.修改功能--其他过滤方式跟select一样 /** * 修改条件构造器 * @throws SQLException */ @Test public void selectUpdate() thr ...

  7. Mybatis-Plus 实战完整学习笔记(十)------条件构造器核心用法大全(下)

    31.升序orderByAsc 31.升序orderByAsc List<Employee> employeeList = employeeMapper.selectList(new Qu ...

  8. Mybatis-Plus 实战完整学习笔记(九)------条件构造器核心用法大全(上)

    一.Mybatisplus通用(公共方法)CRUD,一共17种(3.0.3版),2.3系列也是这么多,这个新版本一定程度进行了改造和删减. 二.构造器UML图(3.0.3)-----实体包装器,主要用 ...

  9. MybatisPlus学习(四)条件构造器Wrapper方法详解

    文章目录 1.条件构造器 2.QueryWrapper 2.1.eq.ne 2.2.gt.ge.lt.le 2.3.between.notBetween 2.4.like.notLike.likeLe ...

随机推荐

  1. (java4)什么是计算机

    (java4)什么是计算机 computer : 全称电子计算机,俗称电脑 能够按照程序运行.自动.高速处理海量数据的现代化智能电子设备 由硬件和软件组成 常见的由台式计算机,笔记本计算机,大型计算机 ...

  2. jvm源码解读--05 常量池 常量项的解析JVM_CONSTANT_Utf8

    当index=18的时候JVM_CONSTANT_Utf8 case JVM_CONSTANT_Utf8 : { cfs->guarantee_more(2, CHECK); // utf8_l ...

  3. SQL之case when then用法_之二

    select CustomerNo, Name, Sex, Birthday, IDType, IDNo, validityday, case (null ) when '1' then '高级VIP ...

  4. 基于MD5+RSA算法实现接口调用防扯皮级鉴权

    概述 最近项目中需要对第三方开发接口调用,考虑了一下,准备采用MD5+RSA算对请求数据进行签名,来达到请求鉴权,过滤非法请求的目标. 数字签名采用MD5+RSA算法实现.RSA私钥要严格保密并提供安 ...

  5. <题解>[IOI2019]景点划分

    题目传送门(luogu) 题目传送门(loj) 这个题对我来说可以算是超出了我的能力范围 被学长拿来教我做构造,构造题真简单,构造题真是人,构造题真能手切... 首先对于本题,必须要知道dfs树这东西 ...

  6. ;~ 并发运行的AutoHotkey脚本真机实际测试模板参考20191010.ahk

    ;~ 并发运行的AutoHotkey脚本真机实际测试模板参考20191010.ahk;~ 2019年10月10日;~ 徐晓亮(aahk6188);~ 操作系统测试环境: Windows 7 专业版 3 ...

  7. 单片机学习(五)LCD1602和矩阵键盘的使用

    目录 LCD1602的使用 矩阵键盘的使用 矩阵键盘相关电路图 按键检测扫描 制作密码输入器 LCD1602的使用 首先LCD1602是外接在开发板上的液晶屏外设,如图所示: 我们主要使用它来代替动态 ...

  8. Netty入门(三):EventLoop

    前言 Netty系列索引: 1.Netty入门(一):ByteBuf 2.Netty入门(二):Channel IO相关: 1.Java基础(一):I/O多路复用模型及Linux中的应用 上文提到,早 ...

  9. vue 源码详解(一):原型对象和全局 `API`的设计

    vue 源码详解(一):原型对象和全局 API的设计 1. 从 new Vue() 开始 我们在实际的项目中使用 Vue 的时候 , 一般都是在 main.js 中通过 new Vue({el : ' ...

  10. 得到、微信、美团、爱奇艺APP组件化架构实践

    一.背景 随着项目逐渐扩展,业务功能越来越多,代码量越来越多,开发人员数量也越来越多.此过程中,你是否有过以下烦恼? 项目模块多且复杂,编译一次要5分钟甚至10分钟?太慢不能忍? 改了一行代码 或只调 ...