广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Mybatis-Plus 条件构造器 QueryWrapper 的基本用法
  • 511
分享到

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

2024-04-02 19:04:59 511人浏览 独家记忆

Python 官方文档:入门教程 => 点击学习

摘要

前言 记录下mybatis-Plus中条件构造器Wrapper 的一些基本用法。 查询示例 表结构 CREATE TABLE `product` ( `id` int(11)

前言

记录下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;

	
    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;
    }

    
    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<>();
        
        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 条件构造器内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: Mybatis-Plus 条件构造器 QueryWrapper 的基本用法

本文链接: https://www.lsjlt.com/news/134969.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作