# mybatis-plus-datascope **Repository Path**: by-yyds/mybatis-plus-datascope ## Basic Information - **Project Name**: mybatis-plus-datascope - **Description**: 基于mybatis-plus的数据权限。支持指定条件,支持全局配置条件,支持动态条件,支持线程变量条件,适配mybatis-plus-join和mybatis分页插件 - **Primary Language**: Java - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 4 - **Created**: 2023-08-07 - **Last Updated**: 2023-08-07 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 说明 ### 2022年9月14日16:03:22 1. 该插件基于mybatis-plus 3.5.1 开发,支持3.5.0 + 2. 同时也支持mybatis-plus-join 3. 配置注意点: 1. 因为配置mybatis-plus-join 需要重写mybatis-plus的sqlSessionFactory,会导致mybatis-plus基本的全局配置需要手动注入, 所以该项目引入了`MybatisDataScopeConfig.sqlSessionFactoryBean`可以重新构建sqlSessionFactory 2. 该插件的引入只需要,添加插件即可,需要将插件添加到第一个,如下 > factory.setPlugins(new MybatisForMeiyaInterceptor(), mybatisPlusInterceptor, mpjInterceptor); 4. 使用注意点: 1. `DataScopeUtil.addGlobalProperty`可以添加全局数据权限 2. 添加的全局数据权限,和独立指定的数据权限会合并,独立指定的权限高于全局的 3. 支持动态添加数据权限`DataScopeUtil.put(方法全名, DataScopeProperty)` 4. 暂不支持自定义条件构建,并且,这个版本只支持=,>,>=,<,<=,in,not in,like 5. 支持不确定字段条件处理`DataScopeUtil.addGlobalLikeProperty` 6. 支持线程层面的不确定字段处理`DataScopeUtil.putThreadDataScopeProperty(globalProperty)` 注意:对于这些不确定字段,因为可能存在相同语言的字段,但名称不同,尽量将特殊的字段名设置在前面 5. 配置类示例 ```java // mybatis-plus 填充处理器 @Autowired private MybatisMetaObjectHandler mybatisMetaObjectHandler; // mybatis-plus 配置 @Autowired private MybatisPlusProperties properties; // 该插件的配置类 @Autowired private MybatisDataScopeConfig mybatisDataScopeConfig; @Bean public MybatisPlusInterceptor mybatisPlusInterceptor(){ MybatisPlusInterceptor interceptor=new MybatisPlusInterceptor(); interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor()); return interceptor; } @Bean public GlobalConfig globalConfig(MPJSqlInjector mpjSqlInjector){ // 这里使用的mybatis-plus配置进行修改 GlobalConfig config=properties.getGlobalConfig(); config.setSqlInjector(mpjSqlInjector); // 将MybatisPlus的配置加进来 config.setMetaObjectHandler(mybatisMetaObjectHandler); return config; } @Bean public SqlSessionFactory sqlSessionFactory(DataSource dataSource, MybatisPlusInterceptor mybatisPlusInterceptor, GlobalConfig globalConfig, MPJInterceptor mpjInterceptor)throws Exception{ MybatisSqlSessionFactoryBean factory=mybatisDataScopeConfig.sqlSessionFactoryBean( dataSource,globalConfig); // 添加插件,这里的顺序依然不变 factory.setPlugins(msInterceptor(),mybatisPlusInterceptor,mpjInterceptor); return factory.getObject(); } public MybatisDataScopeInterceptor msInterceptor(){ return new MybatisDataScopeInterceptor(); } ``` 6. 使用示例 ```java 示例一:指定方法的权限 // mapper下的所有方法,都会添加字段[name like '%test%']的条件 @DataScope({@DataColumn(name = "name", value = "%test%", operator = "like")}) public interface DemoMapper extends MPJBaseMapper { // 方法上的注释优先与类上注释 @DataScope({@DataColumn(name = "db_name", value = "t", operator = "=")}) List getList(@Param("map") Map map); } 示例二:设置全局权限 DataScopeProperty globalProperty=new DataScopeProperty(); DataColumnProperty columnProperty=new DataColumnProperty(); columnProperty.setName("url"); columnProperty.setValue("ht"); columnProperty.setOperator("like"); globalProperty.setColumns(Arrays.asList(columnProperty)); 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(); ```