使用jpa之动态插入与修改(重写save)
作者:c 发布时间:2021-07-04 21:02:26
标签:jpa,动态插入,重写save
jpa之动态插入与修改(重写save)
1.动态插入
@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动态属性
private Integer cpuCore;
// cpu用户使用率
private Double cpuUseRate;
// cpu系统使用率
private Double cpuSysRate;
// cpu等待率
private Double cpuWaitRate;
// cpu空闲率
private Double cpuIdleRate;
// cpu总的使用率
private Double cpuCombineRate;
private Long serverId;
}
关键注解:
@DynamicInsert
@EntityListeners(AuditingEntityListener.class)
2.重写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;
}
/** 获取对象的空属性 */
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.启动类
@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);
}
}
关键注释:
EnableJpaRepositories
扫描的repository包repositoryBaseClass
重写的save类EnableJpaAuditing
使@EntityListeners(AuditingEntityListener.class) 生效
扩展JPA方法,重写save方法
为什么要重构save?
jpa提供的save方法会将原有数据置为null,而大多数情况下我们只希望跟新自己传入的参数,所以便有了重写或者新增一个save方法。
本着解决这个问题,网上搜了很多解决方案,但是没有找到合适的,于是自己研究源码,先展示几个重要源码
1、SimpleJpaRepository方法实现类,由于代码过多只展示部分源码
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();
}
}
根据源码及网上资料总结如下方案
一、重写save
优势:侵入性小,缺点将原方法覆盖。
创建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;
}
/**
* 获取对象的空属性
*/
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);
}
}
二、扩张jpa方法
1、新建新增方法接口BaseRepository
@NoRepositoryBean
public interface BaseRepository<T, ID extends Serializable> extends JpaRepository<T, ID> {
/**
* 保存但不覆盖原有数据
* @param entity
* @return
*/
T saveNotNull(T entity);
}
2、创建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、创建工厂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、启动类引入
@EnableJpaRepositories(repositoryFactoryBeanClass = BaseRepositoryFactory.class, basePackages ="com.XXX")
@SpringBootApplication
@EnableDiscoveryClient // 即消费也注册
public class SystemApplication {
public static void main(String[] args) {
SpringApplication.run(SystemApplication.class, args);
}
}
来源:https://www.cnblogs.com/zhouwenyang/p/11136598.html
0
投稿
猜你喜欢
- 1. 前言随着数据量和调用量的增长,用户对应用的性能要求越来越高。另外,在实际的服务中,还存在着这样的场景:系统在组装数据的时候,对于数据的
- 1. InputStream -> byte[]引入 apache.commons.is 包import org.apache.com
- 本文实例讲述了Java实现把两个数组合并为一个的方法。分享给大家供大家参考,具体如下:在Java中,如何把两个String[]合并为一个?看
- 本部分需要用到微信的JS-SDK,微信JS-SDK是微信公众平台面向网页开发者提供的基于微信内的网页开发工具包。通过使用微信JS-SDK,网
- 前言很多微信公众号中需要生成推广海报的功能,粉丝获得专属海报后可以分享到朋友圈或发给朋友,为公众号代言邀请好友即可获取奖励的。海报自带渠道二
- 操作流程假设你已经有自己的域名,因为微信公众号和微信回调都需要域名先看看官方给的文档根据官方文档,主要流程如下:(1)引导用户进入授权页面同
- 1.常用属性Name:名称;BackColor:设置控件背景颜色;Enabled:是否可用;FlayStyle:控件样式;Image:设置控
- 一、前期准备我们要在IDEA上创建一个新的项目,创建好一个项目后,我们需要创建4个包,分别是英雄包,装备包,铭文包,野怪包,皮肤包然后我们就
- 参考文献:https://www.jb51.net/article/232858.htm使用springboot 2 构建项目,调试代码的时
- 跨域问题,其实百度上面有一堆的解决方案针对普通的情况其实百度上面的方案都是可行的。我这里主要介绍2种情况。当然我这里的配置都是基于网关的,而
- 最新idea2020安装部署超详细教程懂得懂的2020.32020.2.42020.2.32020.2.220.2.12019.32018.
- Session是服务器端技术,利用这个技术,服务器在运行时可以为每一个用户的浏览器创建一个其独享的session对象,注意是默认情况下,一个
- 本文实例讲述了Android 开发使用PopupWindow实现加载等待界面功能。分享给大家供大家参考,具体如下:实现加载等待界面我用了两种
- 1、声明一个测试对象import java.time.LocalDate;import java.util.List;import lomb
- 1 需求Mybatis-plus使用@TableLogic注解进行逻辑删除数据后,在某些场景下,又需要查询该数据时,又不想写SQ
- @PropertySource注解是Spring用于加载配置文件,默认支持.properties与.xml两种配置文件。@PropertyS
- 本文简单分析了C/C++中常用函数的易错点,包括memset、sizeof、getchar等函数。分享给大家供大家参考之用。具体分析如下:1
- 前言SpringCloud 是微服务中的翘楚,最佳的落地方案。Spring Cloud Config 是一个解决分布式系统的配置管理方案,它
- 前段时间学习JDBC,要连接mysql获取数据。按照老师的样例数据,要存一些名字之类的信息,用的都是英文名,我当时就不太想用英文,就把我室友
- IDE工具之IDEA2022.2的简介、下载与安装、初步配置IDEA简介概述IDEA全称是IntelliJ,是JetBrains公司推出一个