MyBatis 自定义 SQL 拦截器,2021腾讯Java面试题精选
2021/11/9 19:43:41
本文主要是介绍MyBatis 自定义 SQL 拦截器,2021腾讯Java面试题精选,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
@Signature(type = Executor.class, method = “query”, args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}),
@Signature(type = Executor.class, method = “query”, args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class, CacheKey.class, BoundSql.class}),
})
public class MyBatisInterceptor implements Interceptor {
private static final Logger LOGGER = LoggerFactory.getLogger(MyBatisInterceptor.class);
@Override
public Object intercept(Invocation invocation) throws Throwable {
// TODO Auto-generated method stub
Object[] args = invocation.getArgs();
MappedStatement ms = (MappedStatement) args[0];
Object parameter = args[1];
RowBounds rowBounds = (RowBounds) args[2];
ResultHandler resultHandler = (ResultHandler) args[3];
Executor executor = (Executor) invocation.getTarget();
CacheKey cacheKey;
BoundSql boundSql;
//由于逻辑关系,只会进入一次
if (args.length == 4) {
//4 个参数时
boundSql = ms.getBoundSql(parameter);
cacheKey = executor.createCacheKey(ms, parameter, rowBounds, boundSql);
} else {
//6 个参数时
cacheKey = (CacheKey) args[4];
boundSql = (BoundSql) args[5];
}
DataScope dataScope = getDataScope(ms);
if (Objects.nonNull(dataScope)) {
String origSql = boundSql.getSql();
log.info(“origSql : {}”, origSql);
// 组装新的 sql
// todo you weaving business
String newSql = origSql + " limit 1";
// 重新new一个查询语句对象
BoundSql newBoundSql = new BoundSql(ms.getConfiguration(), newSql,
boundSql.getParameterMappings(), boundSql.getParameterObject());
// 把新的查询放到statement里
MappedStatement newMs = newMappedStatement(ms, new BoundSqlSource(newBoundSql));
for (ParameterMapping mapping : boundSql.getParameterMappings()) {
String prop = mapping.getProperty();
if (boundSql.hasAdditionalParameter(prop)) {
newBoundSql.setAdditionalParameter(prop, boundSql.getAdditionalParameter(prop));
}
}
args[0] = newMs;
if (args.length == 6) {
args[5] = newMs.getBoundSql(parameter);
}
}
LOGGER.info(“mybatis intercept sql:{},Mapper方法是:{}”, boundSql.getSql(), ms.getId());
return invocation.proceed();
}
private MappedStatement newMappedStatement(MappedStatement ms, SqlSource newSqlSource) {
MappedStatement.Builder builder = new
MappedStatement.Builder(ms.getConfiguration(), ms.getId(), newSqlSource, ms.getSqlCommandType());
builder.resource(ms.getResource());
builder.fetchSize(ms.getFetchSize());
builder.statementType(ms.getStatementType());
builder.keyGenerator(ms.getKeyGenerator());
if (ms.getKeyProperties() != null && ms.getKeyProperties().length > 0) {
builder.keyProperty(ms.getKeyProperties()[0]);
}
builder.timeout(ms.getTimeout());
builder.parameterMap(ms.getParameterMap());
builder.resultMaps(ms.getResultMaps());
builder.resultSetType(ms.getResultSetType());
builder.cache(ms.getCache());
builder.flushCacheRequired(ms.isFlushCacheRequired());
builder.useCache(ms.isUseCache());
return builder.build();
}
private DataScope getDataScope(MappedStatement mappedStatement) {
String id = mappedStatement.getId();
// 获取 Class Method
String clazzName = id.substring(0, id.lastIndexOf(’.’));
String mapperMethod = id.substring(id.lastIndexOf(’.’) + 1);
Class<?> clazz;
try {
clazz = Class.forName(clazzName);
} catch (ClassNotFoundException e) {
return null;
}
Method[] methods = clazz.getMethods();
DataScope dataScope = null;
for (Method method : methods) {
if (method.getName().equals(mapperMethod)) {
dataScope = method.getAnnotation(DataScope.class);
break;
}
}
return dataScope;
}
@Override
public Object plugin(Object target) {
// TODO Auto-generated method stub
LOGGER.info(“MysqlInterCeptor plugin>>>>>>>{}”, target);
return Plugin.wrap(target, this);
}
@Override
public void setProperties(Pro
【一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义】 浏览器打开:qq.cn.hn/FTf 免费领取
perties properties) {
// TODO Auto-generated method stub
String dialect = properties.getProperty(“dialect”);
LOGGER.info(“mybatis intercept dialect:>>>>>>>{}”, dialect);
}
/**
- 定义一个内部辅助类,作用是包装 SQL
*/
class BoundSqlSource implements SqlSource {
private BoundSql boundSql;
public BoundSqlSource(BoundSql boundSql) {
这篇关于MyBatis 自定义 SQL 拦截器,2021腾讯Java面试题精选的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-12-27数据结构与算法面试题详解及练习
- 2024-12-27网络请求面试题详解与实战
- 2024-12-27数据结构和算法面试真题详解与实战教程
- 2024-12-27网络请求面试真题解析与实战教程
- 2024-12-27数据结构和算法大厂面试真题详解与实战指南
- 2024-12-27TS大厂面试真题解析与应对策略
- 2024-12-27TS大厂面试真题详解与解析
- 2024-12-27网站安全入门:如何识别和修复漏洞
- 2024-12-27SQL注入基础教程
- 2024-12-27初学者指南:理解和修复跨域漏洞