Mybatis实现分页功能(完整流程)

Mybatis实现分页功能

准备数据库

实体类 People

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public class People {
private int id;
private String name;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "People [id=" + id + ", name=" + name + ", age=" + age + "]";
}
}

导入相关jar包

全局配置文件mybatis.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?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"> <!--dtd文件 约束(chm文件可以找到)-->

<configuration>

<typeAliases>
<package name="com.bjsxt.pojo"/> <!-- 起别名 方便在配置的xml里面直接返回值就可以只写类名-->
</typeAliases>

<environments default="default">
<environment id="default">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/ssm"/>
<property name="username" value="root"/>
<property name="password" value="njdxrjgc7777777."/>
</dataSource>
</environment>
</environments>

<mappers>
<mapper resource="com/bjsxt/mapper/PeopleMapper.xml"/> <!-- 直接调用配置文件 执行sql语句 -->
</mappers>

</configuration>

配置PeopleMapper.xml

1
2
3
4
5
6
7
8
9
10
11
<?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"> <!--dtd文件 约束(chm文件可以找到)-->

<mapper namespace="com.bjsxt.mapper.PeopleMapper" > <!-- 确定这个mapper名字: 包.类名 -->
<select id="selByPage" resultType="People" parameterType="map"> <!-- 确定方法叫selByPage 返回值给people实体类 传入参数的类型是map -->
select * from people limit #{pageStart},#{pageSize}; <!-- limit 后面通过#{}方式传入两个map参数-->
</select>
</mapper>

构建实体类pageInfo用于传递分层相关的参数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

public class PageInfo {

private int pageSize; //一页分布几个
private int pageNumber; //第几页
private long total; //一共总页数 分多少页
private List<?> list; //当前页显示哪些数据

public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getPageNumber() {
return pageNumber;
}
public void setPageNumber(int pageNumber) {
this.pageNumber = pageNumber;
}
public long getTotal() {
return total;
}
public void setTotal(long total) {
this.total = total;
}
public List<?> getList() {
return list;
}
public void setList(List<?> list) {
this.list = list;
}

}

service和实现层

service层实现代码:

1
2
3
4
public interface PeopleService {
//实现分页展示 传入两个参数
PageInfo showPage(int pageSize,int pageNumber) throws IOException;
}

serviceimpl层实现代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public class PeopleServiceImpl implements PeopleService {

@Override
public PageInfo showPage(int pageSize, int pageNumber) throws IOException {

//获取配置资源
InputStream is = Resources.getResourceAsStream("mybatis.xml");

//使用构建者模式(加快工厂实例化)和工厂模式生产session
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
SqlSession session = factory.openSession();

//创建对象
PageInfo pi=new PageInfo();
pi.setPageNumber(pageNumber);
pi.setPageSize(pageSize);

//新建map对象
Map<String,Object> map=new HashMap<>();
map.put("pageStart",pageSize*(pageNumber-1)); //设置好每一页开始的那个数字
map.put("pageSize",pageSize); //设置好每一页有几个
pi.setList(session.selectList("com.bjsxt.mapper.PeopleMapper.selByPage",map)); //要设置返回结果 第二个参数是传入两个key值

//总条数
long count=session.selectOne("com.bjsxt.mapper.PeopleMapper.selCount"); //获取查询总条数

pi.setTotal(count%pageSize==0?count/pageSize:count/pageSize+1); //要设置总共几页 总数/每一页

return pi;
}

}

因为要获得所有数count 要在xml配一个查询所有的select标签

1
2
3
<select id="selCount" resultType="Long">
select count(*) from people
</select>

Servlet类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
@WebServlet("/page")  //使用注解的方式 最后要在web里面写/page才可以

public class ShowPageServlet extends HttpServlet{

//面向接口编程
private PeopleService peopleService=new com.bjsxt.serviceimpl.PeopleServiceImpl();

@Override
protected void service(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{

//第一次访问的验证 如果没有传递参数 设置默认值
String pageSizeStr = req.getParameter("pageSize");
int pageSize=2; //一页显示几个
if(pageSizeStr!=null&&!pageSizeStr.equals(""))
{
pageSize = Integer.parseInt(pageSizeStr); //获取两个参数
}

String pageNumberStr = req.getParameter("pageNumber");
int pageNumber = 1; //显示第几页
if(pageNumberStr!=null&&!pageNumberStr.equals(""))
{
pageNumber = Integer.parseInt(pageNumberStr); //获取两个参数
}

PageInfo pi=peopleService.showPage(pageSize, pageNumber); //分页展示效果给了pageinfo实体类

// 传过去
req.setAttribute("PageInfo", pi);

// 请求转发
req.getRequestDispatcher("index.jsp").forward(req, resp);

}

}

index.jsp页面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<%@ 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 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>
<table border="1">
<tr>
<th>编号</th>
<th>姓名</th>
<th>年龄</th>
</tr>
<c:forEach items="${PageInfo.list }" var="pi">
<tr>
<td>${pi.id }</td>
<td>${pi.name }</td>
<td>${pi.age }</td>
</tr>
</c:forEach>
</table>

<!-- 跳转通过onclick点击事件 然后js的行内表达方式 取消跳转等功能 c:if进行判断 -->
<a href="page?pageNumber=${PageInfo.pageNumber-1 }&pageSize=${PageInfo.pageSize}" <c:if test="${PageInfo.pageNumber<=1 }"> onclick="javascript:return false;" </c:if> >上一页</a>
<a href="page?pageNumber=${PageInfo.pageNumber+1 }&pageSize=${PageInfo.pageSize}" <c:if test="${PageInfo.pageNumber>=PageInfo.total }"> onclick="javascript:return false;" </c:if> >下一页</a>

</body>
</html>

结果和分析

一定要在输入的后面添加注解的 /path :

完整代码放在mybatis分页的仓库里面


×

纯属好玩

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

文章目录
  1. 1. Mybatis实现分页功能
    1. 1.1. 准备数据库
    2. 1.2. 实体类 People
    3. 1.3. 导入相关jar包
    4. 1.4. 全局配置文件mybatis.xml
    5. 1.5. 配置PeopleMapper.xml
    6. 1.6. 构建实体类pageInfo用于传递分层相关的参数
    7. 1.7. service和实现层
    8. 1.8. Servlet类
    9. 1.9. index.jsp页面
    10. 1.10. 结果和分析
,