Plus分页插件的配置和使用
配置类
package com. xxxx. mybatisplus. config; import com. baomidou. mybatisplus. annotation. DbType;
import com. baomidou. mybatisplus. extension. plugins. MybatisPlusInterceptor;
import com. baomidou. mybatisplus. extension. plugins. inner. PaginationInnerInterceptor;
import org. mybatis. spring. annotation. MapperScan;
import org. springframework. context. annotation. Bean;
import org. springframework. context. annotation. Configuration; @Configuration
@MapperScan ( "com.xxxx.mybatisplus.mapper" )
public class MyBatisPlusConfig { @Bean public MybatisPlusInterceptor mybatisPlusInterceptor ( ) { MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor ( ) ; interceptor. addInnerInterceptor ( new PaginationInnerInterceptor ( DbType. MYSQL) ) ; return interceptor; } }
测试
package com. xxxx. mybatisplus; import com. baomidou. mybatisplus. extension. plugins. pagination. Page;
import com. xxxx. mybatisplus. mapper. UserMapper;
import com. xxxx. mybatisplus. pojo. User;
import org. junit. jupiter. api. Test;
import org. springframework. beans. factory. annotation. Autowired;
import org. springframework. boot. test. context. SpringBootTest; @SpringBootTest
public class MyBatisPlusPluginsTest { @Autowired private UserMapper userMapper; @Test public void testPage ( ) { Page< User> page = new Page < > ( 1 , 3 ) ; userMapper. selectPage ( page, null) ; System. out. println ( page) ; } }
分页相关数据获取
package com. xxxx. mybatisplus; import com. baomidou. mybatisplus. extension. plugins. pagination. Page;
import com. xxxx. mybatisplus. mapper. UserMapper;
import com. xxxx. mybatisplus. pojo. User;
import org. junit. jupiter. api. Test;
import org. springframework. beans. factory. annotation. Autowired;
import org. springframework. boot. test. context. SpringBootTest; @SpringBootTest
public class MyBatisPlusPluginsTest { @Autowired private UserMapper userMapper; @Test public void testPage ( ) { Page< User> page = new Page < > ( 1 , 3 ) ; userMapper. selectPage ( page, null) ; System. out. println ( page. getRecords ( ) ) ; System. out. println ( page. getCurrent ( ) ) ; System. out. println ( page. getSize ( ) ) ; System. out. println ( page. getPages ( ) ) ; System. out. println ( page. getTotal ( ) ) ; System. out. println ( page. hasNext ( ) ) ; System. out. println ( page. hasPrevious ( ) ) ; } }
自定义分页功能
UserMapper
package com. xxxx. mybatisplus. mapper; import com. baomidou. mybatisplus. core. mapper. BaseMapper;
import com. baomidou. mybatisplus. extension. plugins. pagination. Page;
import com. xxxx. mybatisplus. pojo. User;
import org. apache. ibatis. annotations. Param;
import org. springframework. stereotype. Repository; import java. util. Map;
@Repository
public interface UserMapper extends BaseMapper < User> { Map< String, Object> selectMapById ( Long id) ; Page< User> selectPageVo ( @Param ( "page" ) Page< User> page, @Param ( "age" ) Integer age) ; }
yaml
UserMapper.xml
< select id = " selectPageVo" resultType = " User" > select uid,user_name,age,email from user where age > #{age}
</ select>
测试
@Test public void testPageVo ( ) { Page< User> page = new Page < > ( 1 , 3 ) ; userMapper. selectPageVo ( page, 20 ) ; System. out. println ( page. getRecords ( ) ) ; System. out. println ( page. getCurrent ( ) ) ; System. out. println ( page. getSize ( ) ) ; System. out. println ( page. getPages ( ) ) ; System. out. println ( page. getTotal ( ) ) ; System. out. println ( page. hasNext ( ) ) ; System. out. println ( page. hasPrevious ( ) ) ; }