溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

mybatis攔截器怎么實(shí)現(xiàn)通用權(quán)限字段添加功能

發(fā)布時(shí)間:2021-09-09 15:38:55 來(lái)源:億速云 閱讀:131 作者:chen 欄目:編程語(yǔ)言

這篇文章主要講解了“mybatis攔截器怎么實(shí)現(xiàn)通用權(quán)限字段添加功能”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“mybatis攔截器怎么實(shí)現(xiàn)通用權(quán)限字段添加功能”吧!

實(shí)現(xiàn)效果

日常sql中直接使用權(quán)限字段實(shí)現(xiàn)權(quán)限內(nèi)數(shù)據(jù)篩選,無(wú)需入?yún)?,直接使用,使用形式為?/p>

select * from crh_snp.channelinfo where short_code in (${commonEnBranchNo})

注意事項(xiàng)說(shuō)明

1、添加插件若使用xml形式mybatis可在配置文件中plugins標(biāo)簽中添加,本項(xiàng)目實(shí)際使用的為注解形式mybatis,需要通過(guò)SqlSessionFactoryBean代碼方式添加或者SqlSessionFactoryBean的xml配置形式,代碼在jar包中無(wú)法操作,只能使用xml配置形式,故需要覆蓋SqlSessionFactoryBean配置

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  <property name="dataSource" ref="dataSource" />  <property name="mapperLocations">   <list>     <value>classpath*:xmlmapper/*.xml</value>     <value>classpath*:resources/xmlmapper/*.xml</value>   </list>  </property>  <property name="plugins">   <array>     <bean class="com.cairh.xpe.snp.backend.interceptor.MybatisInterceptor"/>   </array>  </property></bean>

2、jdbc的jar包中配置了sqlSessionFactory,本項(xiàng)目中配置進(jìn)行覆蓋,注意spring中同名類后加載的會(huì)覆蓋先加載的類,需要保證本項(xiàng)目配置的類后加載。spring配置文件掃描會(huì)先加載本工程項(xiàng)目bean,可通過(guò)新增額外的配置文件放在原配置文件后實(shí)現(xiàn)后加載,如

<context-param>  <param-name>contextConfigLocation</param-name>  <param-value>   classpath*:spring-beans.xml   classpath*:spring-person.xml  </param-value></context-param>

3、注意添加的參數(shù)需要${}形式使用,#{}會(huì)經(jīng)過(guò)預(yù)編譯獲取到的sql參數(shù)為問(wèn)號(hào),無(wú)法直接替換

攔截器實(shí)現(xiàn)類

@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);      //將操作員可操作的渠道、用戶id及營(yíng)業(yè)部作通用字段放到sql中統(tǒng)一解析      if(sql.contains("commonEnShortCode")){        sql = addPremissionParam(sql);        resetSql2Invocation(invocation, sql);      }    }    return invocation.proceed();  }  @Override  public Object plugin(Object target) {    return Plugin.wrap(target, this);  }  @Override  public void setProperties(Properties properties) {}  /**   * 通用權(quán)限字段添加,目前支持: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<String> 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;  }  /**   * 獲取當(dāng)前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();  }  /**   * 將sql重新設(shè)置到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;  }}

感謝各位的閱讀,以上就是“mybatis攔截器怎么實(shí)現(xiàn)通用權(quán)限字段添加功能”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)mybatis攔截器怎么實(shí)現(xiàn)通用權(quán)限字段添加功能這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI