目录
MyBatis-Plus (opens new window)(简称 MP)是一个 MyBatis (opens new window)的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
mybatis-plues需要的依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.5.1</version>
</dependency>
注册分页组件
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
@Configuration
public class MybatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor(){
// 注册拦截器
MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
// 配置分页拦截器
mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return mybatisPlusInterceptor;
}
}
分页的VO接收page、limit
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ProductPageVO {
private Integer limit;
private Integer page;
private String memberId; // 非必接收
@ApiParam(value = "分类id,非必填")
private String cateId; // 非必接收
}
创建VO接收总条数和数据
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.List;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class PageDataVO {
/**
* 查到的数据条数
*/
private Integer count;
/**
* 获取的数据
*/
private List list;
}
service的核心代码,查询数据。
/**
* 用户商品发布列表
*/
@Override
public PageDataVO memberProduct(ProductPageVO productPageVO) {
// 设置分页参数
Page<RfAddonShopProduct> page = new Page<>(productPageVO.getPage(),productPageVO.getLimit());
// 查询分页数据封装到page中
rfAddonShopProductMapper.selectPage(page, new QueryWrapper<RfAddonShopProduct>()
// 可选的条件查询,可要可不要
.eq("member_id",productPageVO.getMemberId()));
// 封装数据
return new PageDataVO((int)page.getTotal(), page.getRecords());
}
controller的代码,接收pageVO
@GetMapping("memberProductList")
@ApiOperation("用户发布的商品分页展示")
public ResponseVO<List> memberProductList(@ModelAttribute ProductPageVO productPageVO) {
try {
PageDataVO pageDataVO = rfAddonShopProductService.memberProduct(productPageVO);
if (pageDataVO == null){throw new Exception();}
return ResponseVO.getSuccessResponseVo(pageDataVO.getList());
} catch (Exception e) {
System.out.println(e.getMessage());
throw new BusinessException("用户发布的商品分页展示失败,请联系后台人员");
}
}
mp其他用法
eq(),第一个参数可以填判断条件,第二个填mysql字段,第三填入值
还可以使用between,同样可以加判断条件
public PageDataVO itemSearchCommodity(ItemSearchConditionDTO itemSearchConditionDTO) {
// 设置分页参数
Page<ItemCommodityInfo> page = new Page<>(itemSearchConditionDTO.getPage(),itemSearchConditionDTO.getLimit());
// 查询分页数据封装到page中
commodityInfoMapper.selectPage(page, new LambdaQueryWrapper<ItemCommodityInfo>()
// bid字符串不为空并且不为null,则执行条件
.eq(itemSearchConditionDTO.getBId()!=null && !"".equals(itemSearchConditionDTO.getBId()),ItemCommodityInfo::getBrandId,itemSearchConditionDTO.getBId())
.eq(itemSearchConditionDTO.getDegreeLoss() != null ,ItemCommodityInfo::getDegreeLoss,itemSearchConditionDTO.getDegreeLoss())
// 判断最大价格和最小价格是否为null,如果不为null则筛选两者区间价格
.between(itemSearchConditionDTO.getMaxPrice()!=null && itemSearchConditionDTO.getMinPrice() != null,ItemCommodityInfo::getPrice,itemSearchConditionDTO.getMinPrice(),itemSearchConditionDTO.getMaxPrice())
);
// 封装数据
return new PageDataVO((int)page.getTotal(), page.getRecords());
}
使用or语句,比如订单状态为1或者订单状态为0则满足,执行sql语句
public List<RfMemberAddress> getMemberAddressList(String memberId) {
return rfMemberAddressMapper.selectList(new LambdaQueryWrapper<RfMemberAddress>().
eq(RfMemberAddress::getMemberId, memberId)
.and(rf -> rf.eq(RfMemberAddress::getStatus, 1)
.or().eq(RfMemberAddress::getStatus, 0))
);
}