爲何要重構save?java
jpa提供的save方法會將原有數據置爲null,而大多數狀況下咱們只但願跟新本身傳入的參數,因此便有了重寫或者新增一個save方法。app
本着解決這個問題,網上搜了不少解決方案,可是沒有找到合適的,因而本身研究源碼,先展現幾個重要源碼dom
一、SimpleJpaRepository方法實現類,因爲代碼過多隻展現部分源碼ide
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); } } }
二、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(); } }
根據源碼及網上資料總結以下方案ui
1、重寫savethis
優點:侵入性小,缺點將原方法覆蓋。code
建立JpaRepositoryReBuild方法繼承SimpleJpaRepository。直接上代碼orm
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; } /** * 獲取對象的空屬性 */ 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]); } }
啓動類加上JpaRepositoryReBuild 方法對象
@EnableJpaRepositories(value = "com.XXX", repositoryBaseClass = JpaRepositoryReBuild.class) @SpringBootApplication @EnableDiscoveryClient // 即消費也註冊 public class SystemApplication { public static void main(String[] args) { SpringApplication.run(SystemApplication.class, args); } }
2、擴張jpa方法blog
一、新建新增方法接口BaseRepository
@NoRepositoryBean public interface BaseRepository<T, ID extends Serializable> extends JpaRepository<T, ID> { /** * 保存但不覆蓋原有數據 * @param entity * @return */ T saveNotNull(T entity); }
二、建立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]); } }
三、建立工廠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; } } }
四、啓動類引入
@EnableJpaRepositories(repositoryFactoryBeanClass = BaseRepositoryFactory.class, basePackages ="com.XXX") @SpringBootApplication @EnableDiscoveryClient // 即消費也註冊 public class SystemApplication { public static void main(String[] args) { SpringApplication.run(SystemApplication.class, args); } }