MyBatis中逆向工程的分页处理

逆向工程生成的代码是不支持分页处理的,如果想进行分页需要自己编写mapper,这样就失去逆向工程的意义了。为了提高开发效率可以使用mybatis的分页插件PageHelper。

1.Mybatis分页插件 - PageHelper说明:

如果你也在用Mybatis,建议尝试该分页插件,这个一定是最方便使用的分页插件。

该插件目前支持Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库分页。

2.使用方法:

第一步:把PageHelper依赖的jar包添加到工程中。官方提供的代码对逆向工程支持的不好,使用参考资料中的pagehelper-fix

<dependency>
                <groupId>com.github.pagehelper</groupId>
                <artifactId>pagehelper</artifactId>
                <version>3.4.2-fix</version>
 </dependency>

百度云链接:https://pan.baidu.com/s/1awpXThQZGZ0AcEMrb_gMUg 密码:m6jd

将文件下载后导入eclipse中,maven安装到本地仓库即可

 第二步:在Mybatis配置xml中配置拦截器插件:

<!-- 配置拦截器插件 -->
    <plugins>
        <plugin interceptor="com.github.pagehelper.PageHelper">
            <!-- 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库-->        
            <property name="dialect" value="mysql"/>
        </plugin>
    </plugins>

第三部:在代码中使用:

/**
     * 商品列表分页信息
     */
    @Override
    public EasyUIDataGridResult getItemList(int page, int rows) {   //参数从web层传入
        //设置分页信息
        PageHelper.startPage(page, rows);
        //执行查询
        TbItemExample example = new TbItemExample();
        List<TbItem> list = tbItemMapper.selectByExample(example);
        
        //创建一个返回值对象
        EasyUIDataGridResult result = new EasyUIDataGridResult();
        //将查询到的每页显示的数据封装到result中
        result.setRows(list);
        //取分页结果
        PageInfo<TbItem> pageInfo = new PageInfo<>(list);
        long total = pageInfo.getTotal();
        //封装分页的总数
        result.setTotal(total);
        return result;
    }

猜你喜欢

转载自blog.csdn.net/A_jungle/article/details/82256208