aop实现mybetis插入或更新数据时,自动补全某些字段

aop与定时任务依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--springBoot aop-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>

aop

package com.tenzont.worksitejbq.sysadmin;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableAspectJAutoProxy(proxyTargetClass = true)
@EnableCaching
@EnableAsync
@EnableScheduling//开启定时任务
@ComponentScan(basePackages = {"com.tenzont.worksitejbq"})
public class SysadminApplication {

    public static void main(String[] args) {
        SpringApplication.run(SysadminApplication.class, args);
    }

}
## aop切面类代码
```java
package com.tenzont.worksitejbq.sysadmin.config;
import com.tenzont.worksitejbq.common.model.BaseDataSouceModel;
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;
/**
 * 基本添加人、修改人及时间参数补全
 */
@Component
@Aspect
public class AUTimeAspect {
    //在插入数据前增强方法,实现自动补全数据
    //@annotation(org.apache.ibatis.annotations.InsertProvider)-->mybetis的inset与insertSelective方法都会被捕获
    @Before("@annotation(org.apache.ibatis.annotations.InsertProvider)")
    public void setInsertValue(JoinPoint joinPoint) {
        //获取方法的参数数组
        Object[] args = joinPoint.getArgs();
        //BaseDataSouceModel中放的是需要自动补全的属性,让实体类继承它
        //做出判断args[0] instanceof BaseDataSouceModel-->第一个参数是否是BaseDataSouceModel类的实体,或者其子类实体
        if (args != null && args.length == 1 && args[0] instanceof BaseDataSouceModel) {
            BaseDataSouceModel arg = (BaseDataSouceModel) args[0];
            if (arg.getCreateTime() == null)
                //设置时间
                arg.setCreateTime(new Date());
            if (arg.getCreateUser() == null) {
                //设置创建数据用户的ID
                arg.setCreateUser(ShiroUserUtil.getUserId());
            }
        }
    }
    //在更新数据前增强方法,实现自动补全数据
    @Before("@annotation(org.apache.ibatis.annotations.UpdateProvider)")
    public void setUpdateValue(JoinPoint joinPoint) {
        Object[] args = joinPoint.getArgs();
        if (args != null && args.length == 1 && args[0] instanceof BaseDataSouceModel) {
            BaseDataSouceModel arg = (BaseDataSouceModel) args[0];
            arg.setUpdateTime(new Date());
            arg.setUpdateUser(ShiroUserUtil.getUserId());
        }
    }
}

定时器测试插入代码

package com.tenzont.worksitejbq.sysadmin.task;
import com.tenzont.worksitejbq.common.mapper.SysUserMapper;
import com.tenzont.worksitejbq.common.model.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class GldSchedule {

//    定时任务测试
    @Autowired
    private SysUserMapper sysUserMapper;
    @Scheduled(fixedDelay = 300000, initialDelay = 5000)
    public void dayLisGldt1() {
        SysUser sysUser = SysUser.builder()
                .nickName("无敌的我")
                .phone("17639644155")
                .status("1")
                .userName("宫本")
                .organizationId(2)
                .remark("测试")
                .build();
        sysUserMapper.insertSelective(sysUser);
        System.out.println("测试完成");
    }

}

启动类

package com.tenzont.worksitejbq.sysadmin;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableAspectJAutoProxy(proxyTargetClass = true)
@EnableCaching
@EnableAsync
@EnableScheduling//开启定时任务
@ComponentScan(basePackages = {"com.tenzont.worksitejbq"})
public class SysadminApplication {

    public static void main(String[] args) {
        SpringApplication.run(SysadminApplication.class, args);
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值