【JAVA随记】Mybatis-plus实现数据库数据分页显示

1.Mybatis-plus拦截器配置 

(该配置文件位置随便放)

public class MyBatisPlusConfig {

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        //向Mybatis过滤器链中添加分页拦截器
        interceptor.addInnerInterceptor(new 
                                        PaginationInnerInterceptor(DbType.POSTGRE_SQL));
        //还可以添加其它的拦截器
        return interceptor;
    }

    @Bean
    public ConfigurationCustomizer configurationCustomizer() {
        return configuration -> configuration.setUseDeprecatedExecutor(false);
    }

}

2.Service接口

public interface WorkonsecondmentService extends IService<Workonsecondment>{

    /**
     * 分页查询数据
     * @param CurrentPage
     * @param PageSize
     * @return
     */
    Page<Workonsecondment> getworkByPage(Integer CurrentPage,Integer PageSize);
}

3.Service接口实现类

    @Override
    public Page<Workonsecondment> getworkByPage(Integer currentPage, Integer pageSize) {
            Page<Workonsecondment> page=new Page<>(currentPage,pageSize);
            wkmapper.getWorkByPage(page);
            return page;
    }

4.Mapper接口

@Mapper
public interface WorkonsecondmentMapper extends BaseMapper<Workonsecondment> {
       /**
     * 分页返回数据
     * @param page
     * @return
     */
    Page<Workonsecondment> getWorkByPage(Page<Workonsecondment> page);
}

5.Mapper接口对应xml文件

<select id="getWorkByPage" resultType="Workonsecondment">
        select * from WORKONSECONDMENT
</select>

文件里面的resultType是对应实体类的类型

Controller层测试

@RequestMapping(value = "/getByPage", method = RequestMethod.GET)
    public CommonResult getWorkByPage(@RequestParam(value = "currentPage", defaultValue = "1") Integer currentPage,
                                      @RequestParam(value = "pageSize", defaultValue = "1") Integer pageSize) {
        Page<Workonsecondment> page = wkds.getworkByPage(currentPage, pageSize);
        List<Workonsecondment> records = page.getRecords();
        for (Workonsecondment record : records) {
            System.out.println(record);
        }
        System.out.println(page);
        return CommonResult.success(page, "获取成功");
    }

CommonResult是我自己封装的数据统一格式。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值