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
| @@ -10,4 +10,10 @@ import java.lang.annotation.*; | @@ -10,4 +10,10 @@ import java.lang.annotation.*; | ||
| 10 | @Retention(RetentionPolicy.RUNTIME) // 运行时保留,支持反射 | 10 | @Retention(RetentionPolicy.RUNTIME) // 运行时保留,支持反射 |
| 11 | @Documented | 11 | @Documented |
| 12 | public @interface ProcessBusinessLine { | 12 | public @interface ProcessBusinessLine { |
| 13 | + | ||
| 14 | + /** | ||
| 15 | + * 表别名 应对手写sql多表关联增加业务线 | ||
| 16 | + * @return | ||
| 17 | + */ | ||
| 18 | + String aliasName() default ""; | ||
| 13 | } | 19 | } |
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,10 +57,12 @@ public class BusinessLineSqlInterceptor implements Interceptor { | ||
| 57 | @Override | 57 | @Override |
| 58 | public Object intercept(Invocation invocation) throws Throwable { | 58 | public Object intercept(Invocation invocation) throws Throwable { |
| 59 | // 1. 跳过过滤则直接执行原 SQL | 59 | // 1. 跳过过滤则直接执行原 SQL |
| 60 | - if (!isSkipAnnotation(invocation)) { | 60 | + SkipAnnotationVO skipAnnotation = isSkipAnnotation(invocation); |
| 61 | + if (!skipAnnotation.skip) { | ||
| 61 | return invocation.proceed(); | 62 | return invocation.proceed(); |
| 62 | } | 63 | } |
| 63 | String busiLine = SecurityFrameworkUtils.getBusiLine(); | 64 | String busiLine = SecurityFrameworkUtils.getBusiLine(); |
| 65 | + String aliasName = skipAnnotation.aliasName; | ||
| 64 | if (StringUtils.isEmpty(busiLine)) { | 66 | if (StringUtils.isEmpty(busiLine)) { |
| 65 | return invocation.proceed(); | 67 | return invocation.proceed(); |
| 66 | } | 68 | } |
| @@ -94,7 +96,7 @@ public class BusinessLineSqlInterceptor implements Interceptor { | @@ -94,7 +96,7 @@ public class BusinessLineSqlInterceptor implements Interceptor { | ||
| 94 | Set<String> businessLineSet = new HashSet<>(Arrays.asList(busiLine.split(",")));//UserContextHolder.getBusinessLine(); | 96 | Set<String> businessLineSet = new HashSet<>(Arrays.asList(busiLine.split(",")));//UserContextHolder.getBusinessLine(); |
| 95 | if (!CollectionUtils.isEmpty(businessLineSet)) { | 97 | if (!CollectionUtils.isEmpty(businessLineSet)) { |
| 96 | // 拼接业务线条件 | 98 | // 拼接业务线条件 |
| 97 | - String newSql = buildSqlWithBusinessLine(originalSql, businessLineSet, sqlCommandType); | 99 | + String newSql = buildSqlWithBusinessLine(originalSql, businessLineSet, sqlCommandType, aliasName); |
| 98 | log.debug("原始 SQL:{},拼接后 SQL:{}", originalSql, newSql); | 100 | log.debug("原始 SQL:{},拼接后 SQL:{}", originalSql, newSql); |
| 99 | // 替换原始 SQL | 101 | // 替换原始 SQL |
| 100 | metaBoundSql.setValue("sql", newSql); | 102 | metaBoundSql.setValue("sql", newSql); |
| @@ -108,13 +110,18 @@ public class BusinessLineSqlInterceptor implements Interceptor { | @@ -108,13 +110,18 @@ public class BusinessLineSqlInterceptor implements Interceptor { | ||
| 108 | /** | 110 | /** |
| 109 | * 拼接业务线条件到 SQL 中 | 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 | String upperSql = originalSql.toUpperCase(); | 114 | String upperSql = originalSql.toUpperCase(); |
| 113 | // 核心转换:每个元素加单引号,逗号分隔 | 115 | // 核心转换:每个元素加单引号,逗号分隔 |
| 114 | String result = businessLineSet.stream() | 116 | String result = businessLineSet.stream() |
| 115 | .map(s -> "'" + s + "'") // 为每个元素包裹单引号 | 117 | .map(s -> "'" + s + "'") // 为每个元素包裹单引号 |
| 116 | .collect(Collectors.joining(",")); // 用逗号拼接所有元素 | 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 | // SELECT 语句:处理 WHERE/ORDER BY/GROUP BY 等 | 126 | // SELECT 语句:处理 WHERE/ORDER BY/GROUP BY 等 |
| 120 | if (SqlCommandType.SELECT.equals(sqlCommandType)) { | 127 | if (SqlCommandType.SELECT.equals(sqlCommandType)) { |
| @@ -176,7 +183,7 @@ public class BusinessLineSqlInterceptor implements Interceptor { | @@ -176,7 +183,7 @@ public class BusinessLineSqlInterceptor implements Interceptor { | ||
| 176 | * @param invocation 对象 | 183 | * @param invocation 对象 |
| 177 | * @return true:拼接业务线;false不拼接业务线 | 184 | * @return true:拼接业务线;false不拼接业务线 |
| 178 | */ | 185 | */ |
| 179 | - public boolean isSkipAnnotation(Invocation invocation){ | 186 | + public SkipAnnotationVO isSkipAnnotation(Invocation invocation){ |
| 180 | // 1. 获取MappedStatement(MP封装的Mapper方法信息) | 187 | // 1. 获取MappedStatement(MP封装的Mapper方法信息) |
| 181 | MappedStatement ms = (MappedStatement) invocation.getArgs()[0]; | 188 | MappedStatement ms = (MappedStatement) invocation.getArgs()[0]; |
| 182 | String mapperMethodId = ms.getId(); // 格式:com.xxx.mapper.UserMapper.selectByAge | 189 | String mapperMethodId = ms.getId(); // 格式:com.xxx.mapper.UserMapper.selectByAge |
| @@ -191,15 +198,28 @@ public class BusinessLineSqlInterceptor implements Interceptor { | @@ -191,15 +198,28 @@ public class BusinessLineSqlInterceptor implements Interceptor { | ||
| 191 | throw new RuntimeException(e); | 198 | throw new RuntimeException(e); |
| 192 | } | 199 | } |
| 193 | ProcessBusinessLine skipAnnotation = ANNOTATION_CACHE.get(mapperClassName+methodName); | 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 | \ No newline at end of file | 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,6 +22,7 @@ import org.mybatis.spring.annotation.MapperScan; | ||
| 22 | import org.springframework.boot.autoconfigure.AutoConfiguration; | 22 | import org.springframework.boot.autoconfigure.AutoConfiguration; |
| 23 | import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | 23 | import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
| 24 | import org.springframework.context.annotation.Bean; | 24 | import org.springframework.context.annotation.Bean; |
| 25 | +import org.springframework.core.annotation.Order; | ||
| 25 | import org.springframework.core.env.ConfigurableEnvironment; | 26 | import org.springframework.core.env.ConfigurableEnvironment; |
| 26 | 27 | ||
| 27 | import java.util.List; | 28 | import java.util.List; |
| @@ -41,6 +42,7 @@ public class UrbanopsMybatisAutoConfiguration { | @@ -41,6 +42,7 @@ public class UrbanopsMybatisAutoConfiguration { | ||
| 41 | * 注册自定义业务线 SQL 拦截器 | 42 | * 注册自定义业务线 SQL 拦截器 |
| 42 | */ | 43 | */ |
| 43 | @Bean | 44 | @Bean |
| 45 | + @Order(1) | ||
| 44 | public BusinessLineSqlInterceptor businessLineSqlInterceptor() { | 46 | public BusinessLineSqlInterceptor businessLineSqlInterceptor() { |
| 45 | return new BusinessLineSqlInterceptor(); | 47 | return new BusinessLineSqlInterceptor(); |
| 46 | } | 48 | } |
| @@ -54,6 +56,7 @@ public class UrbanopsMybatisAutoConfiguration { | @@ -54,6 +56,7 @@ public class UrbanopsMybatisAutoConfiguration { | ||
| 54 | } | 56 | } |
| 55 | 57 | ||
| 56 | @Bean | 58 | @Bean |
| 59 | + @Order(2) | ||
| 57 | public MybatisPlusInterceptor mybatisPlusInterceptor() { | 60 | public MybatisPlusInterceptor mybatisPlusInterceptor() { |
| 58 | MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor(); | 61 | MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor(); |
| 59 | mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor()); // 分页插件 | 62 | mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor()); // 分页插件 |