1. [代码] mybatis 全局配置文件
<плагины> <плагин receptor = "com.has.core.page.paginationInterceptor" /> < /plugins>
2. [文件] PaginationInterceptor.java
Intercepts ({@signature (type = atportion handler. Class, method = "prepare", args = {connection. Class})}) public class paginationInterceptor реализует Interceptor {@OverridePublic Object Intercept (vlocation vlock) throwable {ratectionhandler athirtler = (ratportHandler) vocation.getTarget (); atputionHandler.getBoundsql ();; MetaObject metaTatementHandler = metaObject.forObject (athugeHandler); Rowbounds rowbounds = (rowbounds) metastementHandler.getValue ("Delegate.rowbounds"); if (rowbounds == null || rowbounds == rowbounds); vlocation.proceedceed ();} configuration configuration = (configuration) metastatementHandler.getValue ("delegate.configuration"); dialect.type databasetype = null; try {databasetype = dialect.type.valueof (configuration.getVariables (). e) {} if (databasetype == null) {throw new Runtimeexception («Значение свойства диалекта в configuration.xml не определено:"+ configuration.getvariables (). getProperty ("Dialect"));} dialect = null; switchAbaseType) {case MySclect = nallectecle = null; switchAbaseType) {case mySclEct = natelecteceect = nall; ; case oracle: dialect = new Oracledialect (); Break;} string inoryalsql = (string) metastatementhandler.getvalue ("delegate.boundsql.sql"); metastatementhandler.setValue ("delegate.boundsql.sql", dialect.getlimitstring ("aryboundsql.sql", dialect.getlimitstring (originallesseblefoft. rowbounds.getlimit ())); metastatementhandler.setValue ("delegate.rowbounds.offset", rowbounds.no_row_offset); metastementHandler.setValue ("delegate.rowbounds.limit", rowbounds.no_row_limit); вернуть velocation.procecept stocults. Target) {return plugin.wrap (target, this);}@overridePublic void setProperties (свойства свойств) {}}3. [文件] Dialect.java
/*** 数据库方言定义 **/Общедоступный абстрактный класс Диалект {public static enum type {mysql, oracle} public Abstract String getLimitsTring (String SQL, int skipresults, int maxresults);}4. [文件] mysql5dialect.java
/*** mysql 方言分页类*/public class mysql5dialect extends dialect {защищенная статическая конечная строка sql_end_delimiter = ";" ; public String getLimitsTring (String SQL, Boolean Hasoffset) {return mysql5pageHepler.getLimitsTring (sql, - 1, - 1);} public String getLimitString (String sql, int offset, int limit) {return mysql5pageHepler.getLimitString (sepspet, offset); supportslimit () {return true;}}5. [文件] oracledialect.java
Пакет com.chyjr.has.core.page.dialect;/*** oracel 方言分页**/public class oracledialect extends dialect {public String getLimitsTring (String sql, int смещение, int limit) {// todo 未实现 return "";}}6. [文件] mysql5pageHepler.java
Импорт java.util.regex.matcher; import java.util.regex.pattern;/*** mysql 分页工具类*/public class mysql5pageHepler {/*** 得到查询总数的 sql*/public static String getCountstring (String QuerySelect) {querySelect = getLineseclectexelect); getLastOrderInsertPoint (QuerySelect); int fromIndex = getaFterFormInsErtPoint (QuerySelect); String Select = QuerySelect.substring (0, formindex); // 如果 Select 中包含 Disful 只能在外层包含 countif (select.tolowercase (). indexof («select») = - 1 || ! = - 1) {вернуть new StringBuffer (querySelect.length ()). Append («Выбрать счет (1) count for (") .append (queryselect.substring (0, orderindex)). Append (") t") .toString ();} else {return new Stringbuffer (QuerySelectectectectectectectect. ) .append (querySelect.subString (formindex, orderindex)). toString ();}}/*** 得到最后一个 Порядок по 的插入点位置 ** @return 返回最后一个 Порядок 插入点的位置*/private int int getLoderOrderInsertPoint (String QuerySelect) {intorderIndex = QuerSelect.toloverSexCaint (). 1 ||. getLimitsTring (String QuerySelect, int Offset, int Limit) {QuerySelect = getLinesQl (QuerySelect); // string sql = QuerySelect.ReplAceall («[^// s,] + //. Limit; return SQL;}/*** 将 SQL 语句变成一条语句 , 并且每个单词的间隔都是 1 个空格 ** @Param SQL* SQL 语句* @return 如果 SQL 是 NULL 返回空 , 否则返回转化后的 SQL*/Private Static String GetLinesQl (String SQL) {return SQL.Replacealll ("[/r/n]", ",". "// s {2,}", "");}/*** 得到 sql 第一个正确的 из 的的插入点*/private static int getafterforminsertpoint (string queryselect) {string regex = "// s+from // s+"; patterd = pattern.compile (regex, pattern.come_insensitive); (matcher.find ()) {int formstartIndex = matcher.start (0); string text = QuerySelect.subString (0, fromStartIndex); if (isbracketCanpartnership (text)) {return fromStartIndex;}} return 0;}/*** 判断括号 "()", 并不会判断排列顺序是否正确 并不会判断排列顺序是否正确 ** @param* @aterurnex如果匹配返回 TRUE, 否则返回 FALSE*/Private Static Boolean IsbracketCanpartnership (String Text) {if (text == null || (getIndexofcount (text, '(')! getIndexOfCount (String Text, char ch) {int count = 0; for (int i = 0; i <text.length (); i ++) {count = (text.charat (i) == ch)? count + 1: count;} return count;}}2. [图片] mybatis.jpg