In the ruoyi framework, I see a piece of code
${params.dataScope} The usage is the first time I saw it , I was shocked when I saw it from behind . It's not how difficult the technology is , But I have never considered it from this angle mybatis That's how it works . Very flexible

where d.del_flag = ‘0’

AND parent_id = #{parentId}

AND dept_name like concat(’%’, #{deptName}, ‘%’)

AND status = #{status}

${params.dataScope}
order by d.parent_id, d.order_num

here params.dataScope It's just a string , It represents a string sql, adopt $ Splice to the end .

Then let's see how ruoyi is stitched together
Here is the annotation position ,server Layer to get the location of the Department list
/** * Query department management data * * @param dept Department information * @return Department information collection */ @Override
@DataScope(deptAlias = "d") public List<SysDept> selectDeptList(SysDept dept) {
return deptMapper.selectDeptList(dept); }
Here's the point , Dynamic stitching through section params.dataScope parameter , And change it sql Scope of query .
The section is taken in @DataScope The method of annotation is cut in and processed before execution , /** * Data filtering processing * * @author sj */ @Aspect
@Component public class DataScopeAspect { /** * All data permissions */ public static final
String DATA_SCOPE_ALL = "1"; /** * Custom data permissions */ public static final String
DATA_SCOPE_CUSTOM = "2"; /** * Department data authority */ public static final String
DATA_SCOPE_DEPT = "3"; /** * Data authority of departments and below */ public static final String
DATA_SCOPE_DEPT_AND_CHILD = "4"; /** * Personal Data permission only */ public static final String
DATA_SCOPE_SELF = "5"; /** * Data permission filtering keywords */ public static final String DATA_SCOPE
= "dataScope"; // Configure weaving point
@Pointcut("@annotation(com.sj.common.annotation.DataScope)") public void
dataScopePointCut() { } @Before("dataScopePointCut()") public void
doBefore(JoinPoint point) throws Throwable { handleDataScope(point); }
protected void handleDataScope(final JoinPoint joinPoint) { // Get comments DataScope
controllerDataScope = getAnnotationLog(joinPoint); if (controllerDataScope ==
null) { return; } // Get the current user LoginUser loginUser =
SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
if (StringUtils.isNotNull(loginUser)) { SysUser currentUser =
loginUser.getUser(); // If you are a super administrator , The data is not filtered if (StringUtils.isNotNull(currentUser)
&& !currentUser.isAdmin()) { dataScopeFilter(joinPoint, currentUser,
controllerDataScope.deptAlias(), controllerDataScope.userAlias()); } } } /** *
Data range filtering * * @param joinPoint Tangent point * @param user user * @param userAlias alias */ public
static void dataScopeFilter(JoinPoint joinPoint, SysUser user, String
deptAlias, String userAlias) { StringBuilder sqlString = new StringBuilder();
for (SysRole role : user.getRoles()) { String dataScope = role.getDataScope();
if (DATA_SCOPE_ALL.equals(dataScope)) { sqlString = new StringBuilder(); break;
} else if (DATA_SCOPE_CUSTOM.equals(dataScope)) {
sqlString.append(StringUtils.format( " OR {}.dept_id IN ( SELECT dept_id FROM
sys_role_dept WHERE role_id = {} ) ", deptAlias, role.getRoleId())); } else if
(DATA_SCOPE_DEPT.equals(dataScope)) { sqlString.append(StringUtils.format(" OR
{}.dept_id = {} ", deptAlias, user.getDeptId())); } else if
(DATA_SCOPE_DEPT_AND_CHILD.equals(dataScope)) {
sqlString.append(StringUtils.format( " OR {}.dept_id IN ( SELECT dept_id FROM
sys_dept WHERE dept_id = {} or find_in_set( {} , ancestors ) )", deptAlias,
user.getDeptId(), user.getDeptId())); } else if
(DATA_SCOPE_SELF.equals(dataScope)) { if (StringUtils.isNotBlank(userAlias)) {
sqlString.append(StringUtils.format(" OR {}.user_id = {} ", userAlias,
user.getUserId())); } else { // The data permission is only for myself and none userAlias Alias does not query any data
sqlString.append(" OR 1=0 "); } } } if
(StringUtils.isNotBlank(sqlString.toString())) { Object params =
joinPoint.getArgs()[0]; if (StringUtils.isNotNull(params) && params instanceof
BaseEntity) { BaseEntity baseEntity = (BaseEntity) params;
baseEntity.getParams().put(DATA_SCOPE, " AND (" + sqlString.substring(4) +
")"); } } } /** * Is there a comment , If it exists, get it */ private DataScope
getAnnotationLog(JoinPoint joinPoint) { Signature signature =
joinPoint.getSignature(); MethodSignature methodSignature = (MethodSignature)
signature; Method method = methodSignature.getMethod(); if (method != null) {
return method.getAnnotation(DataScope.class); } return null; } }
As a pre knowledge, you need to understand the role management page , You can configure the following data ranges /** * All data permissions */ public static final String
DATA_SCOPE_ALL = "1"; Do nothing /** * Custom data permissions */ public static final String
DATA_SCOPE_CUSTOM = "2"; query In the corresponding table of department roles , Which departments can users manage OR d.dept_id IN ( SELECT
dept_id FROM sys_role_dept WHERE role_id = {rolelist} ) /** * Department data authority */ public
static final String DATA_SCOPE_DEPT = "3"; Query department number = Data of user's department number OR {}.dept_id =
{} /** * Data authority of departments and below */ public static final String DATA_SCOPE_DEPT_AND_CHILD =
"4"; Query the authority of departments and sub departments /** * Personal Data permission only */ public static final String DATA_SCOPE_SELF =
"5"; if (StringUtils.isNotBlank(userAlias)) {
sqlString.append(StringUtils.format(" OR {}.user_id = {} ", userAlias,
user.getUserId())); } else { // The data permission is only for myself and none userAlias Alias does not query any data
sqlString.append(" OR 1=0 "); }
summary :

This is the use of mybatis What's more ingenious in grammar , It is currently used in the Department , Use this method to reform the project , It can adapt to the specific data requirements of the project .

Technology
©2019-2020 Toolsou All rights reserved,
【Java8 New features 1】Lambda Expression summary What is a process , Concept of process ?hdfs dfs Common basic commands java When creating objects, you must _Java Basic test questions Generation of random numbers + Figure guessing game When will we enter the old generation ?HDFS Common commands ( summary ) It is never recommended to spend a lot of time studying for employment pythonmacOS Big Sur The installation could not be completed Big Sur Why can't I install it ?Python pyttsx3| Text reading ( Various languages )