SpringBoot整合MybatisSQL過濾@Intercepts的實現(xiàn)
場景:
系統(tǒng)模塊查詢數(shù)據(jù)庫需要根據(jù)用戶的id去篩選數(shù)據(jù)。那么如果在 每個sql加user_id的過濾顯然不明確。所以要在查詢前將sql拼接上條件,做統(tǒng)一管理。
開發(fā)環(huán)境:
spring boot + mybatis
只需一個攔截類即可搞定(在看代碼前需要了解注解@Intercepts()):
@Component
@Intercepts({ @Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class }) })
public class SqlInterceptor implements Interceptor {
private Logger logger = LoggerFactory.getLogger(SqlInterceptor.class);
@Override
public Object intercept(Invocation invocation) throws Throwable {
logger.info("Interceptor......");
// 獲取sql
MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0];
Object parameter = invocation.getArgs()[1];
BoundSql boundSql = mappedStatement.getBoundSql(parameter);
String oldsql = boundSql.getSql();
logger.info("old:"+oldsql);
// 判斷sql是否有where條件。改變sql。
boolean status = oldsql.toLowerCase().contains("where");
if (status) {
BoundSql newBoundSql = new BoundSql(mappedStatement.getConfiguration(), oldsql + " and 1=1",
boundSql.getParameterMappings(), boundSql.getParameterObject());
MappedStatement newMs = copyFromMappedStatement(mappedStatement, new BoundSqlSqlSource(newBoundSql));
invocation.getArgs()[0] = newMs;
}
// 繼續(xù)執(zhí)行
Object result = invocation.proceed();
return result;
}
@Override
public Object plugin(Object target) {
return Plugin.wrap(target, this);
}
@Override
public void setProperties(Properties properties) {
}
// 復(fù)制原始MappedStatement
private MappedStatement copyFromMappedStatement(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) {
for (String keyProperty : ms.getKeyProperties()) {
builder.keyProperty(keyProperty);
}
}
builder.timeout(ms.getTimeout());
builder.parameterMap(ms.getParameterMap());
builder.resultMaps(ms.getResultMaps());
builder.cache(ms.getCache());
builder.useCache(ms.isUseCache());
return builder.build();
}
public static class BoundSqlSqlSource implements SqlSource {
BoundSql boundSql;
public BoundSqlSqlSource(BoundSql boundSql) {
this.boundSql = boundSql;
}
public BoundSql getBoundSql(Object parameterObject) {
return boundSql;
}
}
}
這樣重啟訪問即可發(fā)現(xiàn)每次查詢都會走這邊!
到此這篇關(guān)于SpringBoot整合MybatisSQL過濾@Intercepts的實現(xiàn)的文章就介紹到這了,更多相關(guān)SpringBoot MybatisSQL過濾@Intercepts內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringSecurity rememberme功能實現(xiàn)過程解析
這篇文章主要介紹了SpringSecurity rememberme功能實現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-03-03
Java多線程案例實戰(zhàn)之定時器的實現(xiàn)
在Java中可以使用多線程和定時器來實現(xiàn)定時任務(wù),下面這篇文章主要給大家介紹了關(guān)于Java多線程案例之定時器實現(xiàn)的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01
PageHelper在springboot+mybatis框架中的使用步驟及原理解析
這篇文章主要介紹了PageHelper在springboot+mybatis框架中的使用步驟及原理解析,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-03-03

