Springboot integrates mybatissql filtering @ intercepts

Time:2020-7-22

Scene:

System module query database needs to filter data according to user ID. If you add user to each SQL_ The filtering of ID is obviously not clear. Therefore, the SQL should be spliced with conditions before query to do unified management.

Development environment:

  spring boot + mybatis

Only one interceptor class is needed to do this (you need to understand the annotation @ intercepts()) before looking at the code:

@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......");
//Get 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);

//Determine whether SQL has a where condition. Change 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;
 }
//Carry on
 Object result = invocation.proceed();
 return result;
    }

    @Override
    public Object plugin(Object target) {
 return Plugin.wrap(target, this);
    }

    @Override
    public void setProperties(Properties properties) {

    }

    //Copy the original 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;
 }
    }

}

In this way, you can find that every query will go this way!

Recommended Today

Build HTTP service with C + + Mongoose

Mongoose source code address:https://github.com/cesanta/mo… Mongoose user manual:https://www.cesanta.com/devel… Mngoose set up HTTP service #include <string> #include “mongoose.h” using namespace std; static const char *s_http_port = “8000”; static void ev_handler(mg_connection *nc, int ev, void *ev_data) { struct http_message *hm = (struct http_message *) ev_data; if (ev == MG_EV_HTTP_REQUEST) { std::string uri; if (hm->uri.p && hm->uri.p[0] == ‘/’) […]