Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 一直从数据库获取数据后如何解码值_Java_Spring_Interceptor - Fatal编程技术网

Java 一直从数据库获取数据后如何解码值

Java 一直从数据库获取数据后如何解码值,java,spring,interceptor,Java,Spring,Interceptor,我想在每次获取数据时解码一些值,并在将新数据保存到数据库时进行编码 我不想在模型类中使用编码和解码逻辑 有人能建议我使用拦截器或其他方法来解决这个问题吗?我以前发布过这个,但它适用于这里: @around(value="execution(* db.entities.package)") public void cypher(ProceedingJoinPoint call){ try { // encode or decode logic here call.proc

我想在每次获取数据时解码一些值,并在将新数据保存到数据库时进行编码

我不想在模型类中使用编码和解码逻辑


有人能建议我使用拦截器或其他方法来解决这个问题吗?

我以前发布过这个,但它适用于这里:

@around(value="execution(* db.entities.package)")
public void cypher(ProceedingJoinPoint call){
   try {
     // encode or decode logic here
     call.proceed();
   } catch (Exception e){
      // handle exception
   }
}

您可以使用
EntityListener
进行同样的操作

其中,您可以使用
@PostLoad
注释对数据进行解码,
@PrePersist
@PreUpdate
对数据进行持久化和更新

例如:

实体侦听器 模型
每当使用crud操作时,总是根据其注释调用EntityListener方法。

AOP将是一个不错的选择。如果使用Spring,它是内置的。如果您将此作为答案发布,并且它确实适用,那么此问题是您回答的第一个问题的副本,正确的行为是投票关闭此问题,将其作为另一个问题的副本。我以前发布过代码片段,这适用于另一个问题。如果您选择EntityListener路线,请查看以下内容:
@Component
class EntityListener {
    @PrePersist
    public void onPrePersist(Object o) {
        // encode logic
    }

    @PreUpdate
    public void onPreUpdate(Object o) {
        // encode logic
    }

    @PostLoad
    public void onPostLoad(Object o) {
        // decode logic
    }
}
@Table
@Entity
@EntityListeners({EntityListener.class})
class Model {
  @Id
  @Column(updatable = false)
  @GeneratedValue
  private int id;

  private String password;

  private String username;

}