SSM+PageHelper实现分页查询

pom.xml中引入page分页的jar包

        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>5.1.2</version>
        </dependency> 

mybatis配置文件加入

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

<configuration>
    <!-- 引入 pageHelper插件 -->
<!--注意这里要写成PageInterceptor, 5.0之前的版本都是写PageHelper, 5.0之后要换成PageInterceptor-->

    <plugins>  
        <plugin interceptor="com.github.pagehelper.PageInterceptor">  
            <!--reasonable:分页合理化参数,默认值为false。
                当该参数设置为 true 时,pageNum<=0 时会查询第一页,
                pageNum>pages(超过总数时),会查询最后一页。
                默认false 时,直接根据参数进行查询。-->  
            <property name="reasonable" value="true"/>  
        </plugin>  
    </plugins> 
</configuration>

controller:

@Controller
public class UserController {
    
    @Autowired
    private UserService pageService;
    
    /**
     * 分页查询
     */
    @RequestMapping(value="/list",method=RequestMethod.GET)
    public String pageList(ModelMap map,@RequestParam(defaultValue="1",required=true,value="pageNo") Integer pageNo){
        Integer pageSize=4;//每页显示记录数
        //分页查询
        PageHelper.startPage(pageNo, pageSize);
        List<User> userList = pageService.list();//获取所有用户信息
        PageInfo<User> pageInfo=new PageInfo<User>(userList);
        map.addAttribute("pageInfo", pageInfo);

        return "list";
    }

}

mapper.xml

<?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.jiangfx.mapper.UserMapper">
    <!-- 查询所有結果 -->
    <select id="getAllUser" resultType="com.jiangfx.entity.User">
        select * from user
    </select>
</mapper>

下面是jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
  <center>
        <table width="200" border="1">
          <tr>
            <th scope="col">ID</th>
            <th scope="col">姓名</th>
            <th scope="col">性别</th>
            <th scope="col">城市</th>
          </tr>
          <c:forEach items="${pageInfo.list}" var="user">
          <tr>
            <td>${user.id}</td>
            <td>${user.username}</td>
            <td>${user.sex}</td>
            <td>${user.city}</td>
          </tr>
          </c:forEach>
        </table>
        <p>当前 ${pageInfo.pageNum }页,总${pageInfo.pages }
                页,总 ${pageInfo.total } 条记录</div></p>
        <a href="list?pageNo=${pageInfo.firstPage}">第一页</a>
        <c:if test="${pageInfo.hasPreviousPage }">
            <a href="list?pageNo=${pageInfo.pageNum-1}">上一页</a>
        </c:if>
      
        <c:if test="${pageInfo.hasNextPage }">
            <a href="list?pageNo=${pageInfo.pageNum+1}">下一页</a>
        </c:if>
        
        <a href="list?pageNo=${pageInfo.lastPage}">最后页</a>
    </center>
  </body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值