thymeleaf+springboot+mybatis(PageHelper分页插件)

thymeleaf结合springboot实现分页展示功能;
mybatis集成PageHelper分页插件可以实现后台数据查询功能以及返回数据封装

效果如下

pom.xml配置

<!--mybatis-->
<dependency>
	<groupId>org.mybatis.spring.boot</groupId>
	<artifactId>mybatis-spring-boot-starter</artifactId>
	<version>2.0.0</version>
</dependency>
<!--mybatis分页插件pagehelper-->
<!--<dependency>-->
	<!--<groupId>com.github.pagehelper</groupId>-->
	<!--<artifactId>pagehelper</artifactId>-->
	<!--<version>5.0.0</version>-->
<!--</dependency>-->
<dependency>
	<groupId>com.github.pagehelper</groupId>
	<artifactId>pagehelper-spring-boot-starter</artifactId>
	<version>1.2.5</version>
</dependency>
<!-- pagehelper的依赖包:jsqlparser -->
<dependency>
	<groupId>com.github.jsqlparser</groupId>
	<artifactId>jsqlparser</artifactId>
	<version>0.9.5</version>
</dependency>
<!--thymeleaf引入-->
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

Mapp层

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.example.springBoot.mapper.test1.EmpMapper">
    <resultMap id="BaseResultMap" type="com.example.springBoot.domain.Employee">
        <result column="name" property="name" jdbcType="VARCHAR"/>
        <result column="address" property="address" jdbcType="VARCHAR"/>
    </resultMap>

    <sql id="Base_Column_List">
        id, name, address, position
    </sql>

    <select id="getAll" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM Employee
    </select>
</mapper>
public interface EmpMapper {

    List<Employee> getAll();
}

Controller控制层

@Controller
@RequestMapping("/userInfo")
public class UserInfoController {

    @Autowired
    EmpMapper empMapper;
    /**
     * 用户查询.
     * @return
     */
    @RequestMapping("/userList")
    public String userInfo(Model model,Integer pageNum){
        System.out.println("当前页为:"+pageNum);
        pageNum = pageNum==null?1:pageNum;
        PageHelper.startPage(pageNum,2);
        List<Employee> list = empMapper.getAll();
        PageInfo<Employee> page = new PageInfo<>(list);
        System.out.println("总记录条数为:"+page.getTotal());
        model.addAttribute("page",page);
        return "userInfo";
    }
}

展示页面

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8"/>
    <title>userList</title>
    <link rel="stylesheet" th:href="@{/css/bootstrap.css}"></link>
</head>
<body class="container">
<br/>
<h1>用户列表</h1>
<br/><br/>
<div>
    <table class="table table-striped">
        <thead>
        <tr>
            <th>#</th>
            <th>Name</th>
            <th>Address</th>
            <th>Position</th>
            <th>Edit</th>
            <th>Delete</th>
        </tr>
        </thead>
        <tbody>
        <tr  th:each="user : ${page.list}">
            <th scope="row" th:text="${user.id}">1</th>
            <td th:text="${user.name}">neo</td>
            <td th:text="${user.address}">Otto</td>
            <td th:text="${user.position}">6</td>
            <td><a th:href="@{/toEdit(id=${user.id})}">edit</a></td>
            <td><a th:href="@{/delete(id=${user.id})}">delete</a></td>
        </tr>
        </tbody>
    </table>
</div>
<div class="form-group">
    <div class="col-sm-2 control-label">
        <a href="/toAdd" th:href="@{/toAdd}" class="btn btn-info">add</a>
    </div>
    <div class="col-sm-2 control-label">
        <a href="/userInfo/userAdd" th:href="@{/userInfo/userAdd}" class="btn btn-info">上传</a>
    </div>
</div>
<div class="modal-footer no-margin-top">
    <ul class="pagination pull-right no-margin">

        <!-- 首页 -->
        <li>
            <a th:href="@{'/userInfo/userList?pageNum=1'}">首页</a>
        </li>

        <!-- 上一页 -->
        <li th:if="${page.hasPreviousPage}">
            <a th:href="@{'/userInfo/userList?pageNum=' + ${page.prePage}}" th:text="上一页"></a>
        </li>

        <!-- 中间页 -->
        <li th:each="pageNum:${#numbers.sequence(1, page.pages)}">
            <a th:href="@{'/userInfo/userList?pageNum=' + ${pageNum}}" th:text="${pageNum}" th:if="${pageNum ne page.pageNum}"></a>
            <a th:href="@{'/userInfo/userList?pageNum=' + ${pageNum}}" th:text="${pageNum}" th:if="${pageNum eq page.pageNum}" th:style="'font-weight:bold;background: #6faed9;'"></a>
        </li>

        <!-- 下一页 -->
        <li th:if="${page.hasNextPage}">
            <a th:href="@{'/userInfo/userList?pageNum=' + ${page.nextPage}}" th:text="下一页"></a>
        </li>

        <!-- 尾页 -->
        <li>
            <a th:href="@{'/userInfo/userList?pageNum=' + ${page.pages}}">尾页</a>
        </li>

    </ul>
</div>
</body>
</html>
  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

灬一抹丶清风

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值