springboot jpa實現(xiàn)優(yōu)雅處理isDelete的默認(rèn)值
如果多個實體類都有 isDelete 字段,并且你希望在插入時為它們統(tǒng)一設(shè)置默認(rèn)值,可以采取以下幾種方法來減少代碼重復(fù):
1. 使用基類(抽象類)
創(chuàng)建一個基類,其中包含 isDelete 字段和 @PrePersist 方法。然后讓所有需要這個字段的實體類繼承這個基類。
示例代碼:
import javax.persistence.MappedSuperclass;
import javax.persistence.PrePersist;
@MappedSuperclass
public abstract class BaseEntity {
protected Integer isDelete;
@PrePersist
public void prePersist() {
if (isDelete == null) {
isDelete = 0; // 設(shè)置默認(rèn)值為0
}
}
// Getter 和 Setter
public Integer getIsDelete() {
return isDelete;
}
public void setIsDelete(Integer isDelete) {
this.isDelete = isDelete;
}
}
然后在其他實體類中繼承 BaseEntity:
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class MyEntity extends BaseEntity {
@Id
private Long id;
// 其他字段、getter 和 setter
}
2. 使用 AOP(面向切面編程)
通過 Spring AOP 創(chuàng)建一個切面,在插入操作時檢查并設(shè)置 isDelete 的默認(rèn)值。這種方式不需要修改每個實體類,適合大規(guī)模應(yīng)用。
示例代碼:
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.lang.reflect.Field;
@Aspect
@Component
public class DefaultValueAspect {
@PersistenceContext
private EntityManager entityManager;
@Before("execution(* com.example.repository.*.save(..))") // 根據(jù)你的倉庫路徑調(diào)整
public void setDefaultValues(Object entity) throws IllegalAccessException {
Field[] fields = entity.getClass().getDeclaredFields();
for (Field field : fields) {
if ("isDelete".equals(field.getName())) { // 檢查字段名
field.setAccessible(true);
if (field.get(entity) == null) {
field.set(entity, 0); // 設(shè)置默認(rèn)值為0
}
}
}
}
}
3. 使用 JPA 審計功能
使用 Spring Data JPA 的審計功能,通過實現(xiàn) AuditorAware 接口來統(tǒng)一處理審計字段,包括 createdBy,createdTime,updatedBy,updatedTime等,而isDelete這種狀態(tài)字段在審計注釋中并沒有實現(xiàn)。
4. 使用事件監(jiān)聽@EntityListeners
JPA 提供了事件監(jiān)聽器的功能,你可以定義一個事件監(jiān)聽器來處理所有需要設(shè)置默認(rèn)值的實體類。
示例代碼:
import javax.persistence.PostLoad;
import javax.persistence.PrePersist;
import javax.persistence.EntityListeners;
public interface DeletedField {
Integer getDeletedFlag();
void setDeletedFlag(Integer deletedFlag);
}
public class DeleteDefaultValueListener {
@PrePersist
public void setDefaultValues(DeletedFlagField deletedFlagField) {
if (deletedFlagField.getDeletedFlag() == null) {
deletedFlagField.setDeletedFlag(0); // 設(shè)置默認(rèn)值為0
}
}
}
@EntityListeners(DefaultValueListener.class)
@Entity
public class TableUserAccount extends EntityBase implements DeletedFlagField {
/**
* 刪除標(biāo)識(邏輯刪除),1刪除 0未刪除
*/
@Column(name = "deleted_flag")
private Integer deletedFlag;
}
5. 擴展JPA,對審計字段建立者和更新者的擴展
- CreatedByField 建立者字段接口
- UpdatedByField 更新者字段接口
- CreatedByDefaultValueListener 建立者字段監(jiān)聽器
- UpdatedByDefaultValueListener 更新者字段監(jiān)聽器
- AuditorAwareImpl 審計接口,返回當(dāng)前用戶
CreatedByField
public interface CreatedByField {
String getCreatedBy();
void setCreatedBy(String createdBy);
}
擴展EntityBase實體,不使用默認(rèn)的CreatedBy和LastModifiedBy
@Getter
@Setter
@MappedSuperclass
@EntityListeners({ AuditingEntityListener.class, UpdatedByDefaultValueListener.class,
CreatedByDefaultValueListener.class })
public abstract class EntityBase implements Serializable, CreatedByField, UpdatedByField {
/**
* 創(chuàng)建人
*/
@Column(name = "created_by")
private String createdBy;
/**
* 修改人
*/
@Column(name = "updated_by")
private String updatedBy;
}
CreatedByDefaultValueListener
public class CreatedByDefaultValueListener implements ApplicationContextAware {
private ApplicationContext applicationContext;
@PrePersist
public void setDefaultValues(CreatedByField createdByField) {
if (createdByField.getCreatedBy() == null) {
if (this.applicationContext.getBean(AuditorAwareImpl.class) != null) {
createdByField.setCreatedBy(
this.applicationContext.getBean(AuditorAwareImpl.class).getCurrentAuditor().orElse(""));
}
}
}
/**
* @param applicationContext
* @throws BeansException
*/
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
}到此這篇關(guān)于springboot jpa實現(xiàn)優(yōu)雅處理isDelete的默認(rèn)值的文章就介紹到這了,更多相關(guān)springboot jpa處理isDelete默認(rèn)值內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JAVA 數(shù)據(jù)結(jié)構(gòu)之Queue處理實例代碼
這篇文章主要介紹了JAVA 數(shù)據(jù)結(jié)構(gòu)之Queue處理實例代碼的相關(guān)資料,需要的朋友可以參考下2017-02-02
java監(jiān)聽器實現(xiàn)在線人數(shù)統(tǒng)計
這篇文章主要為大家詳細(xì)介紹了java監(jiān)聽器實現(xiàn)在線人數(shù)統(tǒng)計,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-11-11

