文章目录 1.说明 2.common-mybatis-plus-starter 1.目录结构 2.MybatisPLusAutoConfiguration.java 添加MyBatis Plus分页插件 3.PlusPageResult.java MyBatis Plus 分页对象的增强版 4.SunPlusPageHelper.java 分页工具类 3.EasyCode模板修改 4.common-mybatis-plus-starter-demo 分页代码说明 1.ExampleTableController.java 2.ExampleTableService.java 3.ExampleTableServiceImpl.java 4.ExampleTableMapper.java 5.ExampleTableMapper.xml
1.说明
对MyBatisPlus的分页插件进行扩展,并且实现一行调用分页 对EasyCode模板进行升级
2.common-mybatis-plus-starter
1.目录结构
2.MybatisPLusAutoConfiguration.java 添加MyBatis Plus分页插件
@Bean
@ConditionalOnMissingBean
public MybatisPlusInterceptor mybatisPlusInterceptor ( ) { log. info ( "MybatisPlusInterceptor.PaginationInnerInterceptor 注入成功!" ) ; MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor ( ) ; interceptor. addInnerInterceptor ( new PaginationInnerInterceptor ( DbType . MYSQL ) ) ; return interceptor;
}
3.PlusPageResult.java MyBatis Plus 分页对象的增强版
package cn. sunxiansheng. mybatis. plus. page. plus ; import com. baomidou. mybatisplus. core. metadata. IPage ;
import com. baomidou. mybatisplus. core. metadata. OrderItem ;
import lombok. * ; import java. util. ArrayList ;
import java. util. Collections ;
import java. util. List ;
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
public class PlusPageResult < T > implements IPage < T > { private static final long serialVersionUID = 1L ; private long current = 1 ; private long size = 10 ; private long total = 0 ; private List < T > records = new ArrayList < > ( ) ; private List < OrderItem > orders = new ArrayList < > ( ) ; private long start; private long end; private long totalPages; private boolean hasNextPage; private boolean hasPreviousPage; public PlusPageResult ( long current, long size, long total, List < T > records) { this . current = current; this . size = size; this . total = total; this . records = records != null ? records : Collections . emptyList ( ) ; calculateMetaData ( ) ; } @Override public List < T > getRecords ( ) { return records; } @Override public IPage < T > setRecords ( List < T > records) { this . records = records; return this ; } @Override public long getTotal ( ) { return total; } @Override public IPage < T > setTotal ( long total) { this . total = total; calculateMetaData ( ) ; return this ; } @Override public long getSize ( ) { return size; } @Override public IPage < T > setSize ( long size) { this . size = size; calculateMetaData ( ) ; return this ; } @Override public long getCurrent ( ) { return current; } @Override public IPage < T > setCurrent ( long current) { this . current = current; calculateMetaData ( ) ; return this ; } @Override public List < OrderItem > orders ( ) { return orders; } private void calculateMetaData ( ) { if ( size > 0 ) { this . totalPages = ( total / size) + ( total % size == 0 ? 0 : 1 ) ; } else { this . totalPages = 0 ; } if ( size > 0 ) { this . start = ( current - 1 ) * size + 1 ; this . end = Math . min ( current * size, total) ; } else { this . start = 1 ; this . end = total; } this . hasPreviousPage = current > 1 ; this . hasNextPage = current < totalPages; }
}
4.SunPlusPageHelper.java 分页工具类
package cn. sunxiansheng. mybatis. plus. page. plus ; import java. util. function. Function ;
public class SunPlusPageHelper { public static < T > PlusPageResult < T > paginate ( long current, long pageSize, Class < T > clazz, Function < PlusPageResult < T > , PlusPageResult < T > > function) { PlusPageResult < T > pageResult = ( PlusPageResult < T > ) new PlusPageResult < T > ( ) . setCurrent ( current) . setSize ( pageSize) ; return function. apply ( pageResult) ; }
}
3.EasyCode模板修改
4.common-mybatis-plus-starter-demo 分页代码说明
1.ExampleTableController.java
@GetMapping ( "/queryPage" )
public PlusPageResult < ExampleTableVo > queryPage ( @RequestBody ExampleTableReq exampleTableReq) { return exampleTableService. queryPage ( exampleTableReq) ;
}
2.ExampleTableService.java
package cn. sunxiansheng. mybatis. plus. service ; import cn. sunxiansheng. mybatis. plus. base. service. SunRaysBaseService ;
import cn. sunxiansheng. mybatis. plus. entity. po. ExampleTable ;
import cn. sunxiansheng. mybatis. plus. entity. req. ExampleTableReq ;
import cn. sunxiansheng. mybatis. plus. entity. vo. ExampleTableVo ;
import cn. sunxiansheng. mybatis. plus. page. plus. PlusPageResult ;
public interface ExampleTableService extends SunRaysBaseService < ExampleTable , java. lang. Integer> { PlusPageResult < ExampleTableVo > queryPage ( ExampleTableReq exampleTableReq) ;
}
3.ExampleTableServiceImpl.java
@Override
@Transactional ( rollbackFor = Exception . class )
public PlusPageResult < ExampleTableVo > queryPage ( ExampleTableReq exampleTableReq) { ExampleTable po = ExampleTableConverter . INSTANCE . convertReq2Po ( exampleTableReq) ; PlusPageResult < ExampleTable > poPageResult = SunPlusPageHelper . paginate ( exampleTableReq. getPageNo ( ) , exampleTableReq. getPageSize ( ) , ExampleTable . class , ( plusPageResult) -> { return exampleTableMapper. queryPage ( plusPageResult, po) ; } ) ; return ExampleTableConverter . INSTANCE . convertPoPage2VoPage ( poPageResult) ;
}
4.ExampleTableMapper.java
PlusPageResult < ExampleTable > queryPage ( @Param ( "plusPageResult" ) PlusPageResult < ExampleTable > plusPageResult, @Param ( "po" ) ExampleTable po) ;
5.ExampleTableMapper.xml
< ! -- MyBatisPlus 分页插件的分页查询 -- >
< select id= "queryPage" resultMap= "ExampleTableMap" > select id, user_name, user_email, phone_number, home_address, account_status, create_by, create_time, update_by, update_time, is_deletedfrom example_table< where> < if test= "po.id != null" > and id = #{ po. id} < / if > < if test= "po.userName != null and po.userName != ''" > and user_name = #{ po. userName} < / if > < if test= "po.userEmail != null and po.userEmail != ''" > and user_email = #{ po. userEmail} < / if > < if test= "po.phoneNumber != null and po.phoneNumber != ''" > and phone_number = #{ po. phoneNumber} < / if > < if test= "po.homeAddress != null and po.homeAddress != ''" > and home_address = #{ po. homeAddress} < / if > < if test= "po.accountStatus != null" > and account_status = #{ po. accountStatus} < / if > < if test= "po.createBy != null and po.createBy != ''" > and create_by = #{ po. createBy} < / if > < if test= "po.createTime != null" > and create_time = #{ po. createTime} < / if > < if test= "po.updateBy != null and po.updateBy != ''" > and update_by = #{ po. updateBy} < / if > < if test= "po.updateTime != null" > and update_time = #{ po. updateTime} < / if > < if test= "po.isDeleted != null" > and is_deleted = #{ po. isDeleted} < / if > and is_deleted = 0 < / where>
< / select>