pom.xml 文件导入jar包
<!-- Mybatis分页插件:PageHelper -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.2</version>
</dependency>
mybatis-config.xml 配置 pagehelper 工具
<!-- mybatis分页工具pagehelper的配置,或者是在spring-mybatis.xml配置文件中加<property name="plugins"> -->
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<!--指定数据库连接类型,此属性可以自动检测,不配置也可以 -->
<property name="helperDialect" value="mysql" />
<!--分页合理化参数,默认文false;pageNum<=0,查询第一页;pageNum>总页数,查询最后一页 -->
<property name="reasonable" value="true" />
</plugin>
</plugins>
或者是在 spring-mybatis.xml 中配置
<!-- 创建sqlSessionFactory工厂,在工厂中依赖数据源,将数据源映射到sqlSessionFactory中 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 获得mybatis-config.xml配置文件的别名配置 -->
<property name="configLocation" value="classpath:mybatis-config.xml" />
<!-- mybatis分页工具pagehelper的配置 -->
<!-- <property name="plugins">
<array>
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<value>
helperDialect=mysql
reasonable=true
</value>
</property>
</bean>
</array>
</property> -->
<!-- ref中的dataSource是<bean id="dataSource">创建数据源,连接数据库的id值 -->
<property name="dataSource" ref="dataSource" />
<!-- 获得所有的mapper文件,自动扫描mapping.xml文件 -->
<property name="mapperLocations" value="classpath:mappers/*.xml" />
</bean>
userMapper.xml文件
<!-- 查询全部用户信息 -->
<select id="list" resultMap="userRoleMap">
select *,GROUP_CONCAT(r.role_description) descList from t_user u
left join t_user_role ur on u.id = ur.userid
left join t_role r on ur.roleid = r.role_id
group by u.id
</select>
UserDao层
// 查询全部信息
List<User> list();
UserService层
// 分页查询全部用户信息
PageInfo<User> find(Integer pageNum);
// 或
List<User> list();
UserServiceImpl层
@Override
public PageInfo<User> find(Integer pageNum) {
if (pageNum == null || pageNum < 1) {
pageNum = 1;// 页码,从1开始
}
Integer pageSize = 5;// 每页显示数据
PageHelper.startPage(pageNum, pageSize);// 指定开始分页
List<User> list = userDao.list();
PageInfo<User> page = new PageInfo<User>(list);
return page;
}
// 或
@Override
public List<User> list() {
return userDao.list();
}
UserController层
@GetMapping("list/{pageNum}")
public String find(Model model, @PathVariable Integer pageNum) {
PageInfo<User> page = userService.find(pageNum);
model.addAttribute("page", page);
return "user/list";
}
// 或
//@GetMapping("list/{pageNum}")
//public String list(Model model, @PathVariable Integer pageNum) {
// if (pageNum == null || pageNum < 1) {
// pageNum = 1;// 页码,从1开始
// }
// Integer pageSize = 5;// 每页显示数据
// PageHelper.startPage(pageNum, pageSize);// 指定开始分页
// List<User> list = userService.list();
// PageInfo<User> page = new PageInfo<User>(list);
// model.addAttribute("page", page);
// return "user/list";
//}
JSP页面
<div class="box">
<div class="box-header with-border">
<h3 class="box-title">用户列表</h3>
<div class="box-tools">
<button type="button" class="btn btn-block btn-success btn-flat"
data-toggle="modal" data-target="#add">
<i class="fa fa-plus"></i>
</button>
</div>
</div>
<div class="box-body">
<table class="table table-bordered">
<tr>
<th>用户ID</th>
<th>用户名</th>
<th>手机号码</th>
<th>昵称</th>
<th>邮箱</th>
<th>角色列表</th>
<th style="width: 200px">操作</th>
</tr>
<c:forEach items="${page.list }" var="u">
<tr>
<td>${u.id }</td>
<td>${u.username }</td>
<td>${u.phone }</td>
<td>${u.nickname }</td>
<td>${u.email }</td>
<td>${u.descList }</td>
<td>
<div class="btn-group" style="margin-left: 9px">
<button type="button" class="btn btn-info look"
style="margin-right: 3px;" data-toggle="modal" data-target="#show">
<input type="hidden" value="${u.id }">查看
</button>
<button type="button" class="btn btn-warning look"
style="margin-right: 3px" data-toggle="modal" data-target="#update">
<input type="hidden" value="${u.id }">修改
</button>
<a class="btn btn-danger"
href="javascript:if(confirm('确实要删除吗?'))location='del/'+${u.id}">删除</a>
</div>
</td>
</tr>
</c:forEach>
</table>
</div>
<div class="box-footer clearfix">
<ul class="pagination pagination-sm no-margin pull-right">
<li>
<a
style="
<c:if test="${page.isFirstPage }">
pointer-events: none;border:1px solid #e6e6e6;background-color:#FBFBFB;color:#C9C9C9;
cursor:not-allowed;opacity:1
</c:if>"
href="${pageContext.request.contextPath}/user/list/${page.pageNum-1 }">«</a>
</li>
<li>
<c:forEach begin="1" step="1" end="${page.pages }" var="c">
<c:choose>
<c:when test="${c eq page.pageNum }">
<a style="color: red; pointer-events: none"
href="${pageContext.request.contextPath}/user/list/${c }">${c }</a>
</c:when>
<c:otherwise>
<a href="${pageContext.request.contextPath}/user/list/${c }">${c }</a>
</c:otherwise>
</c:choose>
</c:forEach>
</li>
<li>
<a style="padding-left: 15px;
<c:if test="${page.isLastPage }">
pointer-events: none;border:1px solid #e6e6e6;background-color:#FBFBFB;
color:#C9C9C9;cursor:not-allowed;opacity:1
</c:if>; text-decoration: none;"
href="${pageContext.request.contextPath}/user/list/${page.pageNum+1 }">»</a>
</li>
<li><span style="margin-left: 10px;">共 ${page.total } 条</span></li>
</ul>
</div>
</div>