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