前言

记录下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. ThinkPHP5 SQL注入漏洞 && 敏感信息泄露

    访问看到用户名被显示了 http://192.168.49.2/index.php?ids[]=1&ids[]=2 访问http://your-ip/index.php?ids[0,updat ...

  2. MegEngine TensorCore 卷积算子实现原理

    作者:章晓 | 旷视 MegEngine 架构师 一.前言 2020 年 5 月 Nvidia 发布了新一代的 GPU 架构安培(Ampere).其中和深度学习关系最密切的莫过于性能强劲的第三代的 T ...

  3. MySQL隔离级别的实现

    虽然平时已经很少使用MySQL了,但是数据库作为基本技能仍然不能忘,最近在学习数据库隔离级别,在此写下个人理解以备复习. 大家都知道数据库事务ACID(原子性.一致性.隔离性和持久性)的四个特征,也知 ...

  4. Centos8 Nginx 开机自启配

    第一步:创建 service文件 vim /lib/systemd/system/nginx.service /lib 与 /usr/lib 我这里配置时是一样的,在那个文件夹配置都可以 第二步:编写 ...

  5. WarError syncing load balancer: failed to ensure load balancer: network.SubnetsClient#Get: Failure responding to request: StatusCode=403

    Warning SyncLoadBalancerFailed 4m55s (x8 over 15m) service-controller Error syncing load balancer: f ...

  6. ElasticSearch进阶检索

    ElasticSearch进阶检索 入门检索中讲了如何导入elastic提供的样本测试数据,下面我们用这些数据进一步检索 一.SearchAPI ES 支持两种基本方式检索 : 1.一种是通过使用 R ...

  7. 用 区间判断(if)来猜价格的高低

    1 #include <stdio.h> 2 #include <stdlib.h> 3 int main() 4 { 5 int price = 150; 6 int gue ...

  8. Springboot通过拦截器拦截请求信息收集到日志

    1.需求 最近在工作中遇到的一个需求,将请求中的客户端类型.操作系统类型.ip.port.请求方式.URI以及请求参数值收集到日志中,网上找资料说用拦截器拦截所有请求然后收集信息,于是就开始了操作: ...

  9. MobSF移动安全扫描平台本地化部署与简单汉化

    在之前的文章MobSF移动安全扫描平台环境搭建与试用中,我们用docker进行了搭建,那么我们如何在本地直接搭建呢,其实也是很简单的. 本地化部署 我们在本地安装 其实是很简单的,里面有两个文件,在不 ...

  10. dubbo学习实践(5)之Dubbo-Admin元数据中心配置(zookeeper&Redis&Consul)

    1.Dubbo2.7.8元数据中心配置zookeeper版 前面文章已经写到了dubbo-admin管理平台的docker版配置及dubbo服务注册与调用,这篇文章记录dubbo元数据中心配置 翻开d ...