AOP处理事务
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
package com.core.framework.aspectj;
|
||||
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
import org.aspectj.lang.annotation.Before;
|
||||
import org.aspectj.lang.annotation.AfterReturning;
|
||||
import org.aspectj.lang.annotation.AfterThrowing;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
import org.springframework.transaction.TransactionStatus;
|
||||
import org.springframework.transaction.support.DefaultTransactionDefinition;
|
||||
|
||||
@Aspect
|
||||
@Component
|
||||
public class TransactionAspect {
|
||||
|
||||
private final PlatformTransactionManager transactionManager;
|
||||
private TransactionStatus transactionStatus;
|
||||
|
||||
public TransactionAspect(PlatformTransactionManager transactionManager) {
|
||||
this.transactionManager = transactionManager;
|
||||
}
|
||||
|
||||
@Before("@annotation(org.springframework.web.bind.annotation.PostMapping) || " +
|
||||
"@annotation(org.springframework.web.bind.annotation.GetMapping) || " +
|
||||
"@annotation(org.springframework.web.bind.annotation.PutMapping) || " +
|
||||
"@annotation(org.springframework.web.bind.annotation.DeleteMapping)")
|
||||
public void beginTransaction() {
|
||||
transactionStatus = transactionManager.getTransaction(new DefaultTransactionDefinition());
|
||||
}
|
||||
|
||||
@AfterReturning("@annotation(org.springframework.web.bind.annotation.PostMapping) || " +
|
||||
"@annotation(org.springframework.web.bind.annotation.GetMapping) || " +
|
||||
"@annotation(org.springframework.web.bind.annotation.PutMapping) || " +
|
||||
"@annotation(org.springframework.web.bind.annotation.DeleteMapping)")
|
||||
public void commitTransaction() {
|
||||
if (transactionStatus != null && !transactionStatus.isCompleted()) {
|
||||
transactionManager.commit(transactionStatus);
|
||||
}
|
||||
}
|
||||
|
||||
@AfterThrowing(pointcut = "@annotation(org.springframework.web.bind.annotation.PostMapping) || " +
|
||||
"@annotation(org.springframework.web.bind.annotation.GetMapping) || " +
|
||||
"@annotation(org.springframework.web.bind.annotation.PutMapping) || " +
|
||||
"@annotation(org.springframework.web.bind.annotation.DeleteMapping)",
|
||||
throwing = "ex")
|
||||
public void rollbackTransaction(Exception ex) {
|
||||
if (transactionStatus != null && !transactionStatus.isCompleted()) {
|
||||
transactionManager.rollback(transactionStatus);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -73,7 +73,6 @@ public class HealthcareServiceController {
|
||||
* 服务管理 新增
|
||||
*/
|
||||
@PostMapping(value = "/healthcare-service")
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public R<?> add(@Validated @RequestBody HealthcareServiceAddOrUpdateParam healthcareServiceAddOrUpdateParam) {
|
||||
// 服务管理-表单数据
|
||||
HealthcareService healthcareServiceFormData = healthcareServiceAddOrUpdateParam.getHealthcareServiceFormData();
|
||||
@@ -137,7 +136,6 @@ public class HealthcareServiceController {
|
||||
* @return 删除结果
|
||||
*/
|
||||
@DeleteMapping(value = "/healthcare-service")
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public R<?> delete(@RequestParam Long id) {
|
||||
boolean res = iHealthcareServiceService.removeById(id);
|
||||
// 同时删除非同定价
|
||||
|
||||
@@ -86,7 +86,6 @@ public class ChargeItemDefinitionServiceImpl extends ServiceImpl<ChargeItemDefin
|
||||
* @return 新增结果
|
||||
*/
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public boolean addChargeItemDefinitionByHealthcareService(HealthcareService healthcareService, ChargeItemDefinition chargeItemDefinition) {
|
||||
// 服务管理主键id
|
||||
if (healthcareService.getId() != null) {
|
||||
|
||||
@@ -24,7 +24,6 @@ public class HealthcareServiceServiceImpl extends ServiceImpl<HealthcareServiceM
|
||||
* @return 入库后信息
|
||||
*/
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public HealthcareService addHealthcareService(HealthcareService healthcareService) {
|
||||
baseMapper.insert(healthcareService);
|
||||
return healthcareService;
|
||||
|
||||
Reference in New Issue
Block a user