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) {
WithItems(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
isnull的用法
public class ProductWithItemsVo {
private Integer id;
private String title;
List<ProductItem> items;
/**
* 构造ProductWithItemsVo对象⽤于出参
* @param product
* @param items
*/
public ProductWithItemsVo(Product product, List<ProductItem> items) {        pyProperties(product, this);
this.setItems(items);
}
}
QueryWrapper 的基本使⽤
@Override
public ProductWithItemsVo getWithItems(Integer id) {
Product product = ById(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 = ById(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(wrapper2);
完整代码
@Override
public ProductWithItemsVo getWithItems(Integer id) {
Product product = ById(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 = ById(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 条件构造器内容请搜索以前的⽂章或继续浏览下⾯的相关⽂章希望⼤家以后多多⽀持!

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。