SpringBoot+thymeleaf+Mybatis实现pagehelper的分页功能

SpringBoot+thymeleaf+Mybits(pagehelper)实例:
引入依赖:

        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.2.13</version>
        </dependency>

application.yml配置分页插件参数(文末有介绍)

pagehelper:                #分页插件
  helper-dialect: mysql
  reasonable: true
  support-methods-arguments: true
  params:

分页查询的controller,其实就是在原有的Mapper查询基础上,加pagehelper的方法


    @GetMapping("/getAllUser")
    public String getAllUser(Model model,@RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum, User user){
        //默认第一页开始、一行显示5个
        PageHelper.startPage(pageNum,5);
        
        //原有的查询方法(需写在startpage后)
        List<User> lists = userMapper.getUsers(user);
        
        //pagehelper默认写法、传递session
        PageInfo<User> pageInfo = new PageInfo<User>(lists);
        model.addAttribute("pageInfo",pageInfo);
        return "list"; //跳转到list.html
    }

list.html页面

    <table border="1">
        <tr>
            <th width="10%">用户名</th>
            <th width="20%">真实姓名</th>
            <th width="10%">性别</th>
            <th width="10%">出生日期</th>
            <th width="10%">用户类型</th>
            <th width="30%">操作</th>
        </tr>
        <tr th:each="u:${pageInfo.list}"> 
            <td th:text="${u.username}"></td>
            <td th:text="${u.realName}"></td>
            <!--性别:12-->
            <td th:text="${u.gender == 1 ? '女' : '男'}"></td>
            <td th:text="${#dates.format(u.birthday, 'yyyy-MM-dd')}"></td>
            <!--1管理员  2经理  3普通用户-->
            <td th:text="${u.userType==1 ? '管理员' : (u.userType==2 ? '经理' : '普通用户') }"></td>
            <td>
                <a th:href="@{/user/} + ${u.id}" href="view.html"><img th:src="@{/img/read.png}" src="../img/read.png" alt="查看" title="查看"/></a>
                <a th:href="@{/user/} + ${u.id} +'?type=update'" href="update.html"><img th:src="@{/img/xiugai.png}"  src="../img/xiugai.png" alt="修改" title="修改"/></a>
                <a th:attr="del_uri=@{/user/}+${u.id}"  href="#" class="delete" ><img th:src="@{/img/schu.png}"  src="../img/schu.png" alt="删除" title="删除"/></a>
            </td>
        </tr>
    </table>
    <p>当前 <span th:text="${pageInfo.pageNum}"></span>,<span th:text="${pageInfo.pages}"></span>,<span th:text="${pageInfo.total}"></span> 条记录</p>
    <a th:href="@{/getAllUser}">首页</a>
    <a th:href="@{/getAllUser(pageNum=${pageInfo.hasPreviousPage}?${pageInfo.prePage}:1)}">上一页</a>
    <a th:href="@{/getAllUser(pageNum=${pageInfo.hasNextPage}?${pageInfo.nextPage}:${pageInfo.pages})}">下一页</a>
    <a th:href="@{/getAllUser(pageNum=${pageInfo.pages})}">尾页</a>

User.java部分代码

public class User {

    private Integer id;
    //用户名
	private String username;
    //真实姓名
    private String realName;
    //用户密码
    private String password;
    //性别:1 女  2 男
    private Integer gender;
    //生日
    private Date birthday;
    //1管理员  2经理  3普通用户
    private Integer userType;

Pagehelper地址:

https://github.com/pagehelper/pagehelper-spring-boot

分页插件参数介绍

  1. helperDialect :分页插件会自动检测当前的数据库链接,自动选择合适的分页方式。 你可以配置helperDialect 属性来指定分页插件使用哪种方言。配置时,可以使用下面的缩写值:oracle , mysql , mariadb , sqlite , hsqldb , postgresql , db2 , sqlserver , informix , h2 , sqlserver2012 , derby你也可以实现 AbstractHelperDialect ,然后配置该属性为实现类的全限定名称即可使用自定义的实现方法。

  2. offsetAsPageNum :默认值为 false ,该参数对使用 RowBounds 作为分页参数时有效。 当该参数设置为true 时,会将 RowBounds 中的 offset 参数当成 pageNum 使用,可以用页码和页面大小两个参数进行分页。

  3. rowBoundsWithCount :默认值为 false ,该参数对使用 RowBounds 作为分页参数时有效。 当该参数设置为 true 时,使用 RowBounds 分页会进行 count 查询。

  4. pageSizeZero :默认值为 false ,当该参数设置为 true 时,如果 pageSize=0 或者 RowBounds.limit =0 就会查询出全部的结果(相当于没有执行分页查询,但是返回结果仍然是 Page 类型)。

  5. reasonable :分页合理化参数,默认值为 false 。当该参数设置为 true 时, pageNum<=0 时会查询第一页, pageNum>pages (超过总数时),会查询最后一页。默认 false 时,直接根据参数进行查询。

  6. params :为了支持 startPage(Object params) 方法,增加了该参数来配置参数映射,用于从对象中根据属性名取值, 可以配置 pageNum,pageSize,count,pageSizeZero,reasonable ,不配置映射的用默认值, 默认值为pageNum=pageNum;pageSize=pageSize;count=countSql;reasonable=reasonable;pageSizeZero=pageSizeZero。

  7. supportMethodsArguments :支持通过 Mapper 接口参数来传递分页参数,默认值 false ,分页插件会从查询方法的参数值中,自动根据上面 params 配置的字段中取值,查找到合适的值时就会自动分页。 使用方法可以参考测试代码中的 com.github.pagehelper.test.basic 包下的 ArgumentsMapTest 和ArgumentsObjTest 。

  8. autoRuntimeDialect :默认值为 false 。设置为 true 时,允许在运行时根据多数据源自动识别对应方言的分页 (不支持自动选择 sqlserver2012 ,只能使用 sqlserver ),用法和注意事项参考下面的场景五。

  9. closeConn :默认值为 true 。当使用运行时动态数据源或没有设置 helperDialect 属性自动获取数据库类型时,会自动获取一个数据库连接, 通过该属性来设置是否关闭获取的这个连接,默认 true 关闭,设置为false 后,不会关闭获取的连接,这个参数的设置要根据自己选择的数据源来决定。

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值