mirror of
https://github.com/continew-org/continew-starter.git
synced 2025-09-10 19:03:08 +08:00
feat(extension/crud)): 新增钩子方法,用于增强增、删、改方法
This commit is contained in:
@@ -16,6 +16,12 @@
|
||||
<description>ContiNew Starter 扩展模块 - CRUD(增删改查)</description>
|
||||
|
||||
<dependencies>
|
||||
<!-- Crane4j(一个基于注解的,用于完成一切 “根据 A 的 key 值拿到 B,再把 B 的属性映射到 A” 这类需求的字段填充框架) -->
|
||||
<dependency>
|
||||
<groupId>cn.crane4j</groupId>
|
||||
<artifactId>crane4j-spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.data</groupId>
|
||||
<artifactId>spring-data-commons</artifactId>
|
||||
@@ -44,11 +50,5 @@
|
||||
<groupId>top.charles7c.continew</groupId>
|
||||
<artifactId>continew-starter-api-doc</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Crane4j(一个基于注解的,用于完成一切 “根据 A 的 key 值拿到 B,再把 B 的属性映射到 A” 这类需求的字段填充框架) -->
|
||||
<dependency>
|
||||
<groupId>cn.crane4j</groupId>
|
||||
<artifactId>crane4j-spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
@@ -146,26 +146,31 @@ public abstract class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseDO,
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public Long add(C req) {
|
||||
if (null == req) {
|
||||
return 0L;
|
||||
}
|
||||
this.beforeAdd(req);
|
||||
T entity = BeanUtil.copyProperties(req, entityClass);
|
||||
baseMapper.insert(entity);
|
||||
this.afterAdd(req, entity);
|
||||
return entity.getId();
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void update(C req, Long id) {
|
||||
this.beforeUpdate(req, id);
|
||||
T entity = this.getById(id);
|
||||
BeanUtil.copyProperties(req, entity, CopyOptions.create().ignoreNullValue());
|
||||
baseMapper.updateById(entity);
|
||||
this.afterUpdate(req, entity);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void delete(List<Long> ids) {
|
||||
this.beforeDelete(ids);
|
||||
baseMapper.deleteBatchIds(ids);
|
||||
this.afterDelete(ids);
|
||||
}
|
||||
|
||||
@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) {
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前实体类型
|
||||
*
|
||||
|
Reference in New Issue
Block a user