Comments (1)
@Slf4j
@Aspect
@Component
public class LogAop {
@Autowired private HsLogMapper hsLogMapper;
@Autowired private SqlSession sqlSession;
public static void main(String[] args) throws Exception {
AconDif target = new AconDif();
target.setName("目标数据");
target.setAa("目标数据");
AconDif source = new AconDif();
source.setName("ACACA");
source.setAa("BA");
diffJavaBean(target, source);
}
/**
* @param <T> java bean
* @param target 目标数据
* @param source 原始数据
* @throws Exception 反射相关异常
* @return
*/
private static <T> String diffJavaBean(T target, T source) throws Exception {
Class<?> targetClass = target.getClass();
Class<?> sourceClass = source.getClass();
if (targetClass.equals(sourceClass)) {
Field[] declaredFields = targetClass.getDeclaredFields();
StringBuilder sb = new StringBuilder();
for (Field declaredField : declaredFields) {
String name = declaredField.getName();
FiledDiffValue annotation = declaredField.getAnnotation(FiledDiffValue.class);
if (annotation != null) {
PropertyDescriptor pd = new PropertyDescriptor(name, targetClass);
Method getMethod = pd.getReadMethod(); // 获得get方法
Object newValue = getMethod.invoke(target);
PropertyDescriptor pd2 = new PropertyDescriptor(name, sourceClass);
Method getMethod2 = pd2.getReadMethod(); // 获得get方法
Object oldValue = getMethod2.invoke(source);
if (!oldValue.equals(newValue)) {
StringBuilder stringBuilder =
changeCn(annotation.cnName(), oldValue.toString(), newValue.toString());
sb.append(stringBuilder);
sb.append(",");
}
}
}
// 删除最后一个逗号","
sb.deleteCharAt(sb.length() - 1);
System.out.println(sb);
return sb.toString();
} else {
log.error("非同类不可比");
return "";
}
}
private static StringBuilder changeCn(String cnFiledName, String oldValue, String newValue) {
String format = String.format("字段[%s]从[%s]变成[%s]", cnFiledName, oldValue, newValue);
return new StringBuilder(format);
}
@Pointcut("execution(public * com.shands.mod.dao.mapper.hs.*.updateByPrimaryKeySelective(..))")
private void hsAopUpdateOne() {}
@Pointcut("execution(public * com.shands.mod.dao.mapper.hs.*.updateByPrimaryKey(..))")
private void hsAopUpdateTwo() {}
@Before("hsAopUpdateTwo() && args(base)")
public void beforeTwo(JoinPoint jp, Object base) {
logSourceAndTarget(jp, base);
}
@Before("hsAopUpdateOne() && args(base)")
public void beforeOnce(JoinPoint jp, Object base) {
logSourceAndTarget(jp, base);
}
/** 日志数据记录, 记录 mapper.class , 元数据, 修改后数据,修改人,修改时间 */
private void logSourceAndTarget(JoinPoint jp, Object base) {
try {
ObjectId objectId = new ObjectId();
BeanUtils.copyProperties(base, objectId);
Signature signature = jp.getSignature();
Class<?> aClass = signature.getDeclaringType();
Method selectByPrimaryKey = aClass.getMethod("selectByPrimaryKey", Integer.class);
Object oldValue = selectByPrimaryKey.invoke(sqlSession.getMapper(aClass), objectId.id);
HsLog hsLog = new HsLog();
hsLog.setCreateTime(new Date());
hsLog.setCreateUser(ThreadLocalHelper.getUser().getId());
hsLog.setUpdateTime(new Date());
hsLog.setUpdateUser(ThreadLocalHelper.getUser().getId());
hsLog.setGroupId(ThreadLocalHelper.getGroup());
hsLog.setCompanyId(ThreadLocalHelper.getCompanyId());
// 更新前的
hsLog.setSource(JSON.toJSONString(oldValue));
// 更新后的
hsLog.setTarget(JSON.toJSONString(base));
MapperCLassInfo build = new MapperCLassInfo();
build.setMapperClassName(aClass.getName());
build.setMsg(diffJavaBean(base, oldValue));
hsLog.setMapperClass(aClass.getName());
hsLogMapper.insert(hsLog);
} catch (Exception e) {
log.info("数据日志,{}", e);
}
}
@Data
@NoArgsConstructor
private class MapperCLassInfo {
private String mapperClassName;
private String msg;
}
@Data
@NoArgsConstructor
private class ObjectId {
private Integer id;
}
}
from javabook-src.
Related Issues (19)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from javabook-src.