【一】day08/23(个人笔记)

可商业权限管理系统:

1.数据结构

Set<Permission> savedPermissions = new LinkedHashSet<>();
LinkedHashSet:不可重复,有序

2.权限管理
拓展API:
getBeansWithAnnotation : spring中可以通过getBeansWithAnnotation来获取ioc容器中使用了某个注解的所有bean

这个方法底层原理:我们可以通过反射来获取一个类中所有的注解,然后通过遍历spring容器中所有bean就能获取需要(想要)的结果

@RequestMapping("/load")
@ResponseBody
//不用流的方式
public JsonResult load() {
    List<Permission> permissions = permissionService.listAll();
    Set<Permission> savedPermissions = new LinkedHashSet<>();

    try {
        //1.通过应用程序上下文容器获取所有控制器注解的字节码
        Map<String, Object> beans = ctx.getBeansWithAnnotation(Controller.class);
        //2.获取注解的值
        Collection<Object> coo = beans.values();
        for (Object co : coo) {
            //3.遍历注解中的方法
            for (Method method : co.getClass().getDeclaredMethods()) {
                //4.获取想要注解的注解
                RequiredPermission annotation = method.getAnnotation(RequiredPermission.class);
                //5.判断,执行业务
                if (annotation != null) {
                    String name = annotation.name();
                    String expression = annotation.expression();
                    Permission permission = new Permission();
                    permission.setName(name);
                    permission.setExpression(expression);
                    if (!permissions.contains(permission)) {
                        savedPermissions.add(permission);
                    }
                }
            }
        }
        permissionService.batchSave(savedPermissions);
    } catch (Exception e) {
        return new JsonResult(false, "加载失败");
    }
    return new JsonResult(true, "加载成功");
}

//用流的方式
    @RequestMapping("/load")
    @ResponseBody
    public JsonResult load() {
        List<Permission> permissions = permissionService.listAll();
        try {
        //获取带注解的bean对象的值并且转成流的形式
            Set<Permission> savedPermissions = ctx.getBeansWithAnnotation(Controller.class).values().stream()
                    .flatMap(controller -> Arrays.stream(controller.getClass().getDeclaredMethods()))
                    //过滤
                    .filter(method -> method.getAnnotation(RequiredPermission.class) != null)
                    //转形式(对象)
                    .map(method -> new Permission(method.getAnnotation(RequiredPermission.class).name(), method.getAnnotation(RequiredPermission.class).expression()))
                    //过滤数据库有的数据
                    .filter(permission -> !permissions.contains(permission))
                    //转成set
                    .collect(Collectors.toSet());
            if (savedPermissions.size() > 0) {
                permissionService.batchSave(savedPermissions);
            }
        } catch (Exception e) {
            return new JsonResult(false, "加载失败");
        }
        return new JsonResult(true, "加载成功");
    }

3.maven中sql循环写法:

<insert id="batchInsert">
    insert into permission (name, expression) values
    <foreach collection="savePermissions" item="p" separator=",">	 // collection:参数名 item:别名	separator:fen'ge
        (#{p.name},#{p.expression})
    </foreach>
</insert>

4.新的 A P I:

Array.Stream()返回的是一个元素序列,且支持顺序和并行的聚合操作。其实我们可以把它理解为包装类
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值