前言
记录下Mybatis-Plus
中条件构造器Wrapper
的一些基本用法。
查询示例
表结构
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
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
1
2
3
4
|
@GetMapping ( "/{id}" ) public ProductWithItemsVo getWithItems( @PathVariable Integer id) { return productService.getWithItems(id); } |
ProductService.java
1
2
3
|
public interface ProductService { ProductWithItemsVo getWithItems(Integer id); } |
ProductServiceImpl.java
1
2
3
4
5
6
7
8
9
10
|
@Service public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService { @Autowired private ProductItemMapper productItemMapper; @Override public ProductWithItemsVo getWithItems(Integer id) { // 实现代码 } } |
mapper
1
2
3
4
5
6
7
8
9
|
@Repository public interface ProductMapper extends BaseMapper<Product> { } @Repository public interface ProductItemMapper extends BaseMapper<ProductItem> { } |
model
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
@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出参
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
@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 的基本使用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
@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写法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
@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
的方式:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
/** * 方式一 */ 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); |
完整代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
@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 的链式调用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
@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 条件构造器内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://www.cnblogs.com/maggieq8324/p/15239402.html