Mybatis-Plus - 條件構造器 QueryWrapper 的使用

Maggieq8324發表於2021-09-07

前言

記錄下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 的使用

相關文章