Mybatis分页插件PageHelper的配置和使用方法----经典案例

Mybatis分页插件PageHelper的配置和使用方法

查询所有Demo 代码清晰

1.导两个jar包

①、pagehelper-4.1.6.jar 分页插件的核心包
②、jsqlparser-0.9.4.jar 所依赖的包
<!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper -->
<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper</artifactId>
    <version>4.1.6</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.github.jsqlparser/jsqlparser -->
<dependency>
    <groupId>com.github.jsqlparser</groupId>
    <artifactId>jsqlparser</artifactId>
    <version>0.9.4</version>
</dependency>

2.配置文件:

在mybatis-config.xml中配置如下:
<plugins>
        <!-- 配置pagehelper分页插件 -->
        <!-- PageHelper4.1.1 -->
        <plugin interceptor="com.github.pagehelper.PageHelper">
            <property name="dialect" value="mysql" />
            <property name="offsetAsPageNum" value="false" />
            <property name="rowBoundsWithCount" value="false" />
            <property name="pageSizeZero" value="true" />
            <property name="reasonable" value="false" />
            <property name="supportMethodsArguments" value="false" />
            <property name="returnPageInfo" value="none" />
        </plugin>
    </plugins>

在这里插入图片描述

3、login.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>用户首页</title>
</head>
<body>
<h1>点击进入注册页面</h1>
<a href="${pageContext.request.contextPath }/fenye">查询所有用户信息</a>
</body>
</html>

4、list.jsp分页查询成功页面

在这里插入图片描述

<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <!--引入分页查询按钮的CSS格式-->
    <link href="${pageContext.request.contextPath}/css/page.css" rel="stylesheet" type="text/css"/>
    <title>分页查询</title>
</head>
<body>
<center>
    <h1>分页显示</h1>
    <!-- 设置表格格式-->
    <table border="1" cellpadding="0" cellspacing="0" width="50%" align="center">
        <tr align="center">
            <th>编号</th>
            <th>姓名</th>
            <th>工资</th>
            <th>部门</th>
        </tr>
        <!--
        list是PageInfo<T>的结果集
        这必须是list切记不要写成自己的pojo类属性
        -->
        <c:forEach items="${p.list }" var="emp">
            <tr align="center">
                <td>${emp.id }</td>
                <td>${emp.eName }</td>
                <td>${emp.sal }</td>
                <td>${emp.dept.dname }</td>
            </tr>
        </c:forEach>
    </table>


    <!-- 分页查询按钮  不要改(pageNum、pages等)这些都是插件类提供好的属性变量
    				 拿去用就改一下(/fenye)这个映射的路径 
    -->
    <form action="${pageContext.request.contextPath}/fenye" method="post">
        <ul class="pagination">
            <li><a href="${pageContext.request.contextPath}/fenye?pageNum=1">首页</a></li>
            <c:if test="${p.pageNum>1}">
                <li><a href="${pageContext.request.contextPath}/fenye?pageNum=${p.pageNum-1}">上一页</a></li>
            </c:if>
            <c:forEach begin="1" end="${p.pages}" step="1" var="pageNum">
                <li><a href="${pageContext.request.contextPath}/fenye?pageNum=${pageNum}">${pageNum}</a></li>
            </c:forEach>
            <c:if test="${p.pageNum<p.pages}">
                <li><a href="${pageContext.request.contextPath}/fenye?pageNum=${p.pageNum+1}">下一页</a></li>
            </c:if>
            <li><a href="${pageContext.request.contextPath}/fenye?pageNum=${p.pages}">末页</a></li>
            <li><a>共${p.pages }</a></li>
            <li><a>跳到<input type="text" name="pageNum" style="width: 20px; height: 20px"></a></li>
            <li><a><input type="submit" value="GO"/></a></li>
        </ul>
    </form>

</center>
</body>
</html>

5、page.css 在list.jsp中引入

在这里插入图片描述


ul.pagination {
    display: inline-block;
    padding: 0;
    margin: 0;
}

ul.pagination li {display: inline;}

ul.pagination li a {
    color: black;
    float: left;
    padding: 8px 16px;
    text-decoration: none;
    border-radius: 5px;
}

ul.pagination li a.active {
    background-color: #4CAF50;
    color: white;
    border-radius: 5px;
}

ul.pagination li a:hover:not(.active) {background-color: #ddd;}

6、Controller层

@Controller
public class EmpController {
	//依赖注入
	@Autowired
	private EmpService empService;

    /**
     * 分页插件使用
     * @param pageNum 当前页
     * @param model 作用域
     * @return
     */
	@RequestMapping("/fenye")
	public String fenye(Integer pageNum,Model model){
	    //判断前端查过来的是否为null
		if(pageNum==null){
			pageNum=1;
		}
		
		//每页条数
		Integer pageSize=5;	
		
		//从后台查询数据并返回分页对象传到ServiceImpl
		//pageNum 当前页、pageSize 每页条数
		PageInfo<Emp> p=empService.fenye(pageNum,pageSize);
        //将查出来的信息存储到Model作用域
        model.addAttribute("p", p);
		
		return "list";
	}
	
}

7、Service层

public interface EmpService {

    /**
     * 封装pageInfo对象传入Emp对象
     * @param pageNum 当前页
     * @param pageSize 每页条数
     * @return
     */
	PageInfo<Emp> fenye(Integer pageNum, Integer pageSize);

}

8、ServiceImpl层 Service的实现

@Service
public class EmpServiceImpl implements EmpService {

	//依赖注入
	@Autowired
	private EmpMapper empMapper;

    /**
     * @param pageNum 当前页
     * @param pageSize 每页条数
     * @return
     */
	@Override
	public PageInfo<Emp> fenye(Integer pageNum, Integer pageSize) {
	
		//开启分页
		PageHelper.startPage(pageNum,pageSize);
		
		//获取查询数据放入分页对象、
        // 查询时无需关注查询的条数 不需要向Dao层中的方法传入limit后边的两个参数 (limit 1 ,5)
        //调用Dao层方法
		List<Emp> list=empMapper.findAll();

		///封装pageInfo对象并返回   PageInfo可以看到源码你就明白所有意思
		PageInfo<Emp> p=new PageInfo<Emp>(list);
		
		return p;
	}

}

9、Dao层 (EmpMapper)

public interface EmpMapper {
    //查询所有的方法
	List<Emp> findAll();
}

10、Mapper.xml (EmpMapper.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="cn.bgs.dao.EmpMapper">

    <resultMap type="cn.bgs.pojo.Emp" id="emp_dept">
        <id column="id" property="id"/>
        <result column="ename" property="eName"/>
        <result column="sal" property="sal"/>
        <result column="did" property="did"/>
        <association property="dept" javaType="cn.bgs.pojo.Dept">
            <id property="did" column="did"/>
            <result property="dname" column="dname"/>
        </association>
    </resultMap>

    <!-- 分页插件查询  List<Emp> findAll(); 两表连查-->
      <select id="findAll" resultMap="emp_dept">
		select * from emp e,dept d where e.did=d.did
	</select>

</mapper>

11、数据库表

(一)员工信息emp表

在这里插入图片描述

(二)员工部门信息dept表

在这里插入图片描述

发布了3 篇原创文章 · 获赞 1 · 访问量 341

猜你喜欢

转载自blog.csdn.net/weixin_44954939/article/details/99317691