使用jpa之動(dòng)態(tài)插入與修改(重寫(xiě)save)
jpa之動(dòng)態(tài)插入與修改(重寫(xiě)save)
1.動(dòng)態(tài)插入
@Data
@Entity
@DynamicInsert
@Table(name = "cpu_dynamics_information")
@EntityListeners(AuditingEntityListener.class)
public class CpuDynamicsInformation extends CommonEntity implements Serializable {
private static final long serialVersionUID = -662804563658253624L;
// cpu動(dòng)態(tài)屬性
private Integer cpuCore;
// cpu用戶使用率
private Double cpuUseRate;
// cpu系統(tǒng)使用率
private Double cpuSysRate;
// cpu等待率
private Double cpuWaitRate;
// cpu空閑率
private Double cpuIdleRate;
// cpu總的使用率
private Double cpuCombineRate;
private Long serverId;
}
關(guān)鍵注解:
@DynamicInsert @EntityListeners(AuditingEntityListener.class)
2.重寫(xiě)save(修改)
@SuppressWarnings(value = "all")
public class JpaRepositoryReBuild<T, ID> extends SimpleJpaRepository<T, ID> {
private final JpaEntityInformation<T, ?> entityInformation;
private final EntityManager em;
@Autowired
public JpaRepositoryReBuild(
JpaEntityInformation<T, ?> entityInformation, EntityManager entityManager) {
super(entityInformation, entityManager);
this.entityInformation = entityInformation;
this.em = entityManager;
}
/** 通用save方法 :新增/選擇性更新 */
@Override
@Transactional
public <S extends T> S save(S entity) {
// 獲取ID
ID entityId = (ID) this.entityInformation.getId(entity);
T managedEntity;
T mergedEntity;
if (entityId == null) {
em.persist(entity);
mergedEntity = entity;
} else {
managedEntity = this.findById(entityId).get();
if (managedEntity == null) {
em.persist(entity);
mergedEntity = entity;
} else {
BeanUtils.copyProperties(entity, managedEntity, getNullProperties(entity));
em.merge(managedEntity);
mergedEntity = managedEntity;
}
}
return entity;
}
/** 獲取對(duì)象的空屬性 */
private static String[] getNullProperties(Object src) {
// 1.獲取Bean
BeanWrapper srcBean = new BeanWrapperImpl(src);
// 2.獲取Bean的屬性描述
PropertyDescriptor[] pds = srcBean.getPropertyDescriptors();
// 3.獲取Bean的空屬性
Set<String> properties = new HashSet<>();
for (PropertyDescriptor propertyDescriptor : pds) {
String propertyName = propertyDescriptor.getName();
Object propertyValue = srcBean.getPropertyValue(propertyName);
if (StringUtils.isEmpty(propertyValue)) {
srcBean.setPropertyValue(propertyName, null);
properties.add(propertyName);
}
}
return properties.toArray(new String[0]);
}
}
3.啟動(dòng)類
@EnableJpaAuditing
@SpringBootApplication(exclude = MongoAutoConfiguration.class)
@EnableJpaRepositories(
value = {"com.fooww.research.repository", "com.fooww.research.shiro.repository"},
repositoryBaseClass = JpaRepositoryReBuild.class)
public class MonitorServerApplication {
public static void main(String[] args) {
SpringApplication.run(MonitorServerApplication.class, args);
}
}
關(guān)鍵注釋:
EnableJpaRepositories掃描的repository包repositoryBaseClass重寫(xiě)的save類EnableJpaAuditing使@EntityListeners(AuditingEntityListener.class) 生效
擴(kuò)展JPA方法,重寫(xiě)save方法
為什么要重構(gòu)save?
jpa提供的save方法會(huì)將原有數(shù)據(jù)置為null,而大多數(shù)情況下我們只希望跟新自己傳入的參數(shù),所以便有了重寫(xiě)或者新增一個(gè)save方法。
本著解決這個(gè)問(wèn)題,網(wǎng)上搜了很多解決方案,但是沒(méi)有找到合適的,于是自己研究源碼,先展示幾個(gè)重要源碼
1、SimpleJpaRepository方法實(shí)現(xiàn)類,由于代碼過(guò)多只展示部分源碼
public class SimpleJpaRepository<T, ID> implements JpaRepository<T, ID>, JpaSpecificationExecutor<T> {
private static final String ID_MUST_NOT_BE_NULL = "The given id must not be null!";
private final JpaEntityInformation<T, ?> entityInformation;
private final EntityManager em;
private final PersistenceProvider provider;
@Nullable
private CrudMethodMetadata metadata;
public SimpleJpaRepository(JpaEntityInformation<T, ?> entityInformation, EntityManager entityManager) {
Assert.notNull(entityInformation, "JpaEntityInformation must not be null!");
Assert.notNull(entityManager, "EntityManager must not be null!");
this.entityInformation = entityInformation;
this.em = entityManager;
this.provider = PersistenceProvider.fromEntityManager(entityManager);
}
public SimpleJpaRepository(Class<T> domainClass, EntityManager em) {
this(JpaEntityInformationSupport.getEntityInformation(domainClass, em), em);
}
public void setRepositoryMethodMetadata(CrudMethodMetadata crudMethodMetadata) {
this.metadata = crudMethodMetadata;
}
@Nullable
protected CrudMethodMetadata getRepositoryMethodMetadata() {
return this.metadata;
}
protected Class<T> getDomainClass() {
return this.entityInformation.getJavaType();
}
private String getDeleteAllQueryString() {
return QueryUtils.getQueryString("delete from %s x", this.entityInformation.getEntityName());
}
@Transactional
public <S extends T> S save(S entity) {
if (this.entityInformation.isNew(entity)) {
this.em.persist(entity);
return entity;
} else {
return this.em.merge(entity);
}
}
}
2、JpaRepositoryFactoryBean
public class JpaRepositoryFactoryBean<T extends Repository<S, ID>, S, ID> extends TransactionalRepositoryFactoryBeanSupport<T, S, ID> {
@Nullable
private EntityManager entityManager;
public JpaRepositoryFactoryBean(Class<? extends T> repositoryInterface) {
super(repositoryInterface);
}
@PersistenceContext
public void setEntityManager(EntityManager entityManager) {
this.entityManager = entityManager;
}
public void setMappingContext(MappingContext<?, ?> mappingContext) {
super.setMappingContext(mappingContext);
}
protected RepositoryFactorySupport doCreateRepositoryFactory() {
Assert.state(this.entityManager != null, "EntityManager must not be null!");
return this.createRepositoryFactory(this.entityManager);
}
protected RepositoryFactorySupport createRepositoryFactory(EntityManager entityManager) {
return new JpaRepositoryFactory(entityManager);
}
public void afterPropertiesSet() {
Assert.state(this.entityManager != null, "EntityManager must not be null!");
super.afterPropertiesSet();
}
}
根據(jù)源碼及網(wǎng)上資料總結(jié)如下方案
一、重寫(xiě)save
優(yōu)勢(shì):侵入性小,缺點(diǎn)將原方法覆蓋。
創(chuàng)建JpaRepositoryReBuild方法繼承SimpleJpaRepository。
直接上代碼
public class JpaRepositoryReBuild<T, ID> extends SimpleJpaRepository<T, ID> {
private final JpaEntityInformation<T, ?> entityInformation;
private final EntityManager em;
@Autowired
public JpaRepositoryReBuild(JpaEntityInformation<T, ?> entityInformation, EntityManager entityManager) {
super(entityInformation, entityManager);
this.entityInformation = entityInformation;
this.em = entityManager;
}
/**
* 通用save方法 :新增/選擇性更新
*/
@Override
@Transactional
public <S extends T> S save(S entity) {
//獲取ID
ID entityId = (ID) this.entityInformation.getId(entity);
T managedEntity;
T mergedEntity;
if(entityId == null){
em.persist(entity);
mergedEntity = entity;
}else{
managedEntity = this.findById(entityId).get();
if (managedEntity == null) {
em.persist(entity);
mergedEntity = entity;
} else {
BeanUtils.copyProperties(entity, managedEntity, getNullProperties(entity));
em.merge(managedEntity);
mergedEntity = managedEntity;
}
}
return entity;
}
/**
* 獲取對(duì)象的空屬性
*/
private static String[] getNullProperties(Object src) {
//1.獲取Bean
BeanWrapper srcBean = new BeanWrapperImpl(src);
//2.獲取Bean的屬性描述
PropertyDescriptor[] pds = srcBean.getPropertyDescriptors();
//3.獲取Bean的空屬性
Set<String> properties = new HashSet<>();
for (PropertyDescriptor propertyDescriptor : pds) {
String propertyName = propertyDescriptor.getName();
Object propertyValue = srcBean.getPropertyValue(propertyName);
if (StringUtils.isEmpty(propertyValue)) {
srcBean.setPropertyValue(propertyName, null);
properties.add(propertyName);
}
}
return properties.toArray(new String[0]);
}
}
啟動(dòng)類加上JpaRepositoryReBuild 方法
@EnableJpaRepositories(value = "com.XXX", repositoryBaseClass = JpaRepositoryReBuild.class)
@SpringBootApplication
@EnableDiscoveryClient // 即消費(fèi)也注冊(cè)
public class SystemApplication {
public static void main(String[] args) {
SpringApplication.run(SystemApplication.class, args);
}
}
二、擴(kuò)張jpa方法
1、新建新增方法接口BaseRepository
@NoRepositoryBean
public interface BaseRepository<T, ID extends Serializable> extends JpaRepository<T, ID> {
/**
* 保存但不覆蓋原有數(shù)據(jù)
* @param entity
* @return
*/
T saveNotNull(T entity);
}
2、創(chuàng)建BaseRepositoryImpl方法
@NoRepositoryBean
public class BaseRepositoryImpl<T, ID extends Serializable> extends SimpleJpaRepository<T, ID> implements BaseRepository<T, ID> {
private final JpaEntityInformation<T, ?> entityInformation;
private final EntityManager em;
public BaseRepositoryImpl(JpaEntityInformation<T, ?> entityInformation, EntityManager entityManager) {
super(entityInformation,entityManager);
this.entityInformation = entityInformation;
this.em = entityManager;
}
public BaseRepositoryImpl(Class<T> domainClass, EntityManager em) {
this(JpaEntityInformationSupport.getEntityInformation(domainClass, em), em);
}
@Override
@Transactional
public T saveNotNull(T entity) {
//獲取ID
ID entityId = (ID) this.entityInformation.getId(entity);
T managedEntity;
T mergedEntity;
if(entityId == null){
em.persist(entity);
mergedEntity = entity;
}else{
managedEntity = this.findById(entityId).get();
if (managedEntity == null) {
em.persist(entity);
mergedEntity = entity;
} else {
BeanUtils.copyProperties(entity, managedEntity, getNullProperties(entity));
em.merge(managedEntity);
mergedEntity = managedEntity;
}
}
return mergedEntity;
}
private static String[] getNullProperties(Object src) {
//1.獲取Bean
BeanWrapper srcBean = new BeanWrapperImpl(src);
//2.獲取Bean的屬性描述
PropertyDescriptor[] pds = srcBean.getPropertyDescriptors();
//3.獲取Bean的空屬性
Set<String> properties = new HashSet<>();
for (PropertyDescriptor propertyDescriptor : pds) {
String propertyName = propertyDescriptor.getName();
Object propertyValue = srcBean.getPropertyValue(propertyName);
if (StringUtils.isEmpty(propertyValue)) {
srcBean.setPropertyValue(propertyName, null);
properties.add(propertyName);
}
}
return properties.toArray(new String[0]);
}
}
3、創(chuàng)建工廠BaseRepositoryFactory
public class BaseRepositoryFactory<R extends JpaRepository<T, ID>, T, ID extends Serializable> extends JpaRepositoryFactoryBean<R, T, ID> {
public BaseRepositoryFactory(Class<? extends R> repositoryInterface) {
super(repositoryInterface);
}
@Override
protected RepositoryFactorySupport createRepositoryFactory(EntityManager em) {
return new MyRepositoryFactory(em);
}
private static class MyRepositoryFactory extends JpaRepositoryFactory {
private final EntityManager em;
public MyRepositoryFactory(EntityManager em) {
super(em);
this.em = em;
}
@Override
protected Object getTargetRepository(RepositoryInformation information) {
return new BaseRepositoryImpl((Class) information.getDomainType(), em);
}
@Override
protected Class getRepositoryBaseClass(RepositoryMetadata metadata) {
return BaseRepositoryImpl.class;
}
}
}
4、啟動(dòng)類引入
@EnableJpaRepositories(repositoryFactoryBeanClass = BaseRepositoryFactory.class, basePackages ="com.XXX")
@SpringBootApplication
@EnableDiscoveryClient // 即消費(fèi)也注冊(cè)
public class SystemApplication {
public static void main(String[] args) {
SpringApplication.run(SystemApplication.class, args);
}
}
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java數(shù)據(jù)結(jié)構(gòu)與算法之棧(Stack)實(shí)現(xiàn)詳解
這篇文章主要為大家詳細(xì)介紹了Java數(shù)據(jù)結(jié)構(gòu)學(xué)習(xí)筆記第二篇,Java數(shù)據(jù)結(jié)構(gòu)與算法之棧Stack實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09
spring cloud學(xué)習(xí)入門(mén)之config配置教程
這篇文章主要給大家介紹了關(guān)于spring cloud學(xué)習(xí)入門(mén)之config配置的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用spring cloud具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-09-09
Springboot 整合shiro實(shí)現(xiàn)權(quán)限控制的方法
這篇文章主要介紹了Springboot 整合shiro實(shí)現(xiàn)權(quán)限控制的相關(guān)知識(shí),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11
java Hibernate多對(duì)多映射詳解及實(shí)例代碼
這篇文章主要介紹了java Hibernate多對(duì)多映射詳解及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-01-01
劍指Offer之Java算法習(xí)題精講排列與N叉樹(shù)
跟著思路走,之后從簡(jiǎn)單題入手,反復(fù)去看,做過(guò)之后可能會(huì)忘記,之后再做一次,記不住就反復(fù)做,反復(fù)尋求思路和規(guī)律,慢慢積累就會(huì)發(fā)現(xiàn)質(zhì)的變化2022-03-03
java中switch case語(yǔ)句需要加入break的原因解析
這篇文章主要介紹了java中switch case語(yǔ)句需要加入break的原因解析的相關(guān)資料,需要的朋友可以參考下2017-07-07

