分页查询
分页查询
先在包中引入分页分页是使用的标签文件Page.java和Navigation.java
配置commons.tld文件 自定义的标签文件主要实现分页功能
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE taglib
PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
"http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
<!-- 指定标签库的版本号 -->
<tlib-version>2.0</tlib-version>
<!-- 指定JSP的版本号 -->
<jsp-version>1.2</jsp-version>
<!-- 指定标签库的名称 -->
<short-name>common</short-name>
<!-- 指定标签库的URI -->
<uri>http://itheima.com/common/</uri>
<!-- 指定标签库的显示名称 -->
<display-name>Common Tag</display-name>
<!-- 指定标签库的描述 -->
<description>Common Tag library</description>
<!-- 注册一个自定义标签 -->
<tag>
<!-- 指定注册的自定义标签名称 -->
<name>page</name>
<!-- 指定自定义标签的标签处理器类 -->
<tag-class>com.itheima.common.utils.NavigationTag</tag-class>
<!-- 指定标签体类型 -->
<body-content>JSP</body-content>
<!-- 描述 -->
<description>create navigation for paging</description>
<!-- 指定标签中的属性 -->
<attribute>
<!-- 指定属性名称 -->
<name>url</name>
<!-- 该属性为true时表示其指定是属性为必须属性 -->
<required>true</required>
<!-- 该属性用于指定能不能使用表达式来动态指定数据,为true时表示可以 -->
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>bean</name>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>number</name>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
Item.java文件
package com.itheima.po;
public class Item { private Integer id;
private String project;
private String time;
private String place;
private String type;
private Integer start; //起始行
private Integer rows; //所取行数
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getProject() {
return project;
}
public void setProject(String project) {
this.project = project;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getPlace() {
return place;
}
public void setPlace(String place) {
this.place = place;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public Integer getStart() {
return start;
}
public void setStart(Integer start) {
this.start = start;
}
public Integer getRows() {
return rows;
}
public void setRows(Integer rows) {
this.rows = rows;
}
}
文件ItemDao.java 和ItemDao.xml
public interface ItemDao {
public List<Item> selectList(Item item);
public Integer selectCount(Item item);
}
<?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.itheima.dao.ItemDao">
<select id="selectList" parameterType="item"
resultType="item">
select *from t_m2 where 1=1
<if test="start !=null and rows != null">
limit #{start},#{rows}
</if>
</select>
<!-- 查询客户总数 -->
<select id="selectCount" parameterType="item"
resultType="Integer">
select count(*) from t_m2
</select>
</mapper>
ItemService.java和ItemServiceImpl.java文件
public interface ItemService {
// 查询客户列表
public Page<Item> findList(Integer page, Integer rows );
}
package com.itheima.service;
import com.itheima.common.utils.Page;
import com.itheima.dao.ItemDao;
import com.itheima.po.Customer;
import com.itheima.po.Item;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service("itemService")
@Transactional
public class ItemServiceImpl implements ItemService {
// 声明DAO属性并注入
@Autowired
private ItemDao itemDao;
// 客户列表
@Override
public Page<Item> findList(Integer page, Integer rows) {
Item item=new Item();
// 当前页
item.setStart((page-1) * rows) ;
// 每页数
item.setRows(rows);
// 查询客户列表
List<Item> items =
itemDao.selectList(item);
// 查询客户列表总记录数
Integer count = itemDao.selectCount(item);
// 创建Page返回对象
Page<Item> result = new Page<>();
result.setPage(page);
result.setRows(items);
result.setSize(rows);
result.setTotal(count);
return result;
}
}
分页查询控制类
@RequestMapping(value = "/item")
public String Item(@RequestParam(defaultValue="1")Integer page,
@RequestParam(defaultValue="10")Integer rows, Model model
) {
// 条件查询所有客户
Page<Item> items = itemService
.findList(page, rows);
model.addAttribute("page", items);
return "customer";
}
}
jsp文件
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page trimDirectiveWhitespaces="true"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="itheima" uri="http://itheima.com/common/"%>
<!DOCTYPE HTML>
<html>
<head >
<style>
.style1 ul {
list-style: none;
}
.style1 li {
display: inline-block;
margin: 0 10px;
font-size: 20px;
}
</style>
</head>
<
<table border=1>
<tr>
<td>id</td>
<td>项目</td>
<td>时间</td>
<td>地点</td>
</tr>
<c:forEach items="${page.rows }" var="s" >
<tr>
<td>${s.id}</td>
<td>${s.project}</td>
<td>${s.time}</td>
<td>${s.place}</td>
</tr>
</c:forEach>
</table>
<div class="style1">
<itheima:page url="${pageContext.request.contextPath }/item" />
</div>
</body>
</html>
运行结果