1。[代码] mybatis全局配置文件
<プラグイン> <Plugin Interceptor = "com.has.core.page.paginationinterceptor" /> < /plugins>
2。[文件] paginationInterceptor.java
@intercepts({@signature(type =statementhandler。class、method = "prepare"、args = {connection。class})})インターセプター{@overridepublicオブジェクトインターセプター{@overridepublic object intercept(Invocation Invocation)Throws {StatementHandler StatementHandler =(StatementHandlsklsqlsqlsqlsqlsclsget() StatementHandler.getBoundSql(); MetaObject MetastatementHandler = MetaObject.ForObject(StatementHandler); RowBounds RowBounds =(RowBounds)MetastatementHandler.getValue( "Delegate.Rowbounds"); Invocation.Pro Cheys();} Configuration Configuration =(configuration)metastatementhandler.getValue( "Delegate.Configuration"); dialect.type databaseType = null; try {databaseType = dialect.type.valueof(configutation.getVariables()。 e){} if(databaseType == null){throw new runtimeException( "configuration.xmlの方言の値の値は定義されていません:"+ configuration.getVariables()。 mysql5dialect(); break; case oracle:new oracledialect(); break;} string originalsql =(string)metastatementhandler.getValue( "Delegate.BoundSql.sql"); MetastatementHandler.setValue( "Delegate.BoundSQL.GETLIMITSTRING(" rowbounds.getoffset()、rowbounds.getlimit()); metastatementhandler.setValue( "Delegate.rowbounds.offset"、rowbounds.no_row_offset); metastatementhandler.setValue( "Delegate.Rowbounds.limit"、Rowbounds.no_row_limit); Invocation.proceed();}@OverridePublic Object Plugin(Object Target){return plugin.wrap(target、this);}@overridepublic void setProperties(Properties Properties){}}}3。[文件] dierect.java
/***数据库方言定义**/public abstract class方言{public static enum type {mysql、oracle} public abstract string getlimitstring(string sql、int skipresults、int maxresults);}4。[文件] mysql5dialect.java
/*** mysql ; public String getLimitString(String SQL、boolean hasoffset){mysql5pagehepler.getlimittring(sql、-1、-1);} public string getlimitstring(string sql、int offset、int limit){return mysql5pagehepler.getlimitsing(} getlimitstring(sql、limit); supportslimit(){return true;}}5。[文件] oracledialect.java
パッケージcom.chyjr.has.core.page.dialect;/*** oracel方言分页**/public class oracledialectは方言を拡張します{public string getlimitstring(string sql、int offset、int lime){// todo return "";}}}6。[文件] mysql5pagehepler.java
java.util.regex.matcher; import java.util.regex.pattern;/*** mysql getLastOrderInsertPoint(QuerySelect); int formindex = getafterforminsertpoint(querySelect); string select = queryselect.substring(0、formindex); // select中包含countif(select.tolowercase()。 1){new StringBuffer(querySelect.length())。append( "count(1)count(").append(queryselect.substring(0、orderIndex))。 ).Append(querySelect.substring(formindex、orderindex)) 1 ||!isbracketcanpartnership(queryselect.substring(orderindex、queryselect.length())){throw new runtimeexception( "my sql odder by语句!");} getLimitString(String QuerySelect、int offset、int limit){querySelect = getlinesql(querySelect); // string sql = queryselect.replaceall( "[^// s、] + //。 rimit; rimit; rimit; return sql;}/*** sQl "// s {2、}"、 "");}/*** sql第一个正确的*/private static int getafterforminsertpoint(string querySelect){string regex = "// s+from"; pattern pattern = pattern.compile(regex、pattern.case_insivity); matcher matcher = matcher = pattern. (matcher.find()){int fromstartindex = matcher.start(0); string text = queryselect.substring(0、fromstartindex); if(isbracketcanpartnership(text)){return fromstartindex;}} return 0;}/***如果匹配返回true、否则返回false*/private static boolean isbracketcanpartnership(string text){if(text == null ||(getIndexofCount(text、 '(')!= getIndexofCount(text、 ')')){return false;} return true;}/*** @*** text、char ch){int count = 0; count + 1:count;} return count;}}2。[图片] mybatis.jpg