Mybatis-Plus - 条件构造器 QueryWrapper 的使用
2021-09-07 17:43
1331 查看
目录
查询示例
- End -
﹀
﹀
﹀
梦想是咸鱼
关注一下吧
前言
记录下
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); }
56c 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; /** * ad0 构造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(wrappe ad8 r2);
- 完整代码
@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自定义sql使用条件构造器和分页查询
- Mybatis plus之条件构造器及无法使用lambda做自定义sql查询问题
- springboot + mybatis plus强大的条件构造器queryWrapper、updateWrapper
- Mybatis-plus 3.0条件构造器的使用
- springboot + mybatis plus强大的条件构造器queryWrapper、updateWrapper
- springboot + mybatis plus强大的条件构造器queryWrapper、updateWrapper
- MyBatis-Plus简单使用——条件构造器(5)
- MyBatisPlus之模糊查询加分页和条件构造器
- Mybatis——plus 条件构造器(EntityWrapper)
- MyBatisPlus条件构造器
- Mybatis-Plus中的条件构造器
- mybatis-plus使用QueryWrapper方法说明
- SpringBoot中的MybatisPlus的使用——入门
- 使用mybatis-plus生成Controller,Service,mapper并运行
- MybatisPlus 使用方法
- springboot使用-Mybatis-Plus 的自动生成代码功能
- MyBatis-plus在eclipse中的使用详解
- mybatis中SQL块使用/动态条件查询
- mybatis 的like多条件模糊查询使用
- 优雅高效的MyBatis-Plus工具快速入门使用