Skip to content

Commit

Permalink
调整注解跳过策略.
Browse files Browse the repository at this point in the history
  • Loading branch information
nieqiurong committed Dec 23, 2024
1 parent 5d280b4 commit a22b1ab
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import com.baomidou.mybatisplus.core.plugins.IgnoreStrategy;
import com.baomidou.mybatisplus.core.plugins.InterceptorIgnoreHelper;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.MybatisUtils;
import org.apache.ibatis.binding.MapperProxy;
import org.apache.ibatis.reflection.ExceptionUtil;
import org.apache.ibatis.session.SqlSession;
Expand Down Expand Up @@ -167,11 +167,9 @@ public DefaultMethodInvoker(MethodHandle methodHandle) {
@Override
public Object invoke(Object proxy, Method method, Object[] args, SqlSession sqlSession) throws Throwable {
try {
String className = method.getDeclaringClass().getName();
IgnoreStrategy ignoreStrategy = InterceptorIgnoreHelper.getIgnoreStrategy(className + StringPool.DOT + method.getName());
if (ignoreStrategy == null) {
ignoreStrategy = InterceptorIgnoreHelper.getIgnoreStrategy(className);
}
MybatisMapperProxy<?> mybatisMapperProxy = MybatisUtils.getMybatisMapperProxy(proxy);
Class<?> mapperInterface = mybatisMapperProxy.getMapperInterface();
IgnoreStrategy ignoreStrategy = InterceptorIgnoreHelper.findIgnoreStrategy(mapperInterface, method);
if (ignoreStrategy != null) {
InterceptorIgnoreHelper.handle(ignoreStrategy);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ public abstract class InterceptorIgnoreHelper {
* InterceptorIgnoreHelper.handle(IgnoreStrategy.builder().tenantLine(true).build());
* </p>
* <p>
* 注意,需要手动关闭调用方法 InterceptorIgnoreHelper.clearIgnoreStrategy();
* 注意,需要手动关闭调用方法 {@link #clearIgnoreStrategy()}
* </p>
* <p>简化操作可请使用{@link #execute(IgnoreStrategy, Supplier)}</p>
*
* @param ignoreStrategy {@link IgnoreStrategy}
*/
Expand Down Expand Up @@ -112,6 +113,24 @@ public static <T> T execute(IgnoreStrategy ignoreStrategy, Supplier<T> supplier)
}
}

/**
* 通过方法获取策略信息(优先级方法注解>当前类注解)
*
* @param method 方法信息
* @return 忽略策略信息
* @see #initSqlParserInfoCache(Class)
* @see #initSqlParserInfoCache(IgnoreStrategy, String, Method)
* @since 3.5.10
*/
public static IgnoreStrategy findIgnoreStrategy(Class<?> clz, Method method) {
String className = clz.getName();
IgnoreStrategy ignoreStrategy = getIgnoreStrategy(method.getDeclaringClass().getName() + StringPool.DOT + method.getName());
if (ignoreStrategy == null) {
ignoreStrategy = getIgnoreStrategy(className);
}
return ignoreStrategy;
}

/**
* 初始化缓存
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ void beforeEach() {
init(Xx.class);
init(Pp.class);
init(Gg.class);
init(ExtPp1.class);
init(ExtPp2.class);
init(ExtPp3.class);
}

@Test
Expand All @@ -43,6 +46,19 @@ void m1() {

// 不存在的
checkOthers(Pp.class, "mj", "xxxxx", false);

checkTenantLine(ExtPp1.class, "pp", false);
checkTenantLine(ExtPp1.class, "dd", true);
checkTenantLine(ExtPp1.class, "mj", false);

checkTenantLine(ExtPp2.class, "pp", false);
checkTenantLine(ExtPp2.class, "dd", true);

// 缺省取当前类的
checkTenantLine(ExtPp2.class, "mj", false);

checkTenantLine(ExtPp3.class, "mj", true);

}

private void init(Class<?> clazz) {
Expand Down Expand Up @@ -102,4 +118,21 @@ interface Gg {

void uu();
}

interface ExtPp1 extends Pp {

}

@InterceptorIgnore(tenantLine = "1")
interface ExtPp2 extends Pp {

@InterceptorIgnore(tenantLine = "false")
void mj();
}

@InterceptorIgnore(tenantLine = "true")
interface ExtPp3 extends ExtPp2 {

void mj();
}
}

0 comments on commit a22b1ab

Please sign in to comment.