添加用户操作历史记录时,纪录用户修改内容用反射技术实现
2013-09-29 00:00
477 查看
因为要比较每一个对象的属性的值是否改变,如果每个对象都写一遍比较的方法,不仅繁琐,复用性也不好。所以我们用反射的机制,
获得每个对象的字段,得到字段的get方法,获得每个字段的新旧值,作比较,记录修改的历史记录。
public class GetModifyHistoryUtil {
public static String getEditHistory(Object oldObj,Object newObj){
String content = "";
//获得属性
Field[] fields = oldObj.getClass().getDeclaredFields();
for (Field field : fields) {
try {
PropertyDescriptor descriptor = new PropertyDescriptor(field.getName(), oldObj.getClass());
Method getMethod = descriptor.getReadMethod();//获得get方法
Object oldVal = getMethod.invoke(oldObj);
Object newVal = getMethod.invoke(newObj);
if(oldVal != null || newVal != null){
if(oldVal == null && newVal != null){
if(StringUtils.isNotBlank(newVal.toString()))
content += "增加"+field.getName()+"字段的值为"+newVal.toString()+"; ";
}else if(oldVal != null && newVal == null)
content += "删除了"+field.getName()+"字段的内容["+ oldVal.toString() +"]; ";
else if(oldVal != null && newVal != null){
if(!oldVal.toString().equals(newVal.toString()))
content += "修改"+field.getName()+"字段内容["+oldVal.toString()+"]为:"+newVal.toString() + "; ";
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
return content;
}
}
得到修改的内容后,纪录到历史表中。
获得每个对象的字段,得到字段的get方法,获得每个字段的新旧值,作比较,记录修改的历史记录。
public class GetModifyHistoryUtil {
public static String getEditHistory(Object oldObj,Object newObj){
String content = "";
//获得属性
Field[] fields = oldObj.getClass().getDeclaredFields();
for (Field field : fields) {
try {
PropertyDescriptor descriptor = new PropertyDescriptor(field.getName(), oldObj.getClass());
Method getMethod = descriptor.getReadMethod();//获得get方法
Object oldVal = getMethod.invoke(oldObj);
Object newVal = getMethod.invoke(newObj);
if(oldVal != null || newVal != null){
if(oldVal == null && newVal != null){
if(StringUtils.isNotBlank(newVal.toString()))
content += "增加"+field.getName()+"字段的值为"+newVal.toString()+"; ";
}else if(oldVal != null && newVal == null)
content += "删除了"+field.getName()+"字段的内容["+ oldVal.toString() +"]; ";
else if(oldVal != null && newVal != null){
if(!oldVal.toString().equals(newVal.toString()))
content += "修改"+field.getName()+"字段内容["+oldVal.toString()+"]为:"+newVal.toString() + "; ";
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
return content;
}
}
得到修改的内容后,纪录到历史表中。
相关文章推荐
- C#写的对系统用户和组操作的类(可以实现用户和组的添加、删除、修改)
- Zookeeper客户端基本操作java实现——创建连接、创建节点、添加修改节点内容、获取子节点、获取节点数据、删除节点
- mongodb查看操作记录方法以及用户添加删除权限修改密码
- 一种历史详细记录表,完整实现:CommonOperateLog 详细记录某用户、某时间、对某表、某主键、某字段的修改(新旧值
- 编写android程序实现表记录的添加、删除、修改
- .NET MVC通过反射获取数据修改历史记录,并插入数据表中
- 实现用户的历史记录功能
- 使用Aop面向切面技术实现记录详细操作日志功能
- php实现openfire用户同步添加,删除,修改
- linux系统记录用户操作实现方法
- 在uistoryboard中实现scrollView的添加内容的操作注意事项
- JSP+Servlet+Tomcat+Mysql实现用户注册、登陆、修改、添加、查看详情、分页实例之---数据库
- JSP+Servlet+Tomcat+Mysql实现注册、登陆、修改、添加、查看、分页实例之----返回操作
- 在MySQL中使用init-connect与binlog来实现用户操作追踪记录
- 实现功能:弹出一个DIV层提示等待信息,这个时候禁用用户操作页面中的其他内容
- js实现当用户复制网页内容时添加版权等额外信息源码
- MySql 5.7中新建数据库,添加用户,用户授权,删除用户,修改密码等操作
- MySql使用全记录2 -----列操作(添加列、设定默认值、修改指定多条记录的列值)
- java操作sql server数据添加,修改,删除记录
- ASP.NET MVC+EF框架+EasyUI实现权限管理系列(16)-类库架构扩展以及DLL文件生成修改和用户的简单添加