一、导入需要的JAR
jar下载地址:点击打开链接
jsqlparser-0.9.5.jar
pagehelper-5.0.0.jar
如果是maven项目 就添加下面依赖
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.0.0</version>
</dependency>
二、在mybatis-config.xml中添加配置
<plugins> <plugin interceptor="com.github.pagehelper.PageInterceptor"> <property name="reasonable" value="true"/> </plugin> </plugins>
reasonable:分页合理化参数,默认值为false。当该参数设置为 true 时,pageNum<=0 时会查询第一页,pageNum>pages(超过总数时),会查询最后一页。默认false 时,直接根据参数进行查询
三、代码部分
3.1 实体类代码
import java.util.Date; public class Mobile { private Integer id; private String mobiletype; private String mobiledesc; private Date ontime; private Double price; private String picpath; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getMobiletype() { return mobiletype; } public void setMobiletype(String mobiletype) { this.mobiletype = mobiletype == null ? null : mobiletype.trim(); } public String getMobiledesc() { return mobiledesc; } public void setMobiledesc(String mobiledesc) { this.mobiledesc = mobiledesc == null ? null : mobiledesc.trim(); } public Date getOntime() { return ontime; } public void setOntime(Date ontime) { this.ontime = ontime; } public Double getPrice() { return price; } public void setPrice(Double price) { this.price = price; } public String getPicpath() { return picpath; } public void setPicpath(String picpath) { this.picpath = picpath == null ? null : picpath.trim(); } }
3.2 dao层
3.21 接口
import com.hpit.domain.Mobile; import com.hpit.domain.MobileExample; import java.util.List; import org.apache.ibatis.annotations.Param; public interface MobileMapper { List<Mobile> selectByMobile(@Param("mobile") Mobile mobile,@Param("mobiletype") String mobiletype); }
3.22mapper文件
<?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.hpit.dao.MobileMapper"> <resultMap id="BaseResultMap" type="com.hpit.domain.Mobile"> <id column="id" jdbcType="INTEGER" property="id" /> <result column="mobiletype" jdbcType="VARCHAR" property="mobiletype" /> <result column="mobiledesc" jdbcType="VARCHAR" property="mobiledesc" /> <result column="ontime" jdbcType="TIMESTAMP" property="ontime" /> <result column="price" jdbcType="DOUBLE" property="price" /> <result column="picpath" jdbcType="VARCHAR" property="picpath" /> </resultMap> <select id="selectByMobile" resultMap="BaseResultMap"> select distinct id, mobiletype, mobiledesc, ontime, price, picpath from mobile where 1=1 <if test="mobiletype !='' and mobiletype !=null" > and mobiletype like CONCAT(TP_REPLACE(#{mobiletype}),'%') ESCAPE '/' </if> order by id </select> </mapper>
3.3 service层
3.31 接口
import java.util.List; import com.hpit.domain.Mobile; public interface MobileService { List<Mobile> selectByMobile(Mobile mobile,String mobiletype); }
3.32 接口实现类
import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.hpit.dao.MobileMapper; import com.hpit.domain.Mobile; import com.hpit.domain.MobileExample; import com.hpit.service.MobileService; @Transactional @Service public class MobileServiceImpl implements MobileService{ @Autowired private MobileMapper mobileMapper; public List<Mobile> selectByMobile(Mobile mobile,String mobiletype) { return mobileMapper.selectByMobile(mobile,mobiletype); } }
3.3 controller层
public String list(@RequestParam(value="pageNum",defaultValue="1")Integer pageNum,String mobiletype, Model model){ Mobile mobile=new Mobile(); int pageSize=2; PageHelper.startPage(pageNum, pageSize); //传入pageNum, pageSize if (null!=mobiletype && !mobiletype.equals("")){ mobile.setMobiletype(mobiletype); } List<Mobile> list=mobileService.selectByMobile(mobile, mobiletype); PageInfo<Mobile> pageInfo=new PageInfo<Mobile>(list); //将查询的集合传给pageInfo model.addAttribute("pageInfo", pageInfo); model.addAttribute("pageNum", pageNum); model.addAttribute("mobiletype", mobiletype); return "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" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<!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>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-1.8.0.min.js"></script>
<script type="text/javascript">
function do_page(pageNo){
$("#pageNum").val(pageNo);
$("#form1").submit();
}
</script>
</head>
<body>
<div style="width:1000px; text-align:center">
<h3>手机信息</h3>
</div>
<div style="width:1000px; text-align:center">
<form action="${pageContext.request.contextPath}/mobile/list.html" method="post" id="form1">
<input name="pageNum" type="hidden" id="pageNum" value="1" />
手机型号
<label>
<input type="text" name="mobiletype" />
</label>
<label>
<input name="Search" type="submit" id="Search" value="搜索" />
</label>
</form>
</div>
<div style="width:1000px; text-align:center">
<a href="${pageContext.request.contextPath}/add.jsp">添加</a>
</div>
<table width="1000" border="1">
<tr>
<td>编号</td>
<td>类型</td>
<td>上架时间</td>
<td>价格</td>
<td>图片路径</td>
<td>图片</td>
<td>删除</td>
<td>修改</td>
<td>详细</td>
<td>下载</td>
</tr>
<c:if test="${not empty pageInfo.list}">
<c:forEach items="${pageInfo.list}" var="mobile">
<tr>
<td>${mobile.id}</td>
<td>${mobile.mobiletype}</td>
<td><fmt:formatDate value="${mobile.ontime}" pattern="yyyy-MM-dd"/></td>
<td>${mobile.price}</td>
<td>${mobile.picpath}</td>
<td><img alt="" src="${pageContext.request.contextPath}/upload/${mobile.picpath}"></td>
<td><a href="${pageContext.request.contextPath}/mobile/${mobile.id}/delete.html">删除</a></td>
<td><a href="${pageContext.request.contextPath}/mobile/${mobile.id}/update.html">修改</a></td>
<td><a href="${pageContext.request.contextPath}/mobile/${mobile.id}/view.html">详细</a></td>
<td><a href="${pageContext.request.contextPath}/mobile/${mobile.id}/download.html">下载</a></td>
</tr>
</c:forEach>
</c:if>
<tr>
<td colspan="10" align="center">
<a href="javascript:do_page(1)">首页 </a>
<a href="javascript:do_page(${pageInfo.prePage})">上一页 </a>
<a href="javascript:do_page(${pageInfo.nextPage})">下一页 </a>
<a href="javascript:do_page(${pageInfo.lastPage})">尾页 </a>
${pageInfo.pageNum}/${pageInfo.pages}页
<label>
<input name="pageNo" type="text" id="pageNo" size="5" />
<input name="btnGo" type="button" id="btnGo" value="Go" />
</label></td>
</tr>
</table>
</body>
</html>
简单的效果图如下