亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

自己動手寫的mybatis分頁插件(極其簡單好用)

 更新時間:2016年11月12日 14:35:30   作者:李小拐  
最近做了個項目,需要用到mybatis分頁功能,網(wǎng)上找了很多插件,都不太合適,于是就自己動手寫了個mybatis分頁插件功能,非常不錯,代碼簡單易懂,需要的朋友參考下吧

剛開始項目,需要用到mybatis分頁,網(wǎng)上看了很多插件,其實實現(xiàn)原理基本都大同小異,但是大部分都只給了代碼,注釋不全,所以參考了很多篇文章(每篇文章偷一點代碼,評出來自己的,半抄襲),才自己模仿著寫出了一個適合自己項目的分頁插件,話不多說,直接上代碼,相比大部分文章,注釋算很完整了

最重要的攔截器

package com.dnkx.interceptor; 
import java.sql.*; 
import java.util.HashMap; 
import java.util.Properties; 
import org.apache.ibatis.executor.resultset.ResultSetHandler; 
import org.apache.ibatis.executor.statement.StatementHandler; 
import org.apache.ibatis.mapping.BoundSql; 
import org.apache.ibatis.mapping.MappedStatement; 
import org.apache.ibatis.plugin.Interceptor; 
import org.apache.ibatis.plugin.Intercepts; 
import org.apache.ibatis.plugin.Invocation; 
import org.apache.ibatis.plugin.Plugin; 
import org.apache.ibatis.plugin.Signature; 
import org.apache.ibatis.reflection.MetaObject; 
import org.apache.ibatis.reflection.SystemMetaObject; 
import com.dnkx.pojo.Page; 
/** 
* 
* 分頁攔截器,用于攔截需要進行分頁查詢的操作,然后對其進行分頁處理。 
* 利用攔截器實現(xiàn)Mybatis分頁的原理: 
* 要利用JDBC對數(shù)據(jù)庫進行操作就必須要有一個對應的Statement對象,Mybatis在執(zhí)行Sql語句前就會產生一個包含Sql語句的Statement對象,而且對應的Sql語句 
* 是在Statement之前產生的,所以我們就可以在它生成Statement之前對用來生成Statement的Sql語句下手。在Mybatis中Statement語句是通過RoutingStatementHandler對象的 
* prepare方法生成的。所以利用攔截器實現(xiàn)Mybatis分頁的一個思路就是攔截StatementHandler接口的prepare方法,然后在攔截器方法中把Sql語句改成對應的分頁查詢Sql語句,之后再調用 
* StatementHandler對象的prepare方法,即調用invocation.proceed()。 
* 對于分頁而言,在攔截器里面我們還需要做的一個操作就是統(tǒng)計滿足當前條件的記錄一共有多少,這是通過獲取到了原始的Sql語句后,把它改為對應的統(tǒng)計語句再利用Mybatis封裝好的參數(shù)和設 
* 置參數(shù)的功能把Sql語句中的參數(shù)進行替換,之后再執(zhí)行查詢記錄數(shù)的Sql語句進行總記錄數(shù)的統(tǒng)計。 
* 
* 解釋一下插件中可能要用到的幾個類: 
* MetaObject:mybatis提供的一個基于返回獲取屬性值的對象的類 
* BoundSql : 在這個里面可以獲取都要執(zhí)行的sql和執(zhí)行sql要用到的參數(shù) 
* MappedStatement : 這個可以得到當前執(zhí)行的sql語句在xml文件中配置的id的值 
* RowBounds : 是mybatis內存分頁要用到的。 
* ParameterHandler : 是mybatis中用來替換sql中?出現(xiàn)的值的. 
* 
* @author 李小拐 2016年11月9日 10:59:04 
*/ 
@Intercepts({ 
@Signature(type=StatementHandler.class,method="prepare",args={Connection.class}), 
@Signature(type = ResultSetHandler.class, method = "handleResultSets", args = {Statement.class}) 
}) 
public class PageInterceptor implements Interceptor{ 
//攔截分頁關鍵字 
private static final String SELECT_ID="page"; 
//插件運行的代碼,它將代替原有的方法,要重寫最重要的intercept了 
@Override 
public Object intercept(Invocation invocation) throws Throwable { 
if (invocation.getTarget() instanceof StatementHandler) { 
//這里我們有一個設定 如果查詢方法含有Page 就進行分頁 其他方法無視 
//所以就要獲取方法名 
StatementHandler statementHandler=(StatementHandler)invocation.getTarget(); 
MetaObject metaObject=SystemMetaObject.forObject(statementHandler); 
MappedStatement mappedStatement=(MappedStatement)metaObject.getValue("delegate.mappedStatement"); 
String selectId=mappedStatement.getId(); 
String methorName=selectId.substring(selectId.lastIndexOf(".")+1).toLowerCase(); 
//然后判斷下 如果含有Page 就獲取sql 
if(methorName.contains(SELECT_ID)){ 
BoundSql boundSql=(BoundSql)metaObject.getValue("delegate.boundSql"); 
//分頁參數(shù)作為參數(shù)對象parameterObject的一個屬性 
String sql=boundSql.getSql(); 
System.out.println("獲取到的sql:"+sql); 
HashMap<String, Object> map=(HashMap<String, Object>)(boundSql.getParameterObject()); 
//Page page=(Page)(boundSql.getParameterObject()); 
Page page=(Page)map.get("page"); 
// 重寫sql 
String countSql=concatCountSql(sql); 
String pageSql=concatPageSql(sql,page); 
// System.out.println("重寫的 count sql :"+countSql); 
System.out.println("重寫的 select sql :"+pageSql); 
Connection connection = (Connection) invocation.getArgs()[0]; 
PreparedStatement countStmt = null; 
ResultSet rs = null; 
int totalCount = 0; 
try { 
countStmt = connection.prepareStatement(countSql); 
rs = countStmt.executeQuery(); 
if (rs.next()) { 
totalCount = rs.getInt(1); 
} 
} catch (SQLException e) { 
System.out.println("Ignore this exception"+e); 
} finally { 
try { 
rs.close(); 
countStmt.close(); 
} catch (SQLException e) { 
System.out.println("Ignore this exception"+ e); 
} 
} 
metaObject.setValue("delegate.boundSql.sql", pageSql); 
//綁定count 
page.setNumCount(totalCount); 
} 
} 
return invocation.proceed(); 
} 
// 攔截類型StatementHandler,重寫plugin方法 
@Override 
public Object plugin(Object target) { 
if (target instanceof StatementHandler) { 
return Plugin.wrap(target, this); 
}else { 
return target; 
} 
} 
@Override 
public void setProperties(Properties properties) { 
} 
//改造sql 
public String concatCountSql(String sql){ 
//StringBuffer sb=new StringBuffer("select count(*) from "); 
/*sql=sql.toLowerCase(); 
if(sql.lastIndexOf("order")>sql.lastIndexOf(")")){ 
sb.append(sql.substring(sql.indexOf("from")+4, sql.lastIndexOf("order"))); 
}else{ 
sb.append(sql.substring(sql.indexOf("from")+4)); 
}*/ 
StringBuffer sb=new StringBuffer(); 
sql=sql.toLowerCase(); 
if(sql.lastIndexOf("order")>0){ 
sql=sql.substring(0,sql.indexOf("order")); 
} 
sb.append("select count(*) from ("+sql+") tmp"); 
return sb.toString(); 
} 
public String concatPageSql(String sql,Page page){ 
StringBuffer sb=new StringBuffer(); 
sb.append(sql); 
sb.append(" limit ").append(page.getPageBegin()).append(" , ").append(page.getPageSize()); 
return sb.toString(); 
} 
} 
分頁對象Page類
[java] view plain copy
package com.dnkx.pojo; 
import java.util.HashMap; 
import java.util.Map; 
/** 
* 
* 分頁查詢輔助類 
* @author 李小拐 2016年11月9日 13:55:37 
*/ 
public class Page { 
//----------分頁----------- 
private int pageSize;//每頁顯示條數(shù) 
private int pageCurrentPage;//第幾頁 
private int pageBegin;//開始位置 
private int numCount;//總條數(shù) 
private int pageTotal;//總條數(shù) 
private String orderField = "";//控制排序頁面顯示的 
private String orderDirection = ""; 
public Page(){ 
} 
public Page(int pageSize, int pageCurrentPage) { 
super(); 
this.pageSize = pageSize; 
this.pageCurrentPage = pageCurrentPage; 
} 
public Page(Map<String, String> map){ 
if(map.get("pageNum")!=null){ 
this.setPageCurrentPage(this.pageCurrentPage = Integer.parseInt(map.get("pageNum")));//要查詢的頁數(shù) 
}else{ 
this.setPageCurrentPage(1);//設置初始值 
} 
if(map.get("numPerPage")!=null){ 
this.setPageSize(Integer.parseInt(map.get("numPerPage")));//每頁顯示條數(shù) 
}else{ 
this.setPageSize(5);//設置初始值 
} 
if(map.get("orderField")!=null){ 
this.setOrderField(map.get("orderField")); 
} 
if(map.get("orderDirection")!=null){ 
this.setOrderDirection(map.get("orderDirection")); 
} 
} 
public int getPageCurrentPage() { 
return pageCurrentPage; 
} 
public void setPageCurrentPage(int pageCurrentPage) { 
this.pageCurrentPage = pageCurrentPage; 
} 
public int getNumCount() { 
return numCount; 
} 
public void setNumCount(int numCount) { 
this.numCount = numCount; 
} 
public int getPageTotal() { 
return (numCount%pageSize>0)?(numCount/pageSize+1):(numCount/pageSize); 
} 
public void setPageTotal(int pageTotal) { 
this.pageTotal = pageTotal; 
} 
public int getPageSize() { 
return pageSize; 
} 
public void setPageSize(int pageSize) { 
this.pageSize = pageSize; 
} 
public int getPageBegin() { 
return pageSize*(pageCurrentPage-1); 
} 
public void setPageBegin(int pageBegin) { 
this.pageBegin = pageBegin; 
} 
public String getOrderField() { 
return orderField; 
} 
public void setOrderField(String orderField) { 
this.orderField = orderField; 
} 
public String getOrderDirection() { 
return orderDirection; 
} 
public void setOrderDirection(String orderDirection) { 
this.orderDirection = orderDirection; 
} 
public static Page getPage(int pageSize, int pageCurrentPage){ 
return new Page(pageSize,pageCurrentPage); 
} 
public static Page getPage(Map map){ 
return new Page(map); 
} 
}

Controller里面調用方式

public String list(HttpServletRequest request) { 
long a=System.currentTimeMillis(); 
HashMap<String,Object> map=GetRequestMap.getMap(request);//自己封裝的方法,取request的參數(shù) 
Page page= Page.getPage(map);//初始化page 
map.put("page", page);//把page對象放入?yún)?shù)集合(這個map是mybatis要用到的,包含查詢條件,排序,分頁等) 
//控制排序頁面顯示的 
map.put(map.get("orderField")+"", map.get("orderDirection")); 
List<Employee> list=employeeService.getListPage(map); 
request.setAttribute("emlist", list); 
request.setAttribute("page", page); 
request.setAttribute("map", map); 
//取page相關屬性 
page.getNumCount();//總條數(shù) 
page.getPageTotal();//總頁數(shù) 
long b=System.currentTimeMillis(); 
System.out.println("---------耗時:"+(b-a)+"ms"); 
return "basic/employee_list"; 
}

最后,spring里面配置插件

<bean id="PageInterector" class="com.dnkx.interceptor.PageInterceptor"></bean> 
<!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 --> 
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 
<property name="dataSource" ref="dataSource" /> 
<!-- 自動掃描mapping.xml文件 --> 
<property name="mapperLocations" value="classpath:com/dnkx/mapping/*.xml"></property> 
<property name="plugins"> 
<ref bean="PageInterector"/> 
</property> 
</bean>

好了,到此結束,本文僅供參考!也期待大神提意見

相關文章

最新評論