springboot项目实战之简单aop定义使用

Spring AOP面向切面编程

AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期间动态代理实现程序功能的统一维护的一种技术。

Springboot实战

1. 定义名称,如下  ManagerOperatorRole 

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface ManagerOperatorRole {
}

2. 定义切面

@Configuration
@Aspect
public class ManagerAop {
    @Autowired
    HttpServletRequest request;

    @Autowired
    private DdMemberRolesMapper memberRolesMapper;

    @Pointcut("@annotation(com.ruoyi.shop.api.aop.ManagerOperatorRole)")
    private void permissionCheck() {}

    @Around("permissionCheck()")
    public Object around(ProceedingJoinPoint p) throws Throwable{
        ***********************略  以下示例
        DdMemberRoles roles=new DdMemberRoles();
        roles.setMemberId(user.getUserId());
        roles.setRolesType(Constants.MARKET_ROLE);
        List ddMemberRoles=memberRolesMapper.selectDdMemberRolesList(roles);
        if(CollUtil.size(ddMemberRoles)==0){
            return AjaxResult.warn("无权访问");
        }
        /**
         if (ddMemberRoles == null || ddMemberRoles.isEmpty()) {
             return AjaxResult.warn("无权访问");
         }

         try {
             Map response = (Map) p.proceed();
             response.put("marketId", ddMemberRoles.get(0).getMarketId());
             return response;
         } catch (Exception e) {
             log.error("Error occurred: ", e);
             return AjaxResult.error(e.getMessage());
         }
         */
       ***********略
        return response;
    }



}

 3. controller 案例使用

@GetMapping("getMerchApplyList")
    @ManagerOperatorRole
    public AjaxResult getMerchApplyList(@RequestParam(name = "page",defaultValue = "1")int page,
                                        @RequestParam(name="status",defaultValue = "0") String status){
        MPJLambdaWrapper wrapper=new MPJLambdaWrapper()
                .selectAll(ApplyMerchVo.class)
                .selectAs(DdMarketArea::getAreaName,ApplyMerchVo::getAreaName)
                .selectAs(MarketCateDto::getCateName,ApplyMerchVo::getCateName)
                .selectAs(DdMarket::getMarketName,ApplyMerchVo::getMarketName)
                .leftJoin(MarketCateDto.class, MarketCateDto::getMarketCateId, ApplyMerchVo::getCategoryId)
                .leftJoin(DdMarketArea.class, DdMarketArea::getAreaId, ApplyMerchVo::getAreaId)
                .leftJoin(DdMarket.class,DdMarket::getMarketId,ApplyMerchVo::getMarketId)
                .in(ApplyMerchVo::getStatus, StrUtil.split(status, ","))
                .orderByDesc(ApplyMerchVo::getUpdateTime);//降序排序

        //分页查询
        Page listPage = ddMarketMerchApplyMapper.selectJoinPage(new Page<>(page, 10), ApplyMerchVo.class, wrapper);

        return AjaxResult.success("获取成功",listPage.getRecords());

    }

你可能感兴趣的:(spring,boot,java,后端)