♻️ Refactoring code. 数据权限扩展支持 mybatis-plus 自带方法

This commit is contained in:
冷冷 committed 2020-04-26 20:04:13 +08:00
1 parent 6f50d63fdc
commit 6cba0a8ba6
8 files changed
+116 -17

No files matched your search

@@ -0,0 +1,38 @@
package com.pig4cloud.pigx.common.data.datascope;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* @author lengleng
* @date 2020/4/26
*/
public interface DataScopeMapper<T> extends BaseMapper<T> {
/**
* 根据 entity 条件,查询全部记录
*
* @param queryWrapper 实体对象封装操作类(可以为 null)
* @param scope 数据权限范围
* @return List<T>
*/
List<T> selectListByScope(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper, DataScope scope);
/**
* 根据 entity 条件,查询全部记录(并翻页)
*
* @param page 分页查询条件(可以为 RowBounds.DEFAULT)
* @param queryWrapper 实体对象封装操作类(可以为 null)
* @param scope 数据权限范围
* @return Page
*/
<E extends IPage<T>> E selectPageByScope(E page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper, DataScope scope);
}
@@ -0,0 +1,21 @@
package com.pig4cloud.pigx.common.data.datascope;
import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.injector.DefaultSqlInjector;
import java.util.List;
/**
* @author lengleng
* @date 2020/4/26
*/
public class DataScopeSqlInjector extends DefaultSqlInjector {
@Override
public List<AbstractMethod> getMethodList(Class<?> mapperClass) {
List<AbstractMethod> methodList = super.getMethodList(mapperClass);
methodList.add(new SelectListByScope());
methodList.add(new SelectPageByScope());
return methodList;
}
}
@@ -0,0 +1,22 @@
package com.pig4cloud.pigx.common.data.datascope;
import com.baomidou.mybatisplus.core.enums.SqlMethod;
import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlSource;
/**
* @author lengleng
* @date 2020/4/26
*/
public class SelectListByScope extends AbstractMethod {
@Override
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
SqlMethod sqlMethod = SqlMethod.SELECT_LIST;
String sql = String.format(sqlMethod.getSql(), sqlFirst(), sqlSelectColumns(tableInfo, true), tableInfo.getTableName(),
sqlWhereEntityWrapper(true, tableInfo), sqlComment());
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
return this.addSelectMappedStatementForTable(mapperClass, "selectListByScope", sqlSource, tableInfo);
}
}
@@ -0,0 +1,24 @@
package com.pig4cloud.pigx.common.data.datascope;
import com.baomidou.mybatisplus.core.enums.SqlMethod;
import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlSource;
/**
* @author lengleng
* @date 2020/4/26
*/
public class SelectPageByScope extends AbstractMethod {
@Override
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
SqlMethod sqlMethod = SqlMethod.SELECT_PAGE;
String sql = String.format(sqlMethod.getSql(), sqlFirst(), sqlSelectColumns(tableInfo, true),
tableInfo.getTableName(), sqlWhereEntityWrapper(true, tableInfo), sqlComment());
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
return this.addSelectMappedStatementForTable(mapperClass, "selectPageByScope", sqlSource, tableInfo);
}
}
@@ -22,6 +22,7 @@ import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.tenant.TenantSqlParser;
import com.pig4cloud.pigx.common.data.datascope.DataScopeHandle;
import com.pig4cloud.pigx.common.data.datascope.DataScopeInterceptor;
import com.pig4cloud.pigx.common.data.datascope.DataScopeSqlInjector;
import com.pig4cloud.pigx.common.data.datascope.PigxDefaultDatascopeHandle;
import com.pig4cloud.pigx.common.data.tenant.PigxTenantHandler;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
@@ -97,4 +98,10 @@ public class MybatisPlusConfiguration {
public DataScopeInterceptor dataScopeInterceptor() {
return new DataScopeInterceptor(dataScopeHandle());
}
@Bean
@ConditionalOnBean(DataScopeHandle.class)
public DataScopeSqlInjector dataScopeSqlInjector() {
return new DataScopeSqlInjector();
}
}
@@ -19,13 +19,10 @@
package com.pig4cloud.pigx.admin.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.pig4cloud.pigx.admin.api.entity.SysDept;
import com.pig4cloud.pigx.common.data.datascope.DataScope;
import com.pig4cloud.pigx.common.data.datascope.DataScopeMapper;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
/**
* <p>
* 部门管理 Mapper 接口
@@ -35,13 +32,5 @@ import java.util.List;
* @since 2018-01-20
*/
@Mapper
public interface SysDeptMapper extends BaseMapper<SysDept> {
/**
* 根据数据权限查询部门
*
* @param dataScope
* @return
*/
List<SysDept> listDepts(DataScope dataScope);
public interface SysDeptMapper extends DataScopeMapper<SysDept> {
}
@@ -51,6 +51,7 @@ import java.util.stream.Collectors;
@AllArgsConstructor
public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> implements SysDeptService {
private final SysDeptRelationService sysDeptRelationService;
private final SysDeptMapper deptMapper;
/**
* 添加信息部门
@@ -121,7 +122,7 @@ public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> impl
*/
@Override
public List<DeptTree> selectTree() {
return getDeptTree(baseMapper.listDepts(new DataScope()));
return getDeptTree(deptMapper.selectListByScope(Wrappers.emptyWrapper(), new DataScope()));
}
@@ -21,7 +21,4 @@
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.pig4cloud.pigx.admin.mapper.SysDeptMapper">
<select id="listDepts" resultType="com.pig4cloud.pigx.admin.api.entity.SysDept">
SELECT * FROM sys_dept WHERE del_flag = '0'
</select>
</mapper>