The basic use of about Mybatis plus not go into details.
Getting venue: https://mybatis.plus/guide/
I just record it "Custom SQL + + multi-table query results page" in the case, how to use
Environment: SpringBoot 2.1 + Mybatis Plus 3.1.0
We need to configure mybatis plus pager
@Configuration
public class MyBatisPlusConfig {
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
}
Custom Sql write directly on the Mapper interface methods used @Select comment
This relates to the Sql table 3, the result is a combination of two fields from the table, I have here created a common Java object (i.e. Product) received.
@Select("<script>"
+ " select tp.*,tpt.name as typeName "
+ " from jfcloud_product tp left join jfcloud_product_type_ref ref on tp.id=ref.product_id"
+ " left join jfcloud_product_type tpt on ref.type_id = tpt.id"
+ " where 1=1"
+ " and tp.is_delete=0"
+ " <if test=\"q.typeId != null\">and tpt.id = #{q.typeId} </if>"
+ " order by tp.create_time desc"
+ "</script>")
IPage<Product> getProductPage(IPage<Product> page,@Param("q") ProductQuery query);
parameter list:
page is paged objects mybatis plus offer, which the generic entity filled my custom multi-table this field.
query is custom entity, which is the query parameters to be used, an alias is defined by @Param "q", in Sql may be used to # {q.xxx}
Two parameters in accordance with the order of the best I above this, tracked at the source, will throw an exception if the parameter exchange position, mybatis plus data into making return
Specific use:
ProductQuery query = new ProductQuery();
query.setTypeId(1l);
Page<Product> page = new Page<>(1,10);
IPage<Product> datas = productMapper.getProductPage(page,query);
System.out.println("总记录:"+datas.getTotal());
System.out.println("总页数:"+datas.getPages());
System.out.println("数据:"+datas.getRecords());