PageHelper分页插件的使用二

PageHelper分页插件的使用二:

二、使用流程

1.添加依赖包:下载依赖

pagehelper-5.1.10,jsqlparser-3.0;

2.在spring-mvc中添加:

<!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <!--pageHelper-->
    <property name="plugins">
        <array>
            <bean class="com.github.pagehelper.PageInterceptor">
                <property name="properties">
                    <!--使用下面的方式配置参数,一行配置一个 -->
                    <value>
                        helperDialect=postgresql
                        reasonable=true
                        supportMethodsArguments=true
                        params=count=countSql
                        autoRuntimeDialect=true
                    </value>
                </property>
            </bean>
        </array>
    </property>
</bean> 

4、在service中添加接口:

PageInfo<User> findByPage(Integer pageNum,Integer pageSize);//分页

5、ServiceImpl中实现接口service:

@Override
    public PageInfo<User> findByPage(Integer pageNum, Integer pageSize) {
        // TODO Auto-generated method stub
        
        if(pageNum==null)
        { 
            pageNum=1;
        }
        
        Page<User> page=PageHelper.startPage(pageNum, pageSize);
    
    
        List<User> users=userDao.userFindAll();
        
        PageInfo<User> pageInfo=new PageInfo<User>(users);
        
        return pageInfo;
    }

6、在controller中:

@Controller
public class UserController2 {

    @Autowired
    private UserService userService;
    
    @RequestMapping("/users")
    public String findByPage(Model model,Integer  pageNum,Integer  pageSize)
    {
    PageInfo<User> pageInfo=userService.findByPage(2, 3);
        model.addAttribute("pageInfo",pageInfo); 
        return "test";
    }
     
}

7、再test.jsp中:

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>

    <h1>sdda;sjlfj;aso</h1>
    <table border="1" cellpadding="0" cellspacing="20">

        <tr>
            <th>编号</th>
            <th>编号1</th>
            <th>编号2</th>
            <th>编号3</th>
            <th>编号4</th>
        </tr>

        <c:forEach items="${pageInfo.list}" var="user">
            <tr>
                <td>${user.getUserId() }</td>
                <td>${user.getUserId() }</td>
                <td>${user.getUserId() }</td>
                <td>${user.getUserId() }</td>
            </tr>

        </c:forEach>
    </table>
    <span>第${pageInfo.pageNum}页/共${pageInfo.pages }页</span>

    <a href="users?pageNum=0">首页</a>

    <c:if test="${pageInfo.isHasPreviousPage()}">
        <a href="users?pageNum=${pageInfo.pageNum-1}">上一页</a>
    </c:if>

    <c:if test="${pageInfo.isHasNextPage()}">
        <a href="users?pageNum=${pageInfo.pageNum+1}">下一页</a>
    </c:if>
    <a href="users?pageNum=${pageInfo.pages}">未页</a>
</body>
</html>

结果:

编号 编号1 编号2 编号3  
1 1 1 1
123456 123456 123456 123456
2 2 2 2

第1页/共3页 首页 下一页 未页

上篇:

PageHelper分页插件的使用二:使用流程

猜你喜欢

转载自www.cnblogs.com/ITyunlin/p/11688550.html