一、奖惩管理crud
1.1 pojo1.2 controller 二、部门管理crud(设计到存储管理,mybatis的开发比较广泛的知识点应用)
2.1 存储过程
1. 存储一个部门信息2. 删除一个部门 2.2 查询所有部门
1. pojo层2. controller层3. service层4. mapper的xml 2.3 增加一个部门(⭐运用存储过程就是联动性的crud)
1. controller层2. service层3. mapper的xml写法 ⭐ 删除一个部门(⭐运用存储过程就是联动性的crud,对应上面数据库函数)
1. controller层2. service层3. mapper的xml
一、奖惩管理crud 1.1 pojo@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("t_employee_ec")
@ApiModel(value="EmployeeEc对象", description="")
public class EmployeeEc implements Serializable {
private static final long serialVersionUID = 1L;
@ApiModelProperty(value = "id")
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
@ApiModelProperty(value = "员工编号")
private Integer eid;
@ApiModelProperty(value = "奖罚日期")
private LocalDateTime ecDate;
@ApiModelProperty(value = "奖罚原因")
private String ecReason;
@ApiModelProperty(value = "奖罚分")
private Integer ecPoint;
@ApiModelProperty(value = "奖罚类别,0:奖,1:罚")
private Integer ecType;
@ApiModelProperty(value = "备注")
private String remark;
}
1.2 controller
@Api(tags = "奖惩类接口管理")
@RestController
@RequestMapping("personnel/ec/employee-ec")
public class EmployeeEcController {
@Autowired
private IEmployeeEcService employeeEcService;
@ApiOperation(value = "获取所有奖惩信息")
@GetMapping("/")
public List getAllEmployeeEc() {
return employeeEcService.list();
}
@ApiOperation(value = "添加一条奖惩记录")
@PostMapping("/")
public RespBean addEmployeeEc(@RequestBody EmployeeEc employeeEc) {
employeeEc.setEcDate(LocalDateTime.now());
if (employeeEcService.save(employeeEc)) {
return RespBean.success("奖惩记录添加成功");
}
return RespBean.warning("奖惩记录添加失败");
}
@ApiOperation(value = "修改一条奖惩记录")
@PutMapping("/")
public RespBean updateEmployeeEc(@RequestBody EmployeeEc employeeEc) {
if (employeeEcService.updateById(employeeEc)) {
return RespBean.success("奖惩记录修改成功");
}
return RespBean.warning("奖惩记录修改失败");
}
@ApiOperation(value = "删除一条奖惩记录")
@DeleteMapping("/{id}")
public RespBean deleteEmployeeEc(@PathVariable Integer id) {
if (employeeEcService.removeById(id)) {
return RespBean.success("成功删除一个奖惩记录");
}
return RespBean.warning("删除奖惩记录失败");
}
@ApiOperation(value = "删除一组奖惩记录")
@DeleteMapping("/")
private RespBean deleteEmployeeEcs(Integer[] ids){
if (employeeEcService.removeByIds(Arrays.asList(ids))) {
return RespBean.success("成功删除一组奖惩记录");
}
return RespBean.warning("删除奖惩记录失败");
}
}
二、部门管理crud(设计到存储管理,mybatis的开发比较广泛的知识点应用)
2.1 存储过程
1. 存储一个部门信息假如一个逻辑需要对数据库有串联关系的curd,可以定义数据库级函数来实现
⭐知识点介绍:
第五行——插入一个新的部门
第六行——把影响行数返回
第七行——获取到新加入的记录的id
第八行——把新加入的id返回
第九行——查询新纪录的父id记录,获取父记录的路径
第十行——更新新记录的路径为获取到父记录的id加上.加上新纪录id
第十一行——更新父记录是否有下级为true
CREATE DELETE=`root`@`xxxxxxx` PROCEDURE `addDep`(in depName VARCHAR(32), in parentId int, in enabled boolean, out result int, out result2 int) begin declare did int; declare pDepPath varchar(64); insert into t_department set name=depName,parentId=parentId,enabled=enabled; select row_count() into result; select last_insert_id() into did; set result2=did; select depPath into pDepPath from t_department where id=parentId; update t_department set depPath=concat(pDepPath,'.',did) where id=did; update t_department set isParent=true where id=parentId; end2. 删除一个部门
⭐知识点:
第六行——查询是否有被删除的id这个部门的记录并且这个部门没下级
第七行——如果没这个记录返回-2
第九行——查看员工表,这个部门下是否有员工
第十行——如果有员工就返回-1
第十二行——如果有这个部门记录的话,查出这个部门的父部门id
第十三行——删除这个部门,在这个部门没有下级部门的情况下
第十四行——把影响行数返回
第十五行——查一下删除部门的父部门还是其他哪几个部门的父部门
第十六行——如果删除该部门后,父部门id不再是其他的部门的父部门,将父部门的isParent设置为false
begin declare ecount int; declare pid int; declare pcount int; declare a int; select count(*) into a from t_department where id=did and isParent=false; if a=0 then set result=-2; else select count(*) into ecount from t_employee where departmentId=did; if ecount>0 then set result=-1; else select parentId into pid from t_department where id=did; delete from t_department where id=did and isParent=false; select row_count() into result; select count(*) into pcount from t_department where parentId=pid; if pcount=0 then update t_department set isParent=false where id=pid; end if; end if; end if; end2.2 查询所有部门
⭐知识解读:这边查询的时候会先传一个-1,这个-1是父部门id,是最根部门董事会的parentId的数,然后通过子查询(每次都拿查到记录的id作为父部门parentId去查)通过这种递归查询的方式,返回json层级的部门数据结构,具体体现在mybatis的xml上。
示例:
1. pojo层@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("t_department")
@ApiModel(value="Department对象", description="")
public class Department implements Serializable {
private static final long serialVersionUID = 1L;
@ApiModelProperty(value = "id")
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
@ApiModelProperty(value = "部门名称")
private String name;
@ApiModelProperty(value = "父id")
private Integer parentId;
@ApiModelProperty(value = "路径")
private String depPath;
@ApiModelProperty(value = "是否启用")
private Boolean enabled;
@ApiModelProperty(value = "是否上级")
private Boolean isParent;
@ApiModelProperty(value = "子部门列表")
@TableField(exist = false)
private List children;
@ApiModelProperty(value = "返回结果,存储过程使用")
@TableField(exist = false)
private Integer result;
}
2. controller层
@ApiOperation(value = "获取所有职位")
@GetMapping("/")
public List getAllDepartment(){
return departmentService.getAllDepartment();
}
3. service层
⭐注意点: 这里注意传一个-1,具体逻辑在上面说了。
@Override
public List getAllDepartment() {
return departmentMapper.getAllDepartment(-1);
}
4. mapper的xml
⭐知识解读:
getAllDepartment这个查询语句通过
标签引入的 标签下设置的查询字段DepartmentWithChildren这个 自定义返回的格式继承了extends了baseResultMap设置的基础的查询列,然后增了 设置的集合属性,集合属性的每个元素Department类型,通过
2.3 增加一个部门(⭐运用存储过程就是联动性的crud) 1. controller层id, name, parentId, depPath, enabled, isParent
@ApiOperation(value = "添加一个部门")
@PostMapping("/")
public RespBean addDepartment(@RequestBody Department department) {
return departmentService.addDepartment(department);
}
2. service层
⭐知识点:当完成addDepartment的逻辑后,department的result的属性会改变,存储影响的记录数。
这里注意数据库函数的情况
@Override
public RespBean addDepartment(Department department) {
department.setEnabled(true);
departmentMapper.addDepartment(department);
if (department.getResult() == 1) {
return RespBean.success("部门添加成功!");
}
return RespBean.warning("部门添加失败!");
}
3. mapper的xml写法 ⭐
这里的写法对应了数据库里的存储过程:2.1 存储过程
⭐知识点:
示例:
@ApiOperation(value = "删除一个部门")
@DeleteMapping("/{id}")
public RespBean deleteDepartment(@PathVariable Integer id) {
return departmentService.deleteDepartment(id);
}
2. service层
⭐知识点: 注意这里department的result属性会在执行完后保存返回的修改记录数,里面的值与上面设置的存储过程的删除过程设置的值有绑定。
@Override
public RespBean deleteDepartment(Integer id) {
Department department = new Department();
department.setId(id);
departmentMapper.deleteDepartment(department);
if (department.getResult() == -2) {
return RespBean.warning("该部门下有子部门,删除失败");
}
if (department.getResult() == -1) {
return RespBean.warning("该部门下有员工,删除失败");
}
if (department.getResult() == 1) {
return RespBean.success("删除部门成功");
}
return RespBean.warning("删除部门失败");
}
3. mapper的xml
⭐知识点:通过call调用数据库的自定义函数
call deleteDep(#{id, mode=IN, jdbcType=INTEGER}, #{result, mode=OUT, jdbcType=INTEGER})
示例:



