Mybatis-Plus - 条件构造器 QueryWrapper 的使用
前言
记录下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);
}
- 如上代码,通过条件构造器
QueryWrapper
的lambda
方法引用查询出当前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 的使用的更多相关文章
- MyBatis:条件构造器QueryWrapper方法详解
QueryWrapper 说明: 继承自 AbstractWrapper ,自身的内部属性 entity 也用于生成 where 条件及 LambdaQueryWrapper, 可以通过 n ...
- mybatis plus的条件构造器
我们在使用条件构造器的时候要使用QueryWrapper或者UpdateWrapper来充当条件语句来进行构造 QueryWrapper(LambdaQueryWrapper) 和 UpdateWra ...
- MyBatis:MyBatis-Plus条件构造器EntityWrapper
EntityWrapper 简介 1. MybatisPlus 通过 EntityWrapper(简称 EW,MybatisPlus 封装的一个查询条件构造器)或者 Condition(与 EW 类似 ...
- 小书MybatisPlus第2篇-条件构造器的应用及总结
一.条件构造器Wrapper Mybatis Plus为我们提供了如下的一些条件构造器,我们可以利用它们实现查询条件.删除条件.更新条件的构造. 条件构造器用于给如下的Mapper方法传参,通常情况下 ...
- MyBatisPlus性能分析插件,条件构造器,代码自动生成器详解
性能分析插件 我们在平时的开发中,会遇到一些慢sql,测试,druid MP(MyBatisPlus)也提供性能分析插件,如果超过这个时间就停止 不过官方在3.2版本的时候取消了,原因如下 条件构造器 ...
- Mybatis-Plus 实战完整学习笔记(十一)------条件构造器删除,修改,conditon
1.修改功能--其他过滤方式跟select一样 /** * 修改条件构造器 * @throws SQLException */ @Test public void selectUpdate() thr ...
- Mybatis-Plus 实战完整学习笔记(十)------条件构造器核心用法大全(下)
31.升序orderByAsc 31.升序orderByAsc List<Employee> employeeList = employeeMapper.selectList(new Qu ...
- Mybatis-Plus 实战完整学习笔记(九)------条件构造器核心用法大全(上)
一.Mybatisplus通用(公共方法)CRUD,一共17种(3.0.3版),2.3系列也是这么多,这个新版本一定程度进行了改造和删减. 二.构造器UML图(3.0.3)-----实体包装器,主要用 ...
- MybatisPlus学习(四)条件构造器Wrapper方法详解
文章目录 1.条件构造器 2.QueryWrapper 2.1.eq.ne 2.2.gt.ge.lt.le 2.3.between.notBetween 2.4.like.notLike.likeLe ...
随机推荐
- python3实现名片管理系统(文件版)
def menu(): #首先定义功能列表函数menu() print(" 名片管理系统 V1.0 ") print("1:增加新用户") print(&quo ...
- 3.python编程与计算机的关系,如何执行python文件
上一节预告了这一章想讲如何不停地和世界打招呼,这涉及到编程中一个重要的概念:循环. 但经过了两周断更后细想了一下,不行,我们得对上一章进行补充,而且这个补充非常关键!也印证了上一章所说的: 上一节章很 ...
- 一张图带你搞懂Javascript原型链关系
在某天,我听了一个老师的公开课,一张图搞懂了原型链. 老师花两天时间理解.整理的,他讲了两个小时我们当时就听懂了. 今天我把他整理出来,分享给大家.也让我自己巩固加深一下. 就是这张图: 为了更好的图 ...
- Java面向对象02——回顾方法(定义、调用)及加深
方法的定义 package oop.demon01; //Demon01 类 public class Demon01 { //main 方法 public stati ...
- MSF+Nmap TCP空闲扫描
MSF+Nmap TCP空闲扫描 前言 TCP空闲扫描是一种高级的扫描技术,可以冒充内网中另一台IP地址来对内网中的目标进行隐秘的扫描. 正文 在进行扫描之前,我们需要了解一个概念,即递增IP帧标识, ...
- windows的自动登录和隐藏用户
Launch Regedit. #r -> regedit 1. Navigate to: HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\Cu ...
- 最详细之教你Jenkins+github自动化部署.Net Core程序到Docker
环境 centos7.9,.NET5 一.Jenkins搭建 1)下载Jenkins的war包 在\home目录建一个jenkins目录放jenkins的包 #进入\home目录 cd \home # ...
- 【原创】利用“进程注入”实现无文件不死webshell
引子 上周末,一个好兄弟找我说一个很重要的目标shell丢了,这个shell之前是通过一个S2代码执行的漏洞拿到的,现在漏洞还在,不过web目录全部不可写,问我有没有办法搞个webshell继续做内网 ...
- 【Vulnhub】 DC-4 靶机
Vulnhub DC-4 一.环境搭建 下载链接:https://www.vulnhub.com/entry/dc-4,313/ 解压后用VMware打开,导入虚拟机 网卡配置看个人习惯,我喜欢NAT ...
- Typora+Markdown便捷发布blog
参考文章:https://www.cnblogs.com/Heroge/p/12459762.html 需要下载Typora和dotnet Typora下载链接:https://www.typora. ...