文章目录
- 添加配置类
- 测试分页
- XML自定义分页
MyBatis Plus自带分页插件,只要简单的配置即可实现分页功能
添加配置类
package com.zyd.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.zyd.mapper")
public class MybatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
}
测试分页
@Test
public void selectPage(){
//分页参数
Page<User> pageParams = new Page<>(1,5);
//执行分页
userMapper.selectPage(pageParams,null);
//查看分页参数成员
System.out.println(pageParams);
}
XML自定义分页
Mapper 中
IPage<User> selectUserByPage(Page<?> page, Integer age);
对应Mapper.xml
<select id="selectUserByPage" resultType="com.zyd.entities.User">
SELECT <include refid="Base_Column_List"/> FROM user WHERE age > #{age}
</select>
测试
@Test
public void selectPageVo(){
Page<User> pageParam = new Page<>(1,5);
userMapper.selectUserByPage(pageParam,18);
List<User> users = pageParam.getRecords();
users.forEach(System.out::println);
}