前言

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

查询示例

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

基础代码

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

QueryWrapper 的基本使用

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

QueryWrapper 的lambada写法

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

LambadaQueryWrapper 的使用

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

LambdaQueryChainWrapper 的链式调用

@Override
public ProductWithItemsVo getWithItems(Integer id) {
Product product = this.getById(id);
if (Objects.isNull(product)) {
System.out.println("未查询到product");
return null;
} /**
* 链式调用
*/
List<ProductItem> productItems =
new LambdaQueryChainWrapper<>(productItemMapper)
.eq(ProductItem::getProductId, id)
.list(); return new ProductWithItemsVo(product, productItems);
}
  • 如上代码,通过链式调用查询出当前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. ASP.NET中Textbox后的必填验证控件RequiredFieldValidator的使用方法。

    制作效果如下: 实现方法: 1. 拖动RequiredFieldValidator控件到相应的textbox后位置,点击属性面板,输入ErroMessage相应信息,更改ForeColor为红色 设置 ...

  2. iTop安装 vm虚拟机、Linux、centos7安装itop 2.6.1

    itop安装流程,是我基于下面两位博主发布的文章整理出来的,欢迎大家学习,如有错误之处请大家留言指出我看到之后及时更新.谢谢 https://blog.csdn.net/qq_23565543/art ...

  3. Linux命令(八)之安装Jdk、Tomcat

    .personSunflowerP { background: rgba(51, 153, 0, 0.66); border-bottom: 1px solid rgba(0, 102, 0, 1); ...

  4. 【SpringBoot技术专题】「JWT技术专区」SpringSecurity整合JWT授权和认证实现

    JWT基本概念 JWT,即 JSON Web Tokens(RFC 7519),是一个广泛用于验证 REST APIs 的标准.虽说是一个新兴技术,但它却得以迅速流行. JWT的验证过程是: 前端(客 ...

  5. 常见的嵌入式linux学习和如何选择ARM芯片问答

    常见的ARM嵌入式学习问答,设计者和学习者最关心的11个问题: 1.          ARM嵌入式是学习硬件好还是学习软件好? 2.          嵌入式软件和硬件,哪一种职位待遇更高?或者说, ...

  6. Notes about WindowPadX

    WindowPadX乃一Autohotkey脚本,具有强大的单/多显示器窗口排布能力且易于配置.有了它,那些Pro版收费的.需要安装的DisplayFusion, MultiMon TaskBar, ...

  7. Golang语言系列-04-运算符

    运算符 Go语言内置的运算符有 算术运算符 关系运算符 逻辑运算符 位运算符 赋值运算符 算术运算符 package main import "fmt" func main() { ...

  8. SQL注入的那些面试题总结

    一.知识储备类 1.SQL与NoSQL的区别? SQL:关系型数据库 NoSQL:非关系型数据库 存储方式:SQL具有特定的结构表,NoSQL存储方式灵活 性能:NoSQL较优于SQL 数据类型:SQ ...

  9. Go的Channel发送和接收

    先来看一道面试题: 对已经关闭的 chan 进行读写,会怎么样?为什么? 在上一篇学习 Go 协程的文章中,知道 go 关键字可以用来开启一个 goroutine 进行任务处理,但多个任务之间如果需要 ...

  10. NOIP 模拟 $33\; \rm Hunter$

    题解 \(by\;zj\varphi\) 结论题. 对于 \(1\) 猎人,他死的期望就是有多少个死在它前面. 那么对于一个猎人,它死在 \(1\) 前的概率就是 \(\frac{w_i}{w_i+w ...