feat(extension/crud)): 新增钩子方法,用于增强增、删、改方法

This commit is contained in:
2024-01-15 22:59:20 +08:00
parent 156b02b3d7
commit 43dba72cee
2 changed files with 65 additions and 9 deletions

View File

@@ -16,6 +16,12 @@
<description>ContiNew Starter 扩展模块 - CRUD增删改查</description> <description>ContiNew Starter 扩展模块 - CRUD增删改查</description>
<dependencies> <dependencies>
<!-- Crane4j一个基于注解的用于完成一切 “根据 A 的 key 值拿到 B再把 B 的属性映射到 A” 这类需求的字段填充框架) -->
<dependency>
<groupId>cn.crane4j</groupId>
<artifactId>crane4j-spring-boot-starter</artifactId>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.data</groupId> <groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId> <artifactId>spring-data-commons</artifactId>
@@ -44,11 +50,5 @@
<groupId>top.charles7c.continew</groupId> <groupId>top.charles7c.continew</groupId>
<artifactId>continew-starter-api-doc</artifactId> <artifactId>continew-starter-api-doc</artifactId>
</dependency> </dependency>
<!-- Crane4j一个基于注解的用于完成一切 “根据 A 的 key 值拿到 B再把 B 的属性映射到 A” 这类需求的字段填充框架) -->
<dependency>
<groupId>cn.crane4j</groupId>
<artifactId>crane4j-spring-boot-starter</artifactId>
</dependency>
</dependencies> </dependencies>
</project> </project>

View File

@@ -146,26 +146,31 @@ public abstract class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseDO,
} }
@Override @Override
@Transactional(rollbackFor = Exception.class)
public Long add(C req) { public Long add(C req) {
if (null == req) { this.beforeAdd(req);
return 0L;
}
T entity = BeanUtil.copyProperties(req, entityClass); T entity = BeanUtil.copyProperties(req, entityClass);
baseMapper.insert(entity); baseMapper.insert(entity);
this.afterAdd(req, entity);
return entity.getId(); return entity.getId();
} }
@Override @Override
@Transactional(rollbackFor = Exception.class)
public void update(C req, Long id) { public void update(C req, Long id) {
this.beforeUpdate(req, id);
T entity = this.getById(id); T entity = this.getById(id);
BeanUtil.copyProperties(req, entity, CopyOptions.create().ignoreNullValue()); BeanUtil.copyProperties(req, entity, CopyOptions.create().ignoreNullValue());
baseMapper.updateById(entity); baseMapper.updateById(entity);
this.afterUpdate(req, entity);
} }
@Override @Override
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public void delete(List<Long> ids) { public void delete(List<Long> ids) {
this.beforeDelete(ids);
baseMapper.deleteBatchIds(ids); baseMapper.deleteBatchIds(ids);
this.afterDelete(ids);
} }
@Override @Override
@@ -212,6 +217,57 @@ public abstract class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseDO,
} }
} }
/**
* 新增前置处理
*
* @param req 创建信息
*/
protected void beforeAdd(C req) {
}
/**
* 修改前置处理
*
* @param req 修改信息
* @param id ID
*/
protected void beforeUpdate(C req, Long id) {
}
/**
* 删除前置处理
*
* @param ids ID 列表
*/
protected void beforeDelete(List<Long> ids) {
}
/**
* 新增后置处理
*
* @param req 创建信息
* @param entity 实体信息
*/
protected void afterAdd(C req, T entity) {
}
/**
* 修改后置处理
*
* @param req 修改信息
* @param entity 实体信息
*/
protected void afterUpdate(C req, T entity) {
}
/**
* 删除后置处理
*
* @param ids ID 列表
*/
private void afterDelete(List<Long> ids) {
}
/** /**
* 获取当前实体类型 * 获取当前实体类型
* *