In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains the "mybatis interceptor how to achieve general permissions field add function", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "mybatis interceptor how to achieve general permissions field add function" bar!
Realize the effect
In daily sql, the permission field is directly used to filter the data within the permission. No input parameter is required, and it is used directly. The format is as follows:
Select * from crh_snp.channelinfo where short_code in (${commonEnBranchNo})
Notes
1. Add plug-ins in xml form mybatis can be added in the plugins tag in the configuration file. The annotated form mybatis is actually used in this project, which needs to be added through SqlSessionFactoryBean code or the xml configuration form of SqlSessionFactoryBean. The code cannot be operated in the jar package, so you can only use the xml configuration form, so you need to override the SqlSessionFactoryBean configuration.
Classpath*:xmlmapper/*.xml classpath*:resources/xmlmapper/*.xml
2. SqlSessionFactory is configured in the jar package of jdbc, which is configured in this project to override. Note that classes with the same name loaded after spring will override the classes that are loaded first. You need to ensure that the classes configured in this project are loaded later. Spring configuration file scan will load the project bean first, which can be loaded after adding additional configuration files to the original configuration file, such as
ContextConfigLocation classpath*:spring-beans.xml classpath*:spring-person.xml
3. Note that the added parameters need to be used in the form of ${}. The sql parameter obtained by precompilation will be a question mark and cannot be replaced directly.
Interceptor implementation class
@ Intercepts ({@ Signature (type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class})) public class MybatisInterceptor implements Interceptor {/ / private Logger logger = LoggerFactory.getLogger (getClass ()); @ Override public Object intercept (Invocation invocation) throws Throwable {if (invocation.getTarget () instanceof Executor & invocation.getArgs (). Length==4) {String sql = getSqlByInvocation (invocation) / / uniformly parse if (sql.contains ("commonEnShortCode")) {sql = addPremissionParam (sql); resetSql2Invocation (invocation, sql);}} return invocation.proceed ();} @ Override public Object plugin (Object target) {return Plugin.wrap (target, this) in sql } @ Override public void setProperties (Properties properties) {} / * * General permission fields are added. Currently, commonEnShortCode, commonEnBrokerUserId, commonEnBranchNo * @ param sql * @ return * / private String addPremissionParam (String sql) {CrhUser crhUser = (CrhUser) RequestUtil.getRequest (). GetAttribute (CrhUser.CRH_USER_SESSION); BackendRoleServiceImpl backendRoleService = (BackendRoleServiceImpl) SpringContext.getBean ("backendRoleServiceImpl"); if (sql.contains ("commonEnBranchNo")) {List enBranchNoList = backendRoleService.getEnBranchNo (crhUser.getUser_id ()) String enBranchNoSql = "select to_char (column_value) from TABLE (SELECT F_TO_T_IN ('" + StringUtils.join (enBranchNoList, ",") + "') FROM DUAL)"; sql = sql.replace ("${commonEnBranchNo}", enBranchNoSql);} return sql;} / * * get the current sql * @ param invocation * @ return * / private String getSqlByInvocation (Invocation invocation) {final Object [] args = invocation.getArgs (); MappedStatement ms = (MappedStatement) args [0] Object parameterObject = args [1]; BoundSql boundSql = ms.getBoundSql (parameterObject); return boundSql.getSql ();} / * reset sql to invocation * @ param invocation * @ param sql * @ throws SQLException * / private void resetSql2Invocation (Invocation invocation, String sql) throws SQLException {final Object [] args = invocation.getArgs (); MappedStatement statement = (MappedStatement) args [0]; Object parameterObject = args [1]; BoundSql boundSql = statement.getBoundSql (parameterObject); MappedStatement newStatement = newMappedStatement (statement, new BoundSqlSource (boundSql)) MetaObject msObject = MetaObject.forObject (newStatement, new DefaultObjectFactory (), new DefaultObjectWrapperFactory (), new DefaultReflectorFactory ()); msObject.setValue ("sqlSource.boundSql.sql", sql); args [0] = newStatement;} 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) {StringBuilder keyProperties = new StringBuilder (); for (String keyProperty: ms.getKeyProperties ()) {keyProperties.append (keyProperty) .append (",");} keyProperties.delete (keyProperties.length ()-1, keyProperties.length ()); builder.keyProperty (keyProperties.toString ());} 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 ();}}
Public class BoundSqlSource implements SqlSource {private BoundSql boundSql; public BoundSqlSource (BoundSql boundSql) {this.boundSql = boundSql;} @ Override public BoundSql getBoundSql (Object parameterObject) {return boundSql;}}
Thank you for your reading, the above is the content of "mybatis interceptor how to achieve general permissions field add function". After the study of this article, I believe you have a deeper understanding of how mybatis interceptor realizes the general permissions field addition function, and the specific use still needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.