springboot项目mybatis实现分页

mybatisPlus分页查询需要添加MybatisPlusConfig拦截器
 

@Configuration
public class MybatisPlusAutoConfiguration {

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor(List<InnerInterceptor> innerInterceptors) {
        MybatisPlusInterceptor  interceptor = new MybatisPlusInterceptor ();
        // 额外扩展的插件
        for (InnerInterceptor innerInterceptor : innerInterceptors) {
            interceptor.addInnerInterceptor(innerInterceptor);
        }
        // 分页插件
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        // 乐观锁插件
        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
        return interceptor;
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是SpringBootMyBatis实现分页查询的步骤: 1.在pom.xml文件中引入MyBatis和PageHelper的依赖包: ```xml <!-- 引入MyBatis依赖 --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.4</version> </dependency> <!-- 引入MyBatis 分页插件pageHelper --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.3</version> </dependency> ``` 2.在application.properties文件中配置PageHelper: ```properties # PageHelper配置 pagehelper.helperDialect=mysql pagehelper.reasonable=true pagehelper.supportMethodsArguments=true pagehelper.params=count=countSql ``` 3.在Mapper.xml文件中编写分页查询的SQL语句: ```xml <!-- 分页查询 --> <select id="selectByPage" resultType="com.example.demo.entity.User"> select * from user <where> <if test="name != null and name != ''"> and name like concat('%', #{name}, '%') </if> </where> order by id desc </select> ``` 4.在Service层中调用Mapper的分页查询方法: ```java @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public PageInfo<User> selectByPage(Integer pageNum, Integer pageSize, String name) { PageHelper.startPage(pageNum, pageSize); List<User> userList = userMapper.selectByPage(name); return new PageInfo<>(userList); } } ``` 5.在Controller层中接收前端传来的分页参数,并调用Service层的分页查询方法: ```java @RestController public class UserController { @Autowired private UserService userService; @GetMapping("/users") public PageInfo<User> selectByPage(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize, String name) { return userService.selectByPage(pageNum, pageSize, name); } } ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值