AOP统一处理修改人、创建人、修改时间、创建时间
1.配置拦截
首先开启
<aop:aspectj-autoproxy proxy-target-class="true"/>代理。解释一下下面..的意思是多个
<bean id="dalAop" class="com.biz.common.aop.DalAop" />
<aop:config>
<aop:aspect ref="dalAop" >
<aop:before method="beforeInsert" pointcut="execution(* com.dal.*.mapper..*.insert*(..))" />
<aop:before method="beforeUpdate" pointcut="execution(* com.dal.*.mapper..*.update*(..))" />
<aop:before method="beforeUpdate" pointcut="execution(* com.dal.*.mapper..*.remove*(..))" />
<aop:before method="beforeUpdate" pointcut="execution(* com..dal.*.mapper..*.delete*(..))" />
</aop:aspect>
</aop:config>
2.实体类
package com.biz.common.aop; import java.util.Date;
import java.util.Map; import javax.annotation.Resource; import org.apache.commons.lang.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl; import com.auth.privilege.PrivilegeInfo; /**
* 类DalAop.java的实现描述:对DAO插入和更新方法的拦截,并设置公共字段的信息
*
*
*/
public class DalAop { private static final String CREATOR = "creator";
private static final String MODIFIER = "modifier";
private static final String GMTCREATE = "gmtCreate";
private static final String GMTMODIFIED = "gmtModified";
@Resource
private PrivilegeInfo privilegeInfo; public void beforeInsert(JoinPoint jp) {
Object[] args = jp.getArgs(); if (args != null && args.length > 0) {
Object argument = args[0];
BeanWrapper beanWrapper = new BeanWrapperImpl(argument);
// 设置创建时间和修改时间
if (beanWrapper.isWritableProperty(GMTCREATE)) {
beanWrapper.setPropertyValue(GMTCREATE, new Date());
}
if (beanWrapper.isWritableProperty(GMTMODIFIED)) {
beanWrapper.setPropertyValue(GMTMODIFIED, new Date());
}
// 设置创建人和修改人
if (beanWrapper.isWritableProperty(CREATOR) && privilegeInfo != null
&& StringUtils.isNotBlank(privilegeInfo.getWorkNo())) {
beanWrapper.setPropertyValue(CREATOR, privilegeInfo.getWorkNo());
}
if (beanWrapper.isWritableProperty(MODIFIER) && privilegeInfo != null
&& StringUtils.isNotBlank(privilegeInfo.getWorkNo())) {
beanWrapper.setPropertyValue(MODIFIER, privilegeInfo.getWorkNo());
}
}
} @SuppressWarnings({ "rawtypes", "unchecked" })
public void beforeUpdate(JoinPoint jp) { Object[] args = jp.getArgs();
// myBatis只能传递一个参数
if (args != null && args.length > 0) {
Object argument = args[0]; // 如果argument是map类型
if (argument instanceof Map) {
Map map = (Map) argument;
map.put(GMTMODIFIED, new Date());
if (privilegeInfo != null && StringUtils.isNotBlank(privilegeInfo.getWorkNo())) {
map.put(MODIFIER, privilegeInfo.getWorkNo());
}
} else {
BeanWrapper beanWrapper = new BeanWrapperImpl(argument);
if (beanWrapper.isWritableProperty(GMTMODIFIED)) {
beanWrapper.setPropertyValue(GMTMODIFIED, new Date());
}
if (beanWrapper.isWritableProperty(MODIFIER) && privilegeInfo != null
&& StringUtils.isNotBlank(privilegeInfo.getWorkNo())) {
beanWrapper.setPropertyValue(MODIFIER, privilegeInfo.getWorkNo());
}
}
}
}
}
AOP统一处理修改人、创建人、修改时间、创建时间的更多相关文章
- 如何修改文件的 “创建时间” 和 “修改时间”(Windows Linux macOS)
请访问原文链接:https://sysin.org/blog/how-to-change-file-date,查看最新版.原创作品,转载请保留出处. 作者:gc(at)sysin.org,主页:www ...
- Linux如何查找文件的创建时间
Linux的文件能否找到文件的创建时间取决于文件系统类型,在ext4之前的早期文件系统中(ext.ext2.ext3),文件的元数据不会记录文件的创建时间,它只会记录访问时间.修改时间.更改时间(状态 ...
- Linux 如何用命令查看binlog文件的创建时间
目录 背景 分析 方法 注意 背景 MySQL在26日 16:23:49产生了大量的慢查询,在这段时间内,binlog文件刷新的很快(查看慢日志是mysql DML并发比较多),想知道写完一个binl ...
- 通过自定义特性,使用EF6拦截器完成创建人、创建时间、更新人、更新时间的统一赋值(使用数据库服务器时间赋值,接上一篇)
目录: 前言 设计(完成扩展) 实现效果 扩展设计方案 扩展后代码结构 集思广益(问题) 前言: 在上一篇文章我写了如何重建IDbCommandTreeInterceptor来实现创建人.创建时间.更 ...
- 修改SQL Server数据库表的创建时间最简单最直接有效的方法
说明:这篇文章是几年前我发布在网易博客当中的原创文章,但由于网易博客现在要停止运营了,所以我就把这篇文章搬了过来,因为这种操作方式是通用的,即使是对现在最新的SQL Server数据库里面的操作也是一 ...
- Web 在线文件管理器学习笔记与总结(2)显示文件列表(名称,类型,大小,可读,可写,可执行,创建时间,修改时间,访问时间)
主要函数: filetype() 判断文件类型 filesize() 得到文件大小(字节) is_readable() 判断文件是否可读 is_writeable() 判断文件是否可写 is_exec ...
- touch修改文件的修改时间和访问时间,ls --full-time显示文件详细,stat命令
1. 同时修改文件的修改时间和访问时间 touch -d "2010-05-31 08:10:30" test.doc 2. 只修改文件的修改时间 touch -m -d &quo ...
- Laravel / Lumen 框架修改 创建时间 和 更新时间 对应字段
为避免浪费时间--先上解决方案 在Model中重写 CREATED_AT 和 UPDATED_AT 两个类常量就可以了,这两个常量分别是创建时间和更新时间的字段名. ================= ...
- 查看和修改Linux服务器的时区和时间
一.查看和修改Linux的时区1. 查看当前时区 命令 : "date -R" 2. 修改设置Linux服务器时区方法 A 命令 : "tzselect" 方法 ...
随机推荐
- content编码
1.content有5种属性 a.content:“” //为空 b.content:attr(TItle) //可以获取当前选中标签的属性值 eg: ...
- c++ STD Gems07
reverse.rotate.permutation #include <iostream> #include <vector> #include <string> ...
- python 的第一个界面程序(PyQt5)
这里用到了python的一个第三qt库PyQt5,API与qt几乎完全一样(科学严谨下...) from PyQt5.QtWidgets import QApplication, QMainWindo ...
- HDU_2255 二分图最佳完美匹配 KM匈牙利算法
一开始还没看懂这个算法,后来看了陶叔去年的PPT的实例演示才弄懂 用一个lx[]和ly[]来记录X和Y集合中点的权值,有个定理是 lx[i]+ly[j]==w[i][j](边权值) 则该点是最佳匹配, ...
- 吴裕雄--天生自然 JAVASCRIPT开发学习:函数
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- SAP_ABAP常用事务代码
ST05: 性能跟踪(Performance Trace) SE30/SAT: ABAP对象性能分析 ST12: 单个对象性能分析(Single transaction analysis)
- VScode 修改中文字体
打开vscode ctrl+,打开设置 找到font,第一个是首选的英文字体,第二个是中文字体.
- 简单makefile示例
Makefile cmd: - g++ 相信在linux下编程的没有不知道makefile的,刚开始学习linux平台下的东西,了解了下makefile的制作,觉得有点东西可以记录下. 下面是一个极其 ...
- python3.4+Django+pymysql
pip install Pymysql 修改app里面的__init__.py import pymysqlpymysql.install_as_MySQLdb()
- 2014_csu选拔1_B
Description Here is no naked girl nor naked runners, but a naked problem: you are to find the K-th s ...