Mybatis-Plus 条件构造器 QueryWrapper 的基本用法

 更新时间:2021年09月08日 08:27:44   作者:Maggieq8324  
这篇文章主要介绍了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);
    }

如上代码,通过条件构造器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

到此这篇关于Mybatis-Plus - 条件构造器 QueryWrapper 的使用的文章就介绍到这了,更多相关Mybatis-Plus 条件构造器内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 快速解决idea @Autowired报红线问题

    快速解决idea @Autowired报红线问题

    这篇文章主要介绍了快速解决idea @Autowired报红线问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-02-02
  • 关于weblogic部署Java项目的包冲突问题的解决

    关于weblogic部署Java项目的包冲突问题的解决

    这篇文章主要介绍了关于weblogic部署Java项目的包冲突问题的解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-01-01
  • 新手也能看懂的SpringBoot异步编程指南(简单易懂)

    新手也能看懂的SpringBoot异步编程指南(简单易懂)

    这篇文章主要介绍了新手也能看懂的SpringBoot异步编程指南(简单易懂),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-10-10
  • 如何解决Idea断点调试乱跳的问题

    如何解决Idea断点调试乱跳的问题

    这篇文章主要介绍了如何解决Idea断点调试乱跳的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-11-11
  • http协议进阶之Transfer-Encoding和HttpCore实现详解

    http协议进阶之Transfer-Encoding和HttpCore实现详解

    这篇文章主要给大家介绍了http协议之Transfer-Encoding和HttpCore实现的相关资料,文中介绍的非常详细,相信对大家具有一定的参考价值,需要的朋友们下面来一起看看吧。
    2017-04-04
  • Java获取json数组对象的实例讲解

    Java获取json数组对象的实例讲解

    下面小编就为大家分享一篇Java获取json数组对象的实例讲解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-03-03
  • SpringCloud Config分布式配置中心使用教程介绍

    SpringCloud Config分布式配置中心使用教程介绍

    springcloud config是一个解决分布式系统的配置管理方案。它包含了 client和server两个部分,server端提供配置文件的存储、以接口的形式将配置文件的内容提供出去,client端通过接口获取数据、并依据此数据初始化自己的应用
    2022-12-12
  • 微信小程序与Java后端接口交互

    微信小程序与Java后端接口交互

    本文主要介绍了微信小程序与Java后端接口交互,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-07-07
  • Spring gateway配置Spring Security实现统一权限验证与授权示例源码

    Spring gateway配置Spring Security实现统一权限验证与授权示例源码

    这篇文章主要介绍了Spring gateway配置Spring Security实现统一权限验证与授权,本文通过示例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-07-07
  • Spring详细讲解@Autowired注解

    Spring详细讲解@Autowired注解

    @Autowired注解可以用在类属性,构造函数,setter方法和函数参数上,该注解可以准确地控制bean在何处如何自动装配的过程。在默认情况下,该注解是类型驱动的注入
    2022-06-06

最新评论