entiy
public class PageParam { private int currPage ; // 当前页 private int totalPage ; // 总页 private int rowCount ; // 总记录数 public static int pageSize = 10; // 页大小 private List<TableIp> data ; // 数据 public int getCurrPage() { return currPage; } public void setCurrPage(int currPage) { this.currPage = currPage; } public int getTotalPage() { return totalPage; } public void setTotalPage(int totalPage) { this.totalPage = totalPage; } public int getRowCount() { return rowCount; } public void setRowCount(int rowCount) { int totalPage = rowCount / pageSize; if (rowCount % pageSize > 0) { totalPage += 1; } setTotalPage(totalPage); this.rowCount = rowCount; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public List<TableIp> getData() { return data; } public void setData(List<TableIp> data) { this.data = data; } }
Dao Interface
@Repository public interface ITableIpDao { int getRowCount(); List<TableIp> selectByParams(Map<String, Object> params); List<TableIp> fetchByParams(Map<String, Object> params); }
mybatis.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"> <!-- namespace必须指向Dao接口 --> <mapper namespace="com.jikexueyuan.demo.dao.ITableIpDao"> <select id="getRowCount" resultType="int"> select count(*) from table_ip </select> <select id="selectByParams" resultType="com.jikexueyuan.demo.entity.TableIp" parameterType="map"> select * from table_ip order by findTime desc limit ${offset}, ${size} </select> <select id="fetchByParams" resultType="com.jikexueyuan.demo.entity.TableIp" parameterType="map"> select * from table_ip where 1=1 <if test="country != null and country != ''"> and country = '${country}' </if> <if test="isp != null and isp != ''"> and isp = '${isp}' </if> limit ${size} </select> </mapper>
service
@Service public class TableIpService { @Resource ITableIpDao dao ; public int getRowCount() { return dao.getRowCount(); } public PageParam getIpListByPage(PageParam pageParam) { int currPage = pageParam.getCurrPage(); // limit offset, size int offset = (currPage - 1) * PageParam.pageSize ; int size = PageParam.pageSize; Map<String, Object> params = new HashMap<String, Object>(); params.put("offset", offset); params.put("size", size); List<TableIp> ipList = dao.selectByParams(params); pageParam.setData(ipList); return pageParam; } public String printIp(String number, String country, String isp) { int size = 100; try { size = Integer.parseInt(number); } catch (Exception e) { } Map<String, Object> params = new HashMap<String, Object>(); params.put("size", size); params.put("country", country); params.put("isp", isp); List<TableIp> ipList = dao.fetchByParams(params); StringBuilder sBuilder = new StringBuilder(); for (TableIp tableIp : ipList) { sBuilder.append(tableIp.getIp()).append(":").append(tableIp.getPort()).append(",").append(tableIp.getCountry()).append(",").append(tableIp.getIsp()); sBuilder.append("\r\n"); } return sBuilder.toString(); } }
controller
@Controller public class IndexController { @Resource TableIpService service; @RequestMapping(value = "index") public String helloWorld(HttpServletRequest request){ String currPageStr = request.getParameter("page"); int currPage = 1; try { currPage = Integer.parseInt(currPageStr); } catch (Exception e) { } // 获取总记录数 int rowCount = service.getRowCount(); PageParam pageParam = new PageParam(); pageParam.setRowCount(rowCount); if (pageParam.getTotalPage() < currPage) { currPage = pageParam.getTotalPage(); } pageParam.setCurrPage(currPage); pageParam = service.getIpListByPage(pageParam); request.setAttribute("pageParam", pageParam); return "index"; } }
jsp
<tbody> <c:forEach items="${pageParam.data }" var="item"> <tr> <td>${item.ip }</td> <td>${item.port }</td> <td>${item.country }</td> <td>${item.province } ${item.city }</td> <td>${item.isp }</td> <td>${item.findTime }</td> </tr> </c:forEach> </tbody> <div> <span>第</span> <% PageParam pageParam = (PageParam)request.getAttribute("pageParam"); int currPage = pageParam.getCurrPage(); int totalPage = pageParam.getTotalPage(); for(int i = 1; i <= totalPage; i ++){ if(i == currPage){ %><span class="current"><%=currPage %></span><% }else{ %><a href="index.html?page=<%=i %>"><%=i %></a><% } } %> <span>页</span> </div>