若依框架实体类增加自定义注解,自动插入创建人、时间等信息

该文章优化了若依框架,在新增和修改方法时,之前每次都需要手动注入基础信息。增加aop注解之后,只需要在新增和修改方法上增加注解就行。

1.自定义注解

package com.fuel.common.annotation;

import java.lang.annotation.*;

/**
 * 业务数据过滤注解(使用单位、厂商、经销商、站点、监管等)
 * 
 * @author zwg
 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface AddOrUpdateFilter
{
    /**
     * 新增
     */
    public boolean add() default false;

    /**
     * 修改
     */
    public boolean edit() default false;
}

2.数据过滤处理

package com.fuel.framework.aspectj;

import com.fuel.common.annotation.AddOrUpdateFilter;
import com.fuel.common.core.domain.BaseEntity;
import com.fuel.common.core.domain.entity.SysUser;
import com.fuel.common.core.domain.model.LoginUser;
import com.fuel.common.utils.DateUtils;
import com.fuel.common.utils.SecurityUtils;
import com.fuel.common.utils.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 数据过滤处理
 *
 * @author hhxx
 */
@Aspect
@Component
public class AddOrUpdateAspect
{

    @Before("@annotation(addOrUpdateFilter)")
    public void doBefore(JoinPoint point, AddOrUpdateFilter addOrUpdateFilter) throws Throwable
    {
        handleDataScope(point, addOrUpdateFilter);
    }

    protected void handleDataScope(final JoinPoint joinPoint, AddOrUpdateFilter addOrUpdateFilter)
    {
        // 获取当前的用户
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (StringUtils.isNotNull(loginUser))
        {
            SysUser currentUser = loginUser.getUser();
            dataScopeFilter(joinPoint, currentUser, addOrUpdateFilter);
        }
    }

    /**
     * 操作过滤
     *
     * @param joinPoint 切点
     * @param user 用户
     * @param addOrUpdateFilter 操作过滤
     */
    public static void dataScopeFilter(JoinPoint joinPoint, SysUser user, AddOrUpdateFilter addOrUpdateFilter)
    {
        String userName = user.getUserName();
        Date nowDate = DateUtils.getNowDate();
        Map paramsMap = new HashMap<>();
        boolean add = addOrUpdateFilter.add(); //新增
        boolean edit = addOrUpdateFilter.edit(); //修改

        Object params = joinPoint.getArgs()[0];
        if (StringUtils.isNotNull(params))
        {
            if(params instanceof BaseEntity){
                BaseEntity baseEntity = (BaseEntity) params;
                if(add) {
                    baseEntity.setCreateBy(userName);
                    baseEntity.setCreateTime(nowDate);
                    baseEntity.setUpdateBy(userName);
                    baseEntity.setUpdateTime(nowDate);
                }else if(edit) {
                    baseEntity.setUpdateBy(userName);
                    baseEntity.setUpdateTime(nowDate);
                }
            }
        }
    }
}

3.代码生成增加注解

    /**
     * 新增${functionName}
     * 
     * @param ${className} ${functionName}
     * @return 结果
     */
#if($table.sub)
    @Transactional
#end
    @Override
    @AddOrUpdateFilter(add = true)
    public int insert${ClassName}(${ClassName} ${className})
    {
#foreach ($column in $columns)
#if($column.javaField == 'createTime')
        ${className}.setCreateTime(DateUtils.getNowDate());
#end
#end
#if($table.sub)
        int rows = ${className}Mapper.insert${ClassName}(${className});
        insert${subClassName}(${className});
        return rows;
#else
        return ${className}Mapper.insert${ClassName}(${className});
#end
    }

    /**
     * 修改${functionName}
     * 
     * @param ${className} ${functionName}
     * @return 结果
     */
#if($table.sub)
    @Transactional
#end
    @Override
    @AddOrUpdateFilter(edit = true)
    public int update${ClassName}(${ClassName} ${className})
    {
#foreach ($column in $columns)
#if($column.javaField == 'updateTime')
        ${className}.setUpdateTime(DateUtils.getNowDate());
#end
#end

4.代码示例:

 /**
     * 新增设备信息
     *
     * @param fuelDevice 设备信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @AddOrUpdateFilter(add = true)
    public int insertFuelDevice(FuelDevice fuelDevice) {
        String deviceNo = createDeviceNo();
        fuelDevice.setDeviceNo(deviceNo);
        String username = SecurityUtils.getUsername();
        fuelDevice.setActiveStatus("0");
        fuelDevice.setTankNum(0);
        fuelDevice.setFuelGunNum(0);
        String isMonitor = fuelDevice.getIsMonitor();
        if ("N".equals(isMonitor)) {
            fuelDevice.setAlarmStatus("9");
            fuelDevice.setOnlineStatus("9");
        }else if ("Y".equals(isMonitor)){
            fuelDevice.setAlarmStatus("0");
            fuelDevice.setOnlineStatus("2");
        }
        int insertFuelDevice = fuelDeviceMapper.insertFuelDevice(fuelDevice);
        return insertFuelDevice;
    }

你可能感兴趣的:(java,开发语言)