實作日志功能
- 需求
- 思路
- 參考連結
- 步驟1
- 步驟2
- 儲存到 日志表中去
- 測試代碼
- 測試實體 student.java
- 自定義注解
- 工具類 LogUtil.java
需求
客戶希望實作下面的詳情記錄檔功能

思路
這樣 隻要修改資料庫的地方,比如增,删,改的地方就要儲存日志記錄,增加和删除好做,麻煩的就是修改,怎麼弄呢,整理了下:
參考連結
參考連結:
步驟1
前提,根據id 擷取 舊的對象, 傳參擷取新對象
步驟2
擷取2個對象 同一個屬性,不同的值,
将 屬性名,新值 ,舊值 放入一個 list集合中
by the way 中文名: 根據注解擷取
list = [
{
'name':,
'old':'',
'new':'',
comment:''
}
]
list中的每個對象,最好建一張表
儲存到 日志表中去
測試代碼
測試實體 student.java
package com.platform.utils;
import com.platform.annotation.Comment;
public class Student {
@Comment(value = "編号")
private Integer id;
@Comment(value = "姓名")
private String username;
@Comment(value = "年齡")
private Integer age;
public Student() {
}
public Student(Integer id, String username) {
this.id = id;
this.username = username;
}
public Student(Integer id, String username, Integer age) {
this.id = id;
this.username = username;
this.age = age;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
自定義注解
/**
* 注解實體
*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Comment {
String value() default "";
}
工具類 LogUtil.java
package com.platform.utils;
import com.platform.annotation.Comment;
import java.lang.reflect.Field;
import java.util.*;
public class LogUtil {
/**
* 擷取兩個對象同名屬性内容不相同的清單
*/
public static List<Map<String, Object>> compareTwoClass(Object class1, Object class2) throws ClassNotFoundException, IllegalAccessException {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
//擷取對象的class
Class<?> clazz1 = class1.getClass();
Class<?> clazz2 = class2.getClass();
//擷取對象的屬性清單
Field[] field1 = clazz1.getDeclaredFields();
Field[] field2 = clazz2.getDeclaredFields();
//周遊屬性清單field1
for (int i = 0; i < field1.length; i++) {
//周遊屬性清單field2
for (int j = 0; j < field2.length; j++) {
//如果field1[i]屬性名與field2[j]屬性名内容相同
if (field1[i].getName().equals(field2[j].getName())) {
field1[i].setAccessible(true);
field2[j].setAccessible(true);
//如果field1[i]屬性值與field2[j]屬性值内容不相同
if (!compareTwo(field1[i].get(class1), field2[j].get(class2))) {
Map<String, Object> map2 = new HashMap<String, Object>();
Comment comment = field1[i].getDeclaredAnnotation(Comment.class);
if(comment!=null){
map2.put("name", comment.value()); //注解名字
map2.put("old", field1[i].get(class1));
map2.put("new", field2[j].get(class2));
list.add(map2);
}
}
break;
}
}
}
return list;
}
//對比兩個資料是否内容相同
public static boolean compareTwo(Object object1, Object object2) {
if (object1 == null && object2 == null) {
return true;
}
//以下注掉代碼,看具體需求。因有時會出現源資料是沒有進行指派,是以是null;而通過EditText擷取值的時候,雖然沒有值,但是會變成"",但是本質是沒有指派的。
//if (object1 == "" && object2 == null) {
// return true;
//}
//if (object1 == null && object2 == "") {
// return true;
// }
if (object1 == null && object2 != null) {
return false;
}
if (object1.equals(object2)) {
return true;
}
return false;
}
public static void main(String[] args) throws IllegalAccessException, ClassNotFoundException {
Student s1 = new Student(1, "張三", 29);
Student s2 = new Student(2, "李四",29);
List<Map<String, Object>> maps = compareTwoClass(s1, s2);
for (Map<String, Object> m : maps){
System.out.println(m.get("name")+"===="+m.get("old")+"===="+m.get("new"));
}
}
}
測試結果: