Mybatis-Plus 条件构造器 QueryWrapper 的基本用法
作者:Maggieq8324 发布时间:2022-07-07 13:28:18
标签: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
来源:https://www.cnblogs.com/maggieq8324/p/15239402.html


猜你喜欢
- 一.Mybatis-Plus——sum聚合函数//总收益 Order order =new Orde
- 先贴代码,后面做一些简单说明:public static string sendPostHttpRequest_2(string url,
- 1 低层级 asyncio 索引低层级 API 索引¶ 列出所有低层级的 asyncio API。1.1 获取事件循环获取
- 今天PM提了个需求:用户退出当前网页时,只清除该网页访问的域名相关的cookie,保留其他域名的cookie。查了一下CookieManag
- 背景朋友想从XX超市app购买一些物美价廉的东西,但是因为人多货少经常都是缺货的状态,订阅了到货通知也没什么效果,每次收到短信通知进入app
- C#利用win32 Api 修改本地系统时间、获取硬盘序列号,可以用于软件注册机制的编写!using System;using System
- 这些bug可能够你喝一壶的。1、被断言(assert)包含的代码常发生在切换到release版本时,执行结果乖乖的,最终查找结果是asser
- 在C#中用同一个dataset保存从数据库中取出的多张表:cmd.CommandText = "select * from tab
- 我已经很精简了,两篇(Spring Boot启动过程(一)、spring Boot启动过程(二))依然没写完,接着来。refreshCont
- 在初始化自己位置的时候请求定位权限:Constants.ACCESS_FINE_LOCATION_COMMANDS_REQUEST_CODE
- 本文实例讲述了Android开发实现标题随scrollview滑动变色的方法。分享给大家供大家参考,具体如下:要实现某个view的背景透明度
- 基本原理:利用URLConnection获取要下载文件的长度、头部等相关信息,并设置响应的头部信息。并且通过URLConnection获取输
- Spring Cloud Gateway 默认的filter功能和执行顺序有效性Spring Cloud Gateway 2.0.0.REL
- Java中可以使用关键字synchronized进行线程同步控制,实现关键资源顺序访问,避免由于多线程并发执行导致的数据不一致性等问题。sy
- 实现官方文档说明:com.baomidou.mybatisplus.annotations.TableFieldTableField注解新增
- 一、前言随着 JDK 1.8 Streams API 的发布,使得 HashMap 拥有了更多的遍历的方式,但应该选择那种遍历方式?反而成了
- 本文实例为大家分享了C#实现QQ聊天窗口的具体代码,供大家参考,具体内容如下效果图:using System;using System.Co
- 配置准备在build.gradle文件中添加如下依赖: compile "org.elasticsearc
- spring在启动时会自己把bean(java组件)注册到ioc容器里,实现控制反转,在开发人员使用spring开发应用程序时,你是看不到n
- 1、spring原理内部最核心的就是IOC了,动态注入,让一个对象的创建不用new了,可以自动的生产,这其实就是利用java里的反射,反射其