6

JPA实体类crud监听器以及审计功能

 3 years ago
source link: http://www.lzhpo.com/article/159
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

JPA实体类crud监听器以及审计功能

什么是审计功能?

需要开启JPA的Audit审计功能@EnableJpaAuditing

数据库审计一般是指跟踪和记录与持久化实体相关的事件,或者仅仅是实体版本跟踪。

1.@CreatedBy、@CreatedDate…

开启了审计功能,就可以使用JPA提供的一些功能。

@Column(length = 32)@CreatedByprivate String createBy;@Column(length = 32)@LastModifiedByprivate String updateBy;@Column(nullable = false, updatable = false)@JsonFormat(locale = "zh", timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")@CreatedDateprivate LocalDateTime createTime;@JsonFormat(locale = "zh", timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")@LastModifiedDateprivate LocalDateTime updateTime;

创建时间和更新时间会自动设置并插入数据库,但是createByupdateBy并没有赋值,因为需要实现AuditorAware接口来返回你需要插入的值。

@Componentpublic class SpringSecurityAuditorAware implements AuditorAware<String> {    @Override    public String getCurrentAuditor() {        // 返回会话中或者指定的用户名        final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();        if (authentication == null) {            throw new CustomException(ResultStatus.UNAUTHORIZED, "当前登录状态过期");        }        UserDetails userDetails = (UserDetails) authentication.getPrincipal();        return userDetails.getUsername();    }}

2.Crud前后

开启了审计功能,然后还有…可以做一些你想做的crud前后的事情…

e.g.我需要自己手段设置一些值的话…

/** * 实体类crud监听器 * * @author Zhaopo Liu */@Slf4j@Componentpublic class EntityListener {    /**     * 新增操作之前     * e.g.我需要自己手段设置一些值的话...     *     * @param target     */    @PrePersist    public void PrePersist(Object target) {        log.debug("新增操作之前...");        Assert.notNull(target, "Entity must not be null!");        if (target instanceof BaseEntity) {            BaseEntity baseEntity = (BaseEntity) target;            User user = SpringContextHolder.getBean(UserService.class).findByAccount(SecurityUtil.getCurrentUsername());            if (user != null) {                baseEntity.setCreateByName(user.getName());                baseEntity.setUpdateByName(user.getName());            }        }    }    /**     * 新增操作之后     *     * @param target     */    @PostPersist    public void PostPersist(Object target) {        log.debug("新增操作之后...");    }    /**     * 更新操作之前     *     * @param target     */    @PreUpdate    public void PreUpdate(Object target) {        log.debug("更新操作之前...");    }    /**     * 更新操作之后     *     * @param target     */    @PostUpdate    public void PostUpdate(Object target) {        log.debug("更新操作之后...");    }    /**     * 删除操作之前     *     * @param target     */    @PreRemove    public void PreRemove(Object target) {        log.debug("删除操作之前...");    }    /**     * 删除操作之后     *     * @param target     */    @PostRemove    public void PostRemove(Object target) {        log.debug("删除操作之后...");    }}

实体类使用:

@EntityListeners({AuditingEntityListener.class, EntityListener.class})
正文到此结束
所属分类:Java开发

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK