修改日志(获取字段修改前后内容)

// User 对象实体类
@Data
@EqualsAndHashCode(callSuper = false)
@ApiModel(value="用户对象", description="用户表")
public class User {

    @ApiModelProperty(value = "用户id")
    private int id;
    @ApiModelProperty(value = "姓名")
    private String name;
    @ApiModelProperty(value = "年龄")
    private int age;
    @ApiModelProperty(value = "地址")
    private String address;
}

//  User 修改日志类

@Data
@EqualsAndHashCode(callSuper = false)
@ApiModel(value = "userUpdateLog对象", description = "用户修改日志表")
public class UserUpdateLog {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "主键ID")
    private Long userLogId;


    @ApiModelProperty(value = "修改类型")
    private String modifyField;

    @ApiModelProperty(value = "修改前内容")
    private String beforeModification;

    @ApiModelProperty(value = "修改后内容")
    private String afterModification;

}
// 正式代码测试类
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
@RunWith(SpringRunner.class)
public class demo1 {

    /**
     * JDK内省类库:
     *  PropertyDescriptor类:(属性描述器)
     *   PropertyDescriptor类表示JavaBean类通过存储器导出一个属性。主要方法:
     *   1. getPropertyType(),获得属性的Class对象;
     *   2. getReadMethod(),获得用于读取属性值的方法;
     *    3. getWriteMethod(),获得用于写入属性值的方法;
     *   4. hashCode(),获取对象的哈希值;
     *   5. setReadMethod(Method readMethod),设置用于读取属性值的方法;
     *   6. setWriteMethod(Method writeMethod),设置用于写入属性值的方法
     */
    @Test
    public void test1(){
        User beforeUser=new User();
        beforeUser.setId(1);
        beforeUser.setName("小明");
        beforeUser.setAge(20);
        beforeUser.setAddress("北京");

        User afterUser=new User();
        afterUser.setId(2);
        afterUser.setName("小黑");
        afterUser.setAge(30);
        afterUser.setAddress("上海");
        List<UserUpdateLog> userUpdateLogs = contrastObj(afterUser, beforeUser);
        System.out.println(userUpdateLogs);
    }

    public static List<UserUpdateLog> contrastObj(Object afterObj, Object beforeObj) {
        User afterUser = (User) afterObj;
        User beforeUser = (User) beforeObj;
        List<UserUpdateLog> updateLogList = Lists.newArrayList();
        try {
            Class clazz = afterUser.getClass();
            Field[] fields = afterUser.getClass().getDeclaredFields();
            for (Field field : fields) {
                UserUpdateLog userUpdateLog = new UserUpdateLog();
                if("serialVersionUID".equals(field.getName())){
                    continue;
                }
                //获取 clazz 类型中的 propertyName 的属性描述器
                PropertyDescriptor pd = new PropertyDescriptor(field.getName(), clazz);
                // 获得读取属性值的方法  getReadMethod()
                Method getMethod = pd.getReadMethod();
                Object o1 = getMethod.invoke(afterUser);
                Object o2 = getMethod.invoke(beforeUser);
                String s1 = o1 == null ? "" : o1.toString();//避免空指针异常
                String s2 = o2 == null ? "" : o2.toString();//避免空指针异常
                //思考下面注释的这一行:会bug的,虽然被try catch了,程序没报错,但是结果不是我们想要的
                // todo 我的理解 o1  o2 是object 类型
                //if (!o1.toString().equals(o2.toString())) {
                if (!s1.equals(s2)) {
                    userUpdateLog.setModifyField(field.getAnnotation(ApiModelProperty.class).value());
                    userUpdateLog.setBeforeModification(s2);
                    userUpdateLog.setAfterModification(s1);
                    updateLogList.add(userUpdateLog);
                }
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return updateLogList;
    }

// 控制台输出结果

[UserUpdateLog(userLogId=null, modifyField=用户id, beforeModification=1, afterModification=2), UserUpdateLog(userLogId=null, modifyField=姓名, beforeModification=小明, afterModification=小黑), UserUpdateLog(userLogId=null, modifyField=年龄, beforeModification=20, afterModification=30), UserUpdateLog(userLogId=null, modifyField=地址, beforeModification=北京, afterModification=上海)]

 

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在laravel框架中,可以通过在.env文件中配置数据库相关信息来连接数据库。如果你在.env文件中增加字段失败,可能有几个原因。首先,确保你在.env文件中正确地添加了新字段,并且使用了正确的语法。例如,你可以使用以下语法来添加一个新字段: ``` NEW_FIELD=your_value ``` 确保你在等号前后没有多余的空格,并且值的格式正确。其次,检查你是否正确地保存了.env文件,并且重启了应用程序以使更改生效。如果你仍然遇到问题,可以尝试清除缓存并重新生成配置文件。你可以使用以下命令来清除缓存: ``` php artisan cache:clear ``` 然后使用以下命令重新生成配置文件: ``` php artisan config:cache ``` 这样做可能会解决你的问题。如果问题仍然存在,你可以检查laravel日志文件以获取更多详细的错误信息,以便进一步排查问题。 #### 引用[.reference_title] - *1* *2* [laravel框架数据库操作](https://blog.csdn.net/qq_43683124/article/details/105103290)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [go env](https://blog.csdn.net/ma2595162349/article/details/113004169)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值