Springboot-mybatisplus-解决分页组件IPage失效问题

Springboot-mybatisplus-解决分页组件IPage失效问题

背景

mybatisplus的分页插件IPage很好用,不管是基于@select注解还是基于XML的都可以实现分页查询;
不知道代码有什么改动,用着用着就分页居然不好使了-_-,select时由于没有注入分页条件,导致将所有结果都返回了。没有深究直接上解决方案吧!

添加分页拦截器

@Configuration
public class MybatisPlusConfig {
    @Bean
    public PaginationInterceptor paginationInterceptor(){
        PaginationInterceptor page = new PaginationInterceptor();
        page.setDbType(DbType.POSTGRE_SQL);//选择对应DB类型
        return page;
    }
}

IPage分页使用

mapper需要继承BaseMapper

@Repository
public interface XxxMapper extends BaseMapper<XxxMapper > {
    Page<XxxBo> selectAllByPage(IPage<XxxBo> page,@Param("keyword") String keyword);
}

XML配置

  <select id="selectAllByPage" resultMap="BaseResultMap">
    select * from xx.xxx where  enable=1
    <if test="keyword != null">
      and (id ~* #{keyword} or name  ~* #{keyword} or  code ~* #{keyword})
    </if>
  </select>

服务层调用

    @Override
    public Page<XxxBo> viewInfoPage(PageReq req) {
        IPage<XxxBo> page = new Page<>(req.getPage().getPage(),req.getPage().getSize());
        Page<XxxBo> list = xxxMapper.selectAllByPage(page,req.getKeyword());
        return list;
    }
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 首先需要在pom.xml中添加mybatisplus和分页插件的依赖: ``` <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.0</version> </dependency> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.3.0</version> </dependency> ``` 2. 在application.yml中配置mybatisplus和分页插件的参数: ``` mybatis-plus: mapper-locations: classpath:/mapper/**/*.xml global-config: db-config: id-type: auto field-strategy: not_empty table-prefix: sys_ configuration: map-underscore-to-camel-case: true log-impl: org.apache.ibatis.logging.stdout.StdOutImpl pagehelper: helperDialect: mysql reasonable: true supportMethodsArguments: true params: count=countSql ``` 3. 编写Mapper接口: ``` @Mapper public interface DeptMapper extends BaseMapper<Dept> { List<Dept> selectPage(Page<Dept> page, @Param("deptName") String deptName); } ``` 4. 编写Service实现类: ``` @Service public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept> implements DeptService { @Override public IPage<Dept> selectPage(Page<Dept> page, String deptName) { QueryWrapper<Dept> queryWrapper = new QueryWrapper<>(); if (StringUtils.isNotBlank(deptName)) { queryWrapper.like("dept_name", deptName); } return baseMapper.selectPage(page, queryWrapper); } } ``` 5. 编写Controller: ``` @RestController @RequestMapping("/dept") public class DeptController { @Autowired private DeptService deptService; @GetMapping("/list") public Result list(Page<Dept> page, String deptName) { IPage<Dept> data = deptService.selectPage(page, deptName); return Result.success(data); } } ``` 6. 在前端页面中,可以通过ajax请求/list接口,传入参数page和deptName实现分页查询。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值