diff --git a/readme.md b/readme.md index 606edb84cf3ad7471eef79e24452e78284396321..0ed5c4e61e732c74f54be2a80212f99886b8e82d 100644 --- a/readme.md +++ b/readme.md @@ -16,7 +16,8 @@ 3. 支持动态添加数据权限`DataScopeUtil.put(方法全名, DataScopeProperty)` 4. 暂不支持自定义条件构建,并且,这个版本只支持=,>,>=,<,<=,in,not in,like 5. 支持不确定字段条件处理`DataScopeUtil.addGlobalLikeProperty` - 6. 支持线程层面的数据权限设置 + 6. 支持线程层面的不确定字段处理`DataScopeUtil.putThreadDataScopeProperty(globalProperty)` + 注意:对于这些不确定字段,因为可能存在相同语言的字段,但名称不同,尽量将特殊的字段名设置在前面 5. 配置类示例 @@ -90,6 +91,17 @@ public interface DemoMapper extends MPJBaseMapper { columnProperty.setValue("ht"); columnProperty.setOperator("like"); globalProperty.setColumns(Arrays.asList(columnProperty)); - DataScopeUtil.addGlobalProperty(globalProperty); + DataScopeUtil.putGlobalProperty(globalProperty); + 示例三:设置线程权限 + + DataScopeProperty globalProperty=new DataScopeProperty(); + DataColumnProperty columnProperty=new DataColumnProperty(); + columnProperty.setName("url"); + columnProperty.setValue("ht"); + columnProperty.setOperator("like"); + globalProperty.setColumns(Arrays.asList(columnProperty)); + DataScopeUtil.putThreadDataScopeProperty(globalProperty); + //...过滤器链业务逻辑 + DataScopeUtil.destroyThread(); ``` \ No newline at end of file diff --git a/src/main/java/com/liry/mybatis/datascope/interceptor/DataScopeInnerInterceptor.java b/src/main/java/com/liry/mybatis/datascope/interceptor/DataScopeInnerInterceptor.java index 52b1e580a3f632fe7fab73098808083544dd93e7..225b600548bc219ffd3594826d1a7eb05894126e 100644 --- a/src/main/java/com/liry/mybatis/datascope/interceptor/DataScopeInnerInterceptor.java +++ b/src/main/java/com/liry/mybatis/datascope/interceptor/DataScopeInnerInterceptor.java @@ -32,6 +32,7 @@ import net.sf.jsqlparser.statement.Statements; import net.sf.jsqlparser.statement.select.FromItem; import net.sf.jsqlparser.statement.select.PlainSelect; import net.sf.jsqlparser.statement.select.Select; +import net.sf.jsqlparser.statement.select.SubSelect; import org.apache.ibatis.mapping.BoundSql; import org.apache.ibatis.mapping.MappedStatement; import org.apache.ibatis.mapping.SqlCommandType; @@ -64,35 +65,22 @@ public class DataScopeInnerInterceptor { StringBuilder sb = new StringBuilder(); for (Statement statement : statements.getStatements()) { if (statement instanceof Select) { - PlainSelect selectBody = (PlainSelect) ((Select) statement).getSelectBody(); - where(selectBody, args, dataScopeProperty); + PlainSelect plainSelect = (PlainSelect) ((Select) statement).getSelectBody(); + where(plainSelect, args, dataScopeProperty); - String tableName = ((Table) selectBody.getFromItem()).getName(); - List tableColumns = MybatisPlusUtil.getTableColumns(tableName); - - // 不确定的字段 - if (likeProperty != null) { - for (DataColumnProperty column : likeProperty.getColumns()) { - if (tableColumns.contains(column.getName())) { - where(selectBody, args, new DataScopeProperty(column)); - break; - } - } - } - // 当前线程 - if (threadDataScopeProperty != null) { - for (DataColumnProperty column : threadDataScopeProperty.getColumns()) { - if (tableColumns.contains(column.getName())) { - where(selectBody, args, new DataScopeProperty(column)); - break; - } + if (plainSelect.getFromItem() instanceof Table) { + tableSelectBuild(plainSelect, args, likeProperty, threadDataScopeProperty); + } else if (plainSelect.getFromItem() instanceof SubSelect) { + if (ms.getId().endsWith("_mpCount")) { + SubSelect subSelect = (SubSelect) plainSelect.getFromItem(); + PlainSelect subPlainSelect = (PlainSelect) subSelect.getSelectBody(); + tableSelectBuild(subPlainSelect, args, likeProperty, threadDataScopeProperty); } } - sb.append(selectBody).append(";"); + sb.append(plainSelect); } } - MappedStatement newMs = buildMappedStatement(ms, ms.getSqlCommandType(), sb.toString(), boundSql); // 将旧的声明替换 args[0] = newMs; @@ -105,6 +93,41 @@ public class DataScopeInnerInterceptor { } } + /** + * 构建selectbody + * + * @param plainSelect select 对象 + * @param args 实际参数 + * @param likeProperty 全局参数对象 + * @param threadDataScopeProperty 全局线程参数对象 + */ + private static void tableSelectBuild(PlainSelect plainSelect, Object[] args, DataScopeProperty likeProperty, + DataScopeProperty threadDataScopeProperty) { + if (plainSelect.getFromItem() instanceof Table) { + String tableName = ((Table) plainSelect.getFromItem()).getName(); + List tableColumns = MybatisPlusUtil.getTableColumns(tableName); + + // 不确定的字段 + if (likeProperty != null) { + for (DataColumnProperty column : likeProperty.getColumns()) { + if (tableColumns.contains(column.getName())) { + where(plainSelect, args, new DataScopeProperty(column)); + break; + } + } + } + // 当前线程 + if (threadDataScopeProperty != null) { + for (DataColumnProperty column : threadDataScopeProperty.getColumns()) { + if (tableColumns.contains(column.getName())) { + where(plainSelect, args, new DataScopeProperty(column)); + break; + } + } + } + } + } + /** * 构建新的声明 * diff --git a/src/main/java/com/liry/mybatis/datascope/interceptor/MybatisDataScopeInterceptor.java b/src/main/java/com/liry/mybatis/datascope/interceptor/MybatisDataScopeInterceptor.java index 4a7af042f8b6b894e938d9879c37a6e3484f895d..5168ec0094d8ad0dac32fc257365308fd0c1ad33 100644 --- a/src/main/java/com/liry/mybatis/datascope/interceptor/MybatisDataScopeInterceptor.java +++ b/src/main/java/com/liry/mybatis/datascope/interceptor/MybatisDataScopeInterceptor.java @@ -29,6 +29,8 @@ import org.apache.ibatis.session.RowBounds; ) public class MybatisDataScopeInterceptor implements Interceptor { + public static final String COUNT = "_COUNT"; + @Override public Object intercept(Invocation invocation) throws Throwable { Object target = invocation.getTarget();