tlias管理系统--事务管理-事务进阶-rollbackFor属性
This commit is contained in:
@@ -36,7 +36,7 @@ public class DeptController {
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
public Result delete(@PathVariable Integer id){
|
||||
public Result delete(@PathVariable Integer id) throws Exception {
|
||||
log.info("删除部门:{}",id);
|
||||
deptService.delete(id);
|
||||
return Result.success();
|
||||
|
||||
@@ -65,4 +65,11 @@ public interface EmpMapper {
|
||||
|
||||
@Select("select * from emp where username = #{username} and password = #{password} ")
|
||||
Emp getEmpByUserNameAndPassword(Emp emp);
|
||||
|
||||
/**
|
||||
* 根据部门id删除员工
|
||||
* @param deptId
|
||||
*/
|
||||
@Delete("delete from emp where dept_id = #{deptId}")
|
||||
void deleteByDeptId(Integer deptId);
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ public interface DeptService {
|
||||
* 根据id删除部门
|
||||
* @param id
|
||||
*/
|
||||
void delete(Integer id);
|
||||
void delete(Integer id) throws Exception;
|
||||
|
||||
/**
|
||||
* 新增部门
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
package com.inmind.service.impl;
|
||||
|
||||
import com.inmind.mapper.DeptMapper;
|
||||
import com.inmind.mapper.EmpMapper;
|
||||
import com.inmind.pojo.Dept;
|
||||
import com.inmind.service.DeptService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
@@ -15,6 +17,10 @@ public class DeptServiceImpl implements DeptService {
|
||||
|
||||
@Autowired//从spring容器中获取DeptMapper类的实现类对象,进行自动注入(DI)
|
||||
private DeptMapper deptMapper;
|
||||
|
||||
@Autowired
|
||||
private EmpMapper empMapper;
|
||||
|
||||
/**
|
||||
* 查询所有部门信息
|
||||
* @return
|
||||
@@ -30,9 +36,20 @@ public class DeptServiceImpl implements DeptService {
|
||||
* 根据id删除部门
|
||||
* @param id
|
||||
*/
|
||||
@Transactional(rollbackFor = Exception.class)//将当前的删除功能交给事务管理,达到全成功或全失败的业务功能
|
||||
@Override
|
||||
public void delete(Integer id) {
|
||||
public void delete(Integer id) throws Exception {
|
||||
deptMapper.delete(id);
|
||||
|
||||
//todo 可能存在大量的业务处理代码,而代码可能会出现业务异常
|
||||
// int a = 1/0;
|
||||
|
||||
if (true) {
|
||||
throw new Exception();//模拟抛出一个编译时异常
|
||||
}
|
||||
|
||||
//当删除部门时,也要把相关的员工删除掉
|
||||
empMapper.deleteByDeptId(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user