您好,登錄后才能下訂單哦!
Mybatis分頁切入點(diǎn)
Mybatis內(nèi)部有個(gè)plugins(插件)概念,本質(zhì)上屬于攔截器的思想。具體的解析可見他文MyBatis攔截器原理探究。本文將在此基礎(chǔ)上直接展示實(shí)際項(xiàng)目的實(shí)現(xiàn)代碼和其他的相關(guān)解析
分頁具體代碼實(shí)現(xiàn)
首先我們可以定義方言抽象類,用于實(shí)現(xiàn)分頁AbstractDialect.java
public abstract class AbstractDialect{ /** * 是否支持limit和偏移量 * @return */ public abstract boolean supportsLimitOffset(); /** * 是否支持limit * @return */ public abstract boolean supportsLimit(); /** * 獲取增加了分頁屬性之后的SQL * @param sql * @param offset * @param limit * @return */ public abstract String getLimitString(String sql, int offset, int limit); }
再而我們就以O(shè)racle與Mysql數(shù)據(jù)庫的分頁技術(shù)作下分別的實(shí)現(xiàn)
MySQLDialect.java-Mysql分頁方言
public class MySQLDialect extends AbstractDialect { public boolean supportsLimitOffset() { return true; } public boolean supportsLimit() { return true; } public String getLimitString(String sql, int offset, int limit) { if (offset > 0) { return sql + " limit " + offset + "," + limit; } else { return sql + " limit " + limit; } } }
OracleDialect.java-Oracle方言實(shí)現(xiàn)
public class OracleDialect extends ADialect{ @Override public boolean supportsLimitOffset() { return false; } @Override public boolean supportsLimit() { return false; } @Override public String getLimitString(String sql, int start, int limit) { if(start < 0){ start = 0; } if(limit < 0){ limit = 10; } StringBuilder pageSql = new StringBuilder(100); pageSql.append("select * from ( select temp.*, rownum row_id from ( "); pageSql.append(sql); pageSql.append(" ) temp where rownum <= ").append(start+limit); pageSql.append(") where row_id > ").append(start); return pageSql.toString(); } }
對(duì)應(yīng)的Mybatis插件攔截器實(shí)現(xiàn)如下,攔截StatementHandler#prepare(Connection con)創(chuàng)建SQL語句對(duì)象方法
PaginationInterceptor.java
@Intercepts({ @Signature(type = StatementHandler.class, method = "prepare", args = { Connection.class }) }) public final class PaginationInterceptor implements Interceptor { private final static Logger log = LoggerFactory .getLogger(PaginationInterceptor.class); private ADialect dialect; public void setDialect(ADialect dialect) { this.dialect = dialect; } @Override public Object intercept(Invocation invocation) throws Throwable { // 直接獲取攔截的對(duì)象,其實(shí)現(xiàn)類RoutingStatementHandler StatementHandler statementHandler = (StatementHandler) invocation .getTarget(); BoundSql boundSql = statementHandler.getBoundSql(); // 獲取元對(duì)象,主要用于獲取statementHandler所關(guān)聯(lián)的對(duì)象及屬性 MetaObject metaStatementHandler = MetaObject.forObject( statementHandler, new DefaultObjectFactory(), new DefaultObjectWrapperFactory()); MappedStatement mappedStmt= (MappedStatement) metaStatementHandler .getValue("delegate.mappedStatement".intern()); // 只對(duì)queryPagination()方法進(jìn)行分頁操作 if(mappedStmt.getId().indexOf("queryPagination")==-1){ return invocation.proceed(); } // 重新構(gòu)造分頁的sql String originalSql = (String) metaStatementHandler .getValue("delegate.boundSql.sql".intern()); metaStatementHandler.setValue("delegate.boundSql.sql".intern(), dialect .getLimitString(originalSql, rowBounds.getOffset(), rowBounds.getLimit())); metaStatementHandler.setValue("delegate.rowBounds.offset".intern(), RowBounds.NO_ROW_OFFSET); metaStatementHandler.setValue("delegate.rowBounds.limit".intern(), RowBounds.NO_ROW_LIMIT); log.debug("page sql : " + boundSql.getSql()); return invocation.proceed(); } // 攔截對(duì)象 @Override public Object plugin(Object target) { return Plugin.wrap(target, this); } @Override public void setProperties(Properties properties) { } }
Spring對(duì)應(yīng)的xml配置可如下,以oracle分頁為例子
<!-- oracle方言配置,用于oracle的分頁 --> <bean id="paginationInterceptor" class="com.hsnet.winner.cas.admin.core.dao.mybatis.interceptor.PaginationInterceptor"> <property name="dialect"> <bean class="cn.cloud.winner.oss.manager.mybatis.page.OracleDialect" /> </property> </bean>
使用以上的代碼以及配置即可完成對(duì)oracle數(shù)據(jù)庫以及mysql數(shù)據(jù)庫的分頁操作。并且博主對(duì)其中的某個(gè)點(diǎn)作下解析
Mybatis#MetaObject-元數(shù)據(jù)對(duì)象解析
以上的代碼博主之前在使用的時(shí)候,對(duì)其中的MetaObject這個(gè)類很費(fèi)解,其直接通過getValue()方法便可以將所代理的對(duì)象的所關(guān)聯(lián)的屬性全都拿取到。我們可以跟隨源碼深入了解下
MetaObject#forObject()
代理對(duì)象均通過此靜態(tài)方法進(jìn)入
public static MetaObject forObject(Object object, ObjectFactory objectFactory, ObjectWrapperFactory objectWrapperFactory) { if (object == null) { return SystemMetaObject.NULL_META_OBJECT; } else { return new MetaObject(object, objectFactory, objectWrapperFactory); } }
我們可以直接觀察其中的構(gòu)造函數(shù),玄機(jī)就在此處
private MetaObject(Object object, ObjectFactory objectFactory, ObjectWrapperFactory objectWrapperFactory) { this.originalObject = object; this.objectFactory = objectFactory; this.objectWrapperFactory = objectWrapperFactory; // 所有的屬性獲取均通過objectWrapper類來獲取,此處主要對(duì)所代理的object對(duì)象類型進(jìn)行判斷 if (object instanceof ObjectWrapper) { this.objectWrapper = (ObjectWrapper) object; } else if (objectWrapperFactory.hasWrapperFor(object)) { this.objectWrapper = objectWrapperFactory.getWrapperFor(this, object); } else if (object instanceof Map) { this.objectWrapper = new MapWrapper(this, (Map) object); } else if (object instanceof Collection) { this.objectWrapper = new CollectionWrapper(this, (Collection) object); } else { // 我們常用的便是BeanWrapper this.objectWrapper = new BeanWrapper(this, object); } }
為了理解的更為滲透,我們繼續(xù)跟進(jìn),最后我們得知其會(huì)調(diào)用Reflector類的構(gòu)造函數(shù)
private Reflector(Class<?> clazz) { type = clazz; // 獲取構(gòu)造類 addDefaultConstructor(clazz); // 獲取get方法集合 addGetMethods(clazz); // 獲取set方法集合 addSetMethods(clazz); // 獲取內(nèi)部屬性集合 addFields(clazz); readablePropertyNames = getMethods.keySet().toArray(new String[getMethods.keySet().size()]); writeablePropertyNames = setMethods.keySet().toArray(new String[setMethods.keySet().size()]); for (String propName : readablePropertyNames) { caseInsensitivePropertyMap.put(propName.toUpperCase(Locale.ENGLISH), propName); } for (String propName : writeablePropertyNames) { caseInsensitivePropertyMap.put(propName.toUpperCase(Locale.ENGLISH), propName); } }
由此我們便可知使用Reflector代理類以及MetaObject便可以遍歷代理被代理類的所關(guān)聯(lián)的所有屬性,就拿RoutingStatementHandler
類來說,經(jīng)過上述操作后其便可以訪問內(nèi)部屬性delegate以及delegate的內(nèi)部屬性configuration/objectFactory/typeHandlerRegistry/resultSetHandler/parameterHandler/mappedStatement
等屬性
MetaObject#getValue()
上述闡述的是如何代理被代理類的內(nèi)部屬性,我們也簡單的看下是如何正確的調(diào)用
public Object getValue(String name) { // PropertyTokenizer與StringTokenizer類似,只是前者寫死以.為分隔符 PropertyTokenizer prop = new PropertyTokenizer(name); if (prop.hasNext()) { MetaObject metaValue = metaObjectForProperty(prop.getIndexedName()); if (metaValue == SystemMetaObject.NULL_META_OBJECT) { return null; } else { return metaValue.getValue(prop.getChildren()); } } else { return objectWrapper.get(prop); } }
具體的解析就不在此闡述了,如何用戶想獲取StatementHandler所擁有的sql字符串,可通過getValue("delegate.boundSql.sql")
其中以.為分隔符并其中的屬性必須是內(nèi)部屬性(區(qū)分大小寫)。
MetaObject#setValue()
原理同MetaObject#getValue()
方法
總結(jié)
以上所述是小編給大家介紹的Spring Mybatis 分頁插件使用教程,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)億速云網(wǎng)站的支持!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。