首先去Maven倉庫(http://mvnrepository.com)下載pager-taglib.jar包,它的官網貌似掛掉了.css
固然了,說到分頁,固然得創建分頁bean,以下圖所示:html
而後創建存放分頁請求參數的類SystemContext,該類中用到了ThreadLocal,它是一個跟當前線程相關的Map,它的key就是當前線程的名字java
接着配置SystemContextFilter的過濾器,對全部請求都攔截.web
1 <filter> 2 <filter-name>SystemContextFilter</filter-name> 3 <filter-class>com.scitc.page.web.SystemContextFilter</filter-class> 4 </filter> 5 <filter-mapping> 6 <filter-name>SystemContextFilter</filter-name> 7 <url-pattern>/*</url-pattern> 8 </filter-mapping>
在SystemContextFilter中對頁面請求參數進行處理,放入ThreadLocal中.apache
在DAO層,進行分頁處理session
1 package com.scitc.page.dao; 2 3 import java.util.HashMap; 4 import java.util.List; 5 import java.util.Map; 6 7 import org.apache.ibatis.session.SqlSession; 8 9 import com.scitc.page.domain.Pager; 10 import com.scitc.page.domain.User; 11 import com.scitc.page.util.MyBatisUtil; 12 import com.scitc.page.util.SystemContext; 13 14 public class UserDao { 15 public Pager<User> find(String name) { 16 17 Map<String, Object> params = new HashMap<String, Object>(); 18 if (name != null && !name.equals("")) 19 params.put("name", "%" + name + "%"); 20 int pageSize = SystemContext.getPageSize(); 21 int pageOffset = SystemContext.getPageOffset(); 22 String order = SystemContext.getOrder(); 23 String sort = SystemContext.getSort(); 24 Pager<User> pages = new Pager<User>(); 25 SqlSession session = null; 26 try { 27 session = MyBatisUtil.createSession(); 28 params.put("pageSize", pageSize); 29 params.put("pageOffset", pageOffset); 30 params.put("sort", sort); 31 params.put("order", order); 32 List<User> datas = session.selectList(User.class.getName()+ ".find", params); 33 pages.setDatas(datas); 34 pages.setPageOffset(pageOffset); 35 pages.setPageSize(pageSize); 36 int totalRecord = session.selectOne(User.class.getName()+".find_count", params); 37 pages.setTotalRecord(totalRecord); 38 } finally { 39 MyBatisUtil.closeSession(session); 40 } 41 return pages; 42 } 43 }
Mybatis中的SQL配置mybatis
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE mapper 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 5 6 <mapper namespace="com.scitc.page.domain.User"> 7 8 <!-- 注意namespace不是別名,User的別名在mybatis.config.xml中的<typeAliases> --> 9 10 <insert id="add" parameterType="User"> 11 INSERT INTO t_user (username,password,nickname) 12 VALUES(#{username},#{password},#{nickname}) 13 </insert> 14 <delete id="delete" parameterType="int"> 15 delete from t_user where id=#{id} 16 </delete> 17 <update id="update" parameterType="User"> 18 update t_user set username=#{username} where id=#{id} 19 </update> 20 <select id="load" parameterType="int" resultType="User"> 21 select * from t_user where id=#{id} 22 </select> 23 <!-- List的返回結果仍可爲User --> 24 <select id="list" resultType="User"> 25 select * from t_user 26 </select> 27 <!-- #{sx}會使用?進行替代 ,$(xx)會完整將字符串完成替代--> 28 <select id="find" resultType="User" parameterType="map"> 29 select * from t_user 30 <!-- 動態SQL --> 31 <where> 32 <if test="name!=null"> (username like #{name} or nickname like #{name})</if> 33 </where> 34 35 <if test="sort!=null"> 36 order by ${sort} 37 <if test="order!=null"> 38 ${order} 39 </if> 40 </if> 41 limit #{pageOffset},#{pageSize} 42 </select> 43 44 <select id="find_count" parameterType="map" resultType="int"> 45 select count(*) from t_user 46 <if test="name!=null">where (username like #{name} or nickname like #{name})</if> 47 </select> 48 </mapper>
爲何咱們要使用ThreadLocal呢?app
主要是對跨層數據傳輸的處理,前面也提到了它是一個當前線程Map,因此使得跨層也能夠傳輸數據框架
好了,pager-taglib暫時說到這裏,接下來講說sitemeshdom
什麼是sitemesh呢?
它是一個網頁佈局和修飾的框架,利用它能夠將網頁的內容和頁面結構分離,以達到頁面結構共享的目的。
首先下載sitemesh的jar包,下載方法能夠去官網,也能夠去Maven倉庫
接下來配置sitemesh的過濾器
1 <filter> 2 <filter-name>sitemesh</filter-name> 3 <filter-class>com.opensymphony.sitemesh.webapp.SiteMeshFilter</filter-class> 4 </filter> 5 6 <filter-mapping> 7 <filter-name>sitemesh</filter-name> 8 <url-pattern>/*</url-pattern> 9 </filter-mapping>
模版頁面的配置
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator" %> 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 5 <html> 6 <head> 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 8 <title><decorator:title default="歡迎使用xxx系統"/></title> 9 <link rel="stylesheet" type="text/css" href="<%=request.getContextPath()%>/css/main.css"/> 10 <decorator:head/> 11 </head> 12 <body> 13 <a href="">其餘操做</a> 14 <hr/> 15 <h3 align="center"><decorator:title default="詳細信息"/></h3> 16 <decorator:body/> 17 <hr/> 18 <div align="center" class="distance"> 19 55555555 20 </div> 21 <div align="center" class="footer"> 22 CopyRight@2012-2015<br/> 23 scitc 24 </div> 25 </body> 26 </html>
最後效果如圖