1。 [代碼] Mybatis全局配置文件
<plugins> <插件Interceptor =“ com.has.core.page.pagination interceptor” /> < /plugins>
2。 [文件] pagination Interceptor.java
@intercepts({@signature(type = statementHandler。class,方法=“ prepard”,args = {connect.class})})public class class pagination pagination interpector interceptor {@overridepublic object intercept(Invocation indocation(Invocation) statementHandler.getBoundSQL(); metaObject metastatementHandler = metaObject.forObject(statementHandler); rowbounds rowbounds =(rowbounds)metastatementhandler.getValue(“ delegate.rowbounds”); if(rowbounds == == null | Invocation.proceed();}配置配置=(配置)metastatementHandler.getValue(“ delegate.configuration”); dialect.type databaseType = null; try {databasetype = dialect = dialect = dialect.type.valueof(configuration.geturation.getvariation.getvariableables.getpropproperty() ).toupperCase());} catch(異常e){} if(databaseType == null){purph new RuntimeException(“ configuration.xml中的方言屬性的值未定義:”+ configuration.getVariobles.getVariobles()。getVariobles()。 mySQL:dileect = new mySql5Dialect(); break; case oracle:solect = new oracledialect(); break;} string ointerssql =(string)metastatementHandler.getValue(“ delegate.boundsql.sql.sql.sql”) ,dialect.getLimitString(onirstanSsql,rowbounds.getoffset(),rowbounds.getLimit()); metastatementhandler.setValue(“ delegate.rowbounds.offs.offs.offs.offse”,rowbounds.no_row_row_offsect invocation.proceed();}@overridePublic object插件(object target){return plugin.wrap(target,this);}@overridePublic void setProperties(properties properties {}}}3。 [文件]方言Java
/***數據庫方言定義
4。 [文件] mysql5dialect.java
/*** mysql方言分頁類*/公共類mysql5dialect擴展了方言{受保護的靜態最終字符串SQL_END_DELIMITER =“;” ; public string string getLimitString(string sql,boolean hasOffset){返回mysql5pageHepler.getLimitString(sql,-1,-1);} public string string string string getLimitString(string sql,int Offset,int Offset,int limit,int limit) {返回true;}}5。 [文件] oracledialect.java
軟件包com.chyjr.has.core.page.dialect;/*** oracel方言分頁**/public class class ocledialect擴展方言{public String getLimitString(字符串sql,int offset,int offset,int limit){//6。 [文件] mysql5pagehepler.java
導入java.util.regex.matcher;導入java.util.regex.pattern;/*** mysql分頁工具類*/public class mysql5pageHepler {/*** sql*/public static static string getCountString getCountString(querySelect) getLastOrderInsertpoint(querySelect); int formIndex = getAfterFormInSertPoint(querySelect); string select select = querySelect.SubString(0,FormIndex); // select select select lintical listical listical countif(select.tolowercase(select.tolowercase(select.tolowercase(select.tolowercase(select.tolowercase()。 1){返回新StringBuffer(querySelect.length())。append(“ select count(1)從(“”).append(querySelect.Substring(0,orderIndex)).append(orderIndex)。 ).append(querySelect.substring(formIndex,orderIndex))。 ||。 getLimitString(字符串querySelect,int Offset,int limit){querySelect = getLinesQl(querySelect); // sql = querySelect.replaceAll(“ [^// s,s,] + //。 sql;}/***將sql語句變成一條語句,1個空格** @param sql* sql* @return @return sql sql sql null返回空,sql*/private static strip String getLinesql(string sql sql sql) “ // s {2,}”,“”);}/***得到sql第一個正確的(matcher.find()){int fromstartIndex = matcher.start(0);字符串text = querySelect.substring(0,fromstartIndex); if(isbracketCanpartnohiss(text)){returnstartIndex;}}}}}}}}}}}}}}}}}}}如果匹配返回是的,否則返回fals*/私人靜態布爾iSbracketCanpartnersher(字符串文本){if(text == == null ||(getIndexofCount(text,'(')!= getIndexofCount('('(')文本,char ch){int count = 0; for(int i = 0; i <text.length(); i ++){count =(text.charat(i)== ch)?計數 + 1:count;}返回計數;}}}2。 [圖片] mybatis.jpg