MyBatis-Plus 多表联查+分页
在写东西的过程中,多表联查和分页功能必不可少。当然,crud也很重要,MyBatis-Plus一款国产的框架。优化了许多操作,但大部分文档都记录的是单表操作,多表join在很多场景用的很多,本次主要记录一下,多表联查和分页的使用。
Pom.xml <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.0.7.1</version> </dependency>
//MybatisPlusConfig
Spring boot方式
@Configuration @MapperScan("com.qy.site") public class MybatisPlusConfig { // 最新版 @Bean public MybatisPlusInterceptor mybatisPlusInterceptor() { MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.H2)); return interceptor; } }
mapper接口
public interface OrderExpressMapper extends BaseMapper<OrderExpressEntity> { Page<OrderExpressEntity> findAllAndPage(Page<OrderExpressEntity> page); }
mapper Xml
<select id="findAllAndPage" resultType="com.qy.site.modules.base.entity.OrderExpressEntity"> SELECT e.*,o.orderNo FROM yz_order_express e JOIN yz_orders o ON e.orderId=o.orderId </select>
controller
public String expressList(ModelMap model, HttpServletRequest request,int page,int limit){ Page<OrderExpressEntity> pageArt=new Page<OrderExpressEntity>(page,limit); Page<OrderExpressEntity> page1 = orderExpressMapper.findAllAndPage(pageArt); //自定义方法,多表 int total = (int)page1.getTotal(); List<OrderExpressEntity> orderList=page1.getRecords(); Map<String,Object> map=new HashMap<>(); map.put("list",orderList); map.put("total",total); System.out.println(map); model.addAttribute("orderList",orderList); return "admin/order/index"; }