Mybatis注解权限

​ 我们在写项目的时候,很多时候都是需要判断权限,比如你是哪个部门的有那个权限,比如你是哪个层级的管理需要那个权限,这个时候我们不能再SQL中固定死,其主要原因是因为我们不知道增加多少权限或者分组,所以这个时候就需要注解的闪亮登场了!

编写注解

package com.pv3.springboot_base.Annotation;

import java.lang.annotation.*;

/** @author sulwan */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface DataScope {
  public String deptAlias() default "";
}

编写切面

package com.pv3.springboot_base.Aspectj;

import com.pv3.springboot_base.Annotation.DataScope;
import com.pv3.springboot_base.Domain.BaseDomain;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

@Aspect
@Component
public class DataScopeAspect {
  /** 配置织入点 */
  @Pointcut("@annotation(com.pv3.springboot_base.Annotation.DataScope)")
  public void dataScopePointCut() {}

  /**
   * 织入点之前执行
   *
   * @param point
   * @throws Throwable
   */
  @Before("dataScopePointCut()")
  public void doBefore(JoinPoint point) throws Throwable {
    handleDataScope(point);
  }

  /** @param joinPoint */
  protected void handleDataScope(final JoinPoint joinPoint) {

    Signature signature = joinPoint.getSignature();
    MethodSignature methodSignature = (MethodSignature) signature;
    Method method = methodSignature.getMethod();
    if (method != null) {
      DataScope controllerDataScope = method.getAnnotation(DataScope.class);
      dataScopeFilter(joinPoint, controllerDataScope.deptAlias());
    } else {
      return;
    }
  }

  /** 数据范围过滤 */
  public static void dataScopeFilter(JoinPoint joinPoint, String deptAlias) {
    BaseDomain baseEntity = (BaseDomain) joinPoint.getArgs()[0];
    System.out.println("------------------");
    System.out.println(baseEntity);
    System.out.println("------------------");
    baseEntity.getParams().put("dataScope", "  where 1=1 ");
    System.out.println("##################");
    System.out.println(baseEntity);
    System.out.println("##################");
  }
}

编写基类

package com.pv3.springboot_base.Domain;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

public class BaseDomain implements Serializable {
  // 这里就是关键了
  private Map<String, Object> params;

  public Map<String, Object> getParams() {
    if (params == null) {
      params = new HashMap<>();
    }
    return params;
  }

  public void setParams(Map<String, Object> params) {
    this.params = params;
  }

  @Override
  public String toString() {
    return "BaseDomain{" + "params=" + params + '}';
  }
}

别写实体类继承基类

package com.pv3.springboot_base.Domain;

public class UserDomain extends BaseDomain {
  private String username;

  public String getUsername() {
    return username;
  }

  public void setUsername(String username) {
    this.username = username;
  }
}

Mybatis xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.pv3.springboot_base.Mapper.UserMapper">

    <select id="select" parameterType="com.pv3.springboot_base.Domain.UserDomain" resultType="java.util.HashMap">
        SELECT * FROM `sys_user`
    // 看清楚这句话,这个是关键
		${params.dataScope}
    </select>
</mapper> 

编写Controller

  @RequestMapping("/mybatis")
  public String mybatis() {
    UserDomain userDomain = new UserDomain();
    userDomain.setUsername("张三");

    List<Map<String, Object>> select = userService.select(userDomain);
    System.out.println(select);
    return "mybatis演示";
  }

Mapper没有写,大家应该会,我就不写了,最后效果截图

image-20200403185126650