use of com.baomidou.mybatisplus.solon.plugins.inner.InnerInterceptor in project solon by noear.
the class MybatisPlusInterceptorTest method setProperties.
@Test
void setProperties() {
Properties properties = new Properties();
properties.setProperty("@page", "com.baomidou.mybatisplus.solon.plugins.inner.PaginationInnerInterceptor");
properties.setProperty("page:maxLimit", "10");
properties.setProperty("page:dbType", "h2");
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.setProperties(properties);
List<InnerInterceptor> interceptors = interceptor.getInterceptors();
assertThat(interceptors).isNotEmpty();
assertThat(interceptors.size()).isEqualTo(1);
InnerInterceptor page = interceptors.get(0);
assertThat(page).isInstanceOf(PaginationInnerInterceptor.class);
PaginationInnerInterceptor pii = (PaginationInnerInterceptor) page;
assertThat(pii.getMaxLimit()).isEqualTo(10);
assertThat(pii.getDbType()).isEqualTo(DbType.H2);
}
use of com.baomidou.mybatisplus.solon.plugins.inner.InnerInterceptor in project solon by noear.
the class MybatisPlusInterceptor method setProperties.
/**
* 使用内部规则,拿分页插件举个栗子:
* <p>
* - key: "@page" ,value: "com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor"
* - key: "page:limit" ,value: "100"
* <p>
* 解读1: key 以 "@" 开头定义了这是一个需要组装的 `InnerInterceptor`, 以 "page" 结尾表示别名
* value 是 `InnerInterceptor` 的具体的 class 全名
* 解读2: key 以上面定义的 "别名 + ':'" 开头指这个 `value` 是定义的该 `InnerInterceptor` 属性需要设置的值
* <p>
* 如果这个 `InnerInterceptor` 不需要配置属性也要加别名
*/
@Override
public void setProperties(Properties properties) {
PropertyMapper pm = PropertyMapper.newInstance(properties);
Map<String, Properties> group = pm.group(StringPool.AT);
group.forEach((k, v) -> {
InnerInterceptor innerInterceptor = ClassUtils.newInstance(k);
innerInterceptor.setProperties(v);
addInnerInterceptor(innerInterceptor);
});
}
use of com.baomidou.mybatisplus.solon.plugins.inner.InnerInterceptor in project solon by noear.
the class MybatisPlusInterceptor method intercept.
@Override
public Object intercept(Invocation invocation) throws Throwable {
Object target = invocation.getTarget();
Object[] args = invocation.getArgs();
if (target instanceof Executor) {
final Executor executor = (Executor) target;
Object parameter = args[1];
boolean isUpdate = args.length == 2;
MappedStatement ms = (MappedStatement) args[0];
if (!isUpdate && ms.getSqlCommandType() == SqlCommandType.SELECT) {
RowBounds rowBounds = (RowBounds) args[2];
ResultHandler resultHandler = (ResultHandler) args[3];
BoundSql boundSql;
if (args.length == 4) {
boundSql = ms.getBoundSql(parameter);
} else {
// 几乎不可能走进这里面,除非使用Executor的代理对象调用query[args[6]]
boundSql = (BoundSql) args[5];
}
for (InnerInterceptor query : interceptors) {
if (!query.willDoQuery(executor, ms, parameter, rowBounds, resultHandler, boundSql)) {
return Collections.emptyList();
}
query.beforeQuery(executor, ms, parameter, rowBounds, resultHandler, boundSql);
}
CacheKey cacheKey = executor.createCacheKey(ms, parameter, rowBounds, boundSql);
return executor.query(ms, parameter, rowBounds, resultHandler, cacheKey, boundSql);
} else if (isUpdate) {
for (InnerInterceptor update : interceptors) {
if (!update.willDoUpdate(executor, ms, parameter)) {
return -1;
}
update.beforeUpdate(executor, ms, parameter);
}
}
} else {
// StatementHandler
final StatementHandler sh = (StatementHandler) target;
// 目前只有StatementHandler.getBoundSql方法args才为null
if (null == args) {
for (InnerInterceptor innerInterceptor : interceptors) {
innerInterceptor.beforeGetBoundSql(sh);
}
} else {
Connection connections = (Connection) args[0];
Integer transactionTimeout = (Integer) args[1];
for (InnerInterceptor innerInterceptor : interceptors) {
innerInterceptor.beforePrepare(sh, connections, transactionTimeout);
}
}
}
return invocation.proceed();
}
Aggregations