1. [代码] mybatis 全局配置文件
<flugins> <plugin interceptor = "com.has.core.page.paginationIntercept" /> < /plugins>
2. [文件] paginationIntercept.java
@Intercepts ({@signature (type = declarationHandler. Class, method = "preparar", args = {Connection. Class})}) Classe pública PaginationInterceptor implementa interceptor {@OverridePublic Object Intercept (invocação de invocação) lança throwable {DeclarentHandler = (Declarectler) converch.getLegget () LoundSletSl ()) DeclarationHandler.GetBoundSql (); MetaObject MetastatementHandler = metaObject.ForObject (DeclarationHandler); RowBounds RowBounds = (RowBounds) metástasiaHandler.getValue ("DeLegate.RowBounds"); se (rowbounds ==lings | Invocation.proeced ();} Configuração da configuração = (Configuração) metastatementHandler.getValue ("Delegate.configuration"); dialect.type databaseType = null; try {databasetype = dialect.type.valueof (configuration.getVariABS () () e) {} if (databaseType == NULL) {tiro a nova RunTimeException ("O valor da propriedade Dialect em Configuration.xml não é definido:"+ configuration.getVariables (). GetProperty ("Dialect"));} dialect = null; switch (DataTypeType) {Casect "); Oracle: dialect = new oracledialect (); break;} string originalsql = (string) metastatementHandler.getValue ("delegate.boundsql.sql"); metatatementHandler.setVring ("delegate.boundsql.sql", dialect.getInString (originais rowbounds.getLimit ())); metastatementHandler.setValue ("delegate.rowbounds.offset", rowbounds.no_row_offset); metastatementHandler.setValue ("deLegate.rowbounds.limit", rowbounds.no_row_limit); retorno, consultas.Prowbounds.limit ", rowbounds.no_row_limit); retorno, consultas.Prowbounds.limit", rowbounds.no_row_limit); substituição Plugin.wrap (Target, this);}@SubsteridePublic void setProperties (Propriedades Propriedades) {}}3. [文件] dialeto.java
/*** 数据库方言定义 **/public abstract classe dialeto {public static enum tipo {mysql, oracle} public string string getlimitstring (string sql, int skipresults, int maxresults);}4. [文件] mysql5dialect.java
/*** mysql 方言分页类*/classe pública mysql5dialect estende dialeto {string final protegida protegida 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(sql, offset, limit);}public boolean supportsLimit() {return true;}}5. [文件] oracledialect.java
pacote com.chyjr.has.core.page.dialect;/*** oracel 方言分页**/public class oracledialect estende o dialeto {public string getlimitstring (string sql, int offset, int limite) {// TODO 未实现 Return ";}}}}}6. [文件] mysql5pagehepler.java
importar java.util.regex.matcher; importar java.util.regex.pattern;/*** mysql 分页工具类*/public class MySql5PageHepler {/*** 得到查询总数的 sql*/public static string getCountString (string queryselect) {queryselect = GetLinesql getLastOrderInsertPoint (QuerySelect); int formIndex = getAfterformInsertPoint (QuerySelect); String select = querySelect.substring (0, formIndex); // 如果 selecione 中包含 distinto 只能在外层包含 contagemf (select.TolowerCase (). {Retorne novo StringBuffer (QuerySelect.Length ()). Append ("Selecione a contagem (1) contagem de (") .Append (QuerySelect.SubString (0, OrderIndex)). Append (") t") .ToString ();} else {Return New Stringbuffer (REURYSECT.LURNCON). ) .Append (QuerySelect.Substring (formIndex, OrderIndex)). ! IsbracketCanpartnership (QuerySelect.substring (OrderIndex, QuerySelect.Length ()))) {THRON RUNTIMEIXCECCECTION ("MY SQL 分页必须要有 Ordem por 语句!");} Return OrderIndex;}/*** 得到分页的 ql ** @param Offset* 偏移量* @param limit* 位置 位置 位置 位置 位置param; QuerySelect, Int Offset, Int Limit) {QuerySelect = getLinesql (QuerySelect); // String sql = QuerySelect.ReplaceAll ("[^// s,] + //.", "") + "limite" + // Offset +, " + limite; string; sql = sheryselectSet + limite" " + // Offset", " + limite; string;将 sql 语句变成一条语句 , 并且每个单词的间隔都是 1 个空格 ** @param sql* sql 语句* @return 如果 sql 是 null 返回空 , 否则返回转化后的 sql*/string estática privada getLinesql (string sql) {return sql.replaceAll ("[/r/n]", "). );}/*** 得到 sql 第一个正确的 de 的的插入点*/private estático int getAfterFerSertInsToint (string QuerySelect) {string regex = "// s+de // s+"; padring = padring.compile (regex, padrony.case_insensitive); mattherind = padring.matcher (queryselect); Matcher.Start (0); String text = QuerySelect.Substring (0, FromStarTindex); if (IsbracketCanpartnership (text)) {retornar dostartIndex;}} retornar 0;}/*** 判断括号 "()" 是否匹配, 并不会判断排列顺序是否正确 ** @param text* 要判断的文本* @True 如果匹配返回 True, 否则返回, 否则返回 否则返回 否则返回 否则返回 否则返回 @paramat*********************************************** texto) {if (text == null || (getIndexOfCount (text, '(')! text.Length (); contagem + 1: contagem;} contagem de retorno;}}2. [图片] mybatis.jpg