Commit 0e964aec399dccaff5521d934298f08dc9f1f5d0
1 parent
a7a0a590
ProcessBusinessLine增加别名
Showing
3 changed files
with
41 additions
and
12 deletions
urbanops-framework/urbanops-spring-boot-starter-mybatis/src/main/java/com/zteits/urbanops/framework/annotation/ProcessBusinessLine.java
urbanops-framework/urbanops-spring-boot-starter-mybatis/src/main/java/com/zteits/urbanops/framework/mybatis/config/BusinessLineSqlInterceptor.java
| ... | ... | @@ -57,10 +57,12 @@ public class BusinessLineSqlInterceptor implements Interceptor { |
| 57 | 57 | @Override |
| 58 | 58 | public Object intercept(Invocation invocation) throws Throwable { |
| 59 | 59 | // 1. 跳过过滤则直接执行原 SQL |
| 60 | - if (!isSkipAnnotation(invocation)) { | |
| 60 | + SkipAnnotationVO skipAnnotation = isSkipAnnotation(invocation); | |
| 61 | + if (!skipAnnotation.skip) { | |
| 61 | 62 | return invocation.proceed(); |
| 62 | 63 | } |
| 63 | 64 | String busiLine = SecurityFrameworkUtils.getBusiLine(); |
| 65 | + String aliasName = skipAnnotation.aliasName; | |
| 64 | 66 | if (StringUtils.isEmpty(busiLine)) { |
| 65 | 67 | return invocation.proceed(); |
| 66 | 68 | } |
| ... | ... | @@ -94,7 +96,7 @@ public class BusinessLineSqlInterceptor implements Interceptor { |
| 94 | 96 | Set<String> businessLineSet = new HashSet<>(Arrays.asList(busiLine.split(",")));//UserContextHolder.getBusinessLine(); |
| 95 | 97 | if (!CollectionUtils.isEmpty(businessLineSet)) { |
| 96 | 98 | // 拼接业务线条件 |
| 97 | - String newSql = buildSqlWithBusinessLine(originalSql, businessLineSet, sqlCommandType); | |
| 99 | + String newSql = buildSqlWithBusinessLine(originalSql, businessLineSet, sqlCommandType, aliasName); | |
| 98 | 100 | log.debug("原始 SQL:{},拼接后 SQL:{}", originalSql, newSql); |
| 99 | 101 | // 替换原始 SQL |
| 100 | 102 | metaBoundSql.setValue("sql", newSql); |
| ... | ... | @@ -108,13 +110,18 @@ public class BusinessLineSqlInterceptor implements Interceptor { |
| 108 | 110 | /** |
| 109 | 111 | * 拼接业务线条件到 SQL 中 |
| 110 | 112 | */ |
| 111 | - private String buildSqlWithBusinessLine(String originalSql, Set<String> businessLineSet, SqlCommandType sqlCommandType) { | |
| 113 | + private String buildSqlWithBusinessLine(String originalSql, Set<String> businessLineSet, SqlCommandType sqlCommandType, String aliasName) { | |
| 112 | 114 | String upperSql = originalSql.toUpperCase(); |
| 113 | 115 | // 核心转换:每个元素加单引号,逗号分隔 |
| 114 | 116 | String result = businessLineSet.stream() |
| 115 | 117 | .map(s -> "'" + s + "'") // 为每个元素包裹单引号 |
| 116 | 118 | .collect(Collectors.joining(",")); // 用逗号拼接所有元素 |
| 117 | - String condition = "busi_line in( " + result + ")"; | |
| 119 | + String condition = ""; | |
| 120 | + if(StringUtils.isEmpty(aliasName)){ | |
| 121 | + condition = "busi_line in( " + result + ")"; | |
| 122 | + }else{ | |
| 123 | + condition = aliasName + ".busi_line in( " + result + ")"; | |
| 124 | + } | |
| 118 | 125 | |
| 119 | 126 | // SELECT 语句:处理 WHERE/ORDER BY/GROUP BY 等 |
| 120 | 127 | if (SqlCommandType.SELECT.equals(sqlCommandType)) { |
| ... | ... | @@ -176,7 +183,7 @@ public class BusinessLineSqlInterceptor implements Interceptor { |
| 176 | 183 | * @param invocation 对象 |
| 177 | 184 | * @return true:拼接业务线;false不拼接业务线 |
| 178 | 185 | */ |
| 179 | - public boolean isSkipAnnotation(Invocation invocation){ | |
| 186 | + public SkipAnnotationVO isSkipAnnotation(Invocation invocation){ | |
| 180 | 187 | // 1. 获取MappedStatement(MP封装的Mapper方法信息) |
| 181 | 188 | MappedStatement ms = (MappedStatement) invocation.getArgs()[0]; |
| 182 | 189 | String mapperMethodId = ms.getId(); // 格式:com.xxx.mapper.UserMapper.selectByAge |
| ... | ... | @@ -191,15 +198,28 @@ public class BusinessLineSqlInterceptor implements Interceptor { |
| 191 | 198 | throw new RuntimeException(e); |
| 192 | 199 | } |
| 193 | 200 | ProcessBusinessLine skipAnnotation = ANNOTATION_CACHE.get(mapperClassName+methodName); |
| 194 | - if (skipAnnotation == null) { | |
| 195 | - if(mapperClass.isAnnotationPresent(ProcessBusinessLine.class)){ | |
| 196 | - ProcessBusinessLine processBusinessLine = mapperClass.getAnnotation(ProcessBusinessLine.class); | |
| 197 | - ANNOTATION_CACHE.put(mapperClassName+methodName, processBusinessLine); | |
| 198 | - return true; | |
| 199 | - } | |
| 201 | + if (skipAnnotation != null) { | |
| 202 | + return new SkipAnnotationVO(true, skipAnnotation.aliasName()); | |
| 203 | + } | |
| 204 | + | |
| 205 | + if(mapperClass.isAnnotationPresent(ProcessBusinessLine.class)){ | |
| 206 | + ProcessBusinessLine processBusinessLine = mapperClass.getAnnotation(ProcessBusinessLine.class); | |
| 207 | + ANNOTATION_CACHE.put(mapperClassName+methodName, processBusinessLine); | |
| 208 | + return new SkipAnnotationVO(true, processBusinessLine.aliasName()); | |
| 209 | + } | |
| 210 | + | |
| 211 | + return new SkipAnnotationVO(false, ""); | |
| 212 | + } | |
| 213 | + | |
| 214 | + public static class SkipAnnotationVO { | |
| 215 | + public boolean skip; | |
| 216 | + public String aliasName; | |
| 217 | + | |
| 218 | + public SkipAnnotationVO(boolean skip, String aliasName) { | |
| 219 | + this.skip = skip; | |
| 220 | + this.aliasName = aliasName; | |
| 200 | 221 | } |
| 201 | 222 | |
| 202 | - return false; | |
| 203 | 223 | } |
| 204 | 224 | |
| 205 | 225 | } |
| 206 | 226 | \ No newline at end of file | ... | ... |
urbanops-framework/urbanops-spring-boot-starter-mybatis/src/main/java/com/zteits/urbanops/framework/mybatis/config/UrbanopsMybatisAutoConfiguration.java
| ... | ... | @@ -22,6 +22,7 @@ import org.mybatis.spring.annotation.MapperScan; |
| 22 | 22 | import org.springframework.boot.autoconfigure.AutoConfiguration; |
| 23 | 23 | import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
| 24 | 24 | import org.springframework.context.annotation.Bean; |
| 25 | +import org.springframework.core.annotation.Order; | |
| 25 | 26 | import org.springframework.core.env.ConfigurableEnvironment; |
| 26 | 27 | |
| 27 | 28 | import java.util.List; |
| ... | ... | @@ -41,6 +42,7 @@ public class UrbanopsMybatisAutoConfiguration { |
| 41 | 42 | * 注册自定义业务线 SQL 拦截器 |
| 42 | 43 | */ |
| 43 | 44 | @Bean |
| 45 | + @Order(1) | |
| 44 | 46 | public BusinessLineSqlInterceptor businessLineSqlInterceptor() { |
| 45 | 47 | return new BusinessLineSqlInterceptor(); |
| 46 | 48 | } |
| ... | ... | @@ -54,6 +56,7 @@ public class UrbanopsMybatisAutoConfiguration { |
| 54 | 56 | } |
| 55 | 57 | |
| 56 | 58 | @Bean |
| 59 | + @Order(2) | |
| 57 | 60 | public MybatisPlusInterceptor mybatisPlusInterceptor() { |
| 58 | 61 | MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor(); |
| 59 | 62 | mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor()); // 分页插件 | ... | ... |