Mybatis内部有个plugins(插件)概念,本质上属于拦截器的思想。具体的解析可见他文MyBatis拦截器原理探究。本文将在此基础上直接展示实际项目的实现代码和其他的相关解析
分页具体代码实现
首先我们可以定义方言抽象类,用于实现分页AbstractDialect.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
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);
}
|
再而我们就以Oracle与Mysql数据库的分页技术作下分别的实现
MySQLDialect.java-Mysql分页方言
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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方言实现
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
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();
}
}
|
对应的Mybatis插件拦截器实现如下,拦截StatementHandler#prepare(Connection con)创建SQL语句对象方法
PaginationInterceptor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
@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 {
// 直接获取拦截的对象,其实现类RoutingStatementHandler
StatementHandler statementHandler = (StatementHandler) invocation
.getTarget();
BoundSql boundSql = statementHandler.getBoundSql();
// 获取元对象,主要用于获取statementHandler所关联的对象及属性
MetaObject metaStatementHandler = MetaObject.forObject(
statementHandler, new DefaultObjectFactory(),
new DefaultObjectWrapperFactory());
MappedStatement mappedStmt= (MappedStatement) metaStatementHandler
.getValue( "delegate.mappedStatement" .intern());
// 只对queryPagination()方法进行分页操作
if (mappedStmt.getId().indexOf( "queryPagination" )==- 1 ){
return invocation.proceed();
}
// 重新构造分页的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();
}
// 拦截对象
@Override
public Object plugin(Object target) {
return Plugin.wrap(target, this );
}
@Override
public void setProperties(Properties properties) {
}
}
|
Spring对应的xml配置可如下,以oracle分页为例子
1
2
3
4
5
6
|
<!-- 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>
|
使用以上的代码以及配置即可完成对oracle数据库以及mysql数据库的分页操作。并且博主对其中的某个点作下解析
Mybatis#MetaObject-元数据对象解析
以上的代码博主之前在使用的时候,对其中的MetaObject这个类很费解,其直接通过getValue()方法便可以将所代理的对象的所关联的属性全都拿取到。我们可以跟随源码深入了解下
MetaObject#forObject()
代理对象均通过此静态方法进入
1
2
3
4
5
6
7
|
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);
}
}
|
我们可以直接观察其中的构造函数,玄机就在此处
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
private MetaObject(Object object, ObjectFactory objectFactory, ObjectWrapperFactory objectWrapperFactory) {
this .originalObject = object;
this .objectFactory = objectFactory;
this .objectWrapperFactory = objectWrapperFactory;
// 所有的属性获取均通过objectWrapper类来获取,此处主要对所代理的object对象类型进行判断
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);
}
}
|
为了理解的更为渗透,我们继续跟进,最后我们得知其会调用Reflector类的构造函数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
private Reflector(Class<?> clazz) {
type = clazz;
// 获取构造类
addDefaultConstructor(clazz);
// 获取get方法集合
addGetMethods(clazz);
// 获取set方法集合
addSetMethods(clazz);
// 获取内部属性集合
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便可以遍历代理被代理类的所关联的所有属性,就拿RoutingStatementHandler
类来说,经过上述操作后其便可以访问内部属性delegate以及delegate的内部属性configuration/objectFactory/typeHandlerRegistry/resultSetHandler/parameterHandler/mappedStatement
等属性
MetaObject#getValue()
上述阐述的是如何代理被代理类的内部属性,我们也简单的看下是如何正确的调用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
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")
其中以.为分隔符并其中的属性必须是内部属性(区分大小写)。
MetaObject#setValue()
原理同MetaObject#getValue()
方法
总结
以上所述是小编给大家介绍的Spring Mybatis 分页插件使用教程,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!
原文链接:https://www.cnblogs.com/question-sky/p/8462577.html