Controller
/**
* 批量删除员工
*/
@Operation(summary = "根据ID批量删除员工")
@DeleteMapping("/{ids}")
public Result delete(@PathVariable List<Integer> ids) {
log.info("批量删除员工,IDs:{}", ids);
empService.deleteByIds(ids);
return Result.success();
}
/**
* 修改员工
*/
@Operation(summary = "修改员工")
@PutMapping
public Result update(@RequestBody Emp emp) {
log.info("修改员工:{}", emp);
empService.update(emp);
return Result.success();
}
}
sverver
/**
* 批量删除员工(同时删除员工的工作经历)
* @param ids 员工ID列表
*/
@Transactional(rollbackFor = Exception.class) //事务:出现异常回滚
@Override
public void deleteByIds(List<Integer> ids) {
if (ids == null || ids.isEmpty()) {
return;
}
//1. 批量删除员工的工作经历信息
empExprMapper.deleteByEmpIds(ids);
//2. 批量删除员工基本信息
empMapper.deleteByIds(ids);
}
/**
* 修改员工(同时更新员工的工作经历)
* @param emp
*/
@Transactional(rollbackFor = Exception.class) //事务:出现异常回滚
@Override
public void update(Emp emp) {
//1. 补全修改时间
emp.setUpdateTime(LocalDateTime.now());
//2. 更新员工基本信息
empMapper.updateById(emp);
//3. 先删除该员工的旧工作经历,再批量保存新的工作经历(页面传回的是全量数据)
empExprMapper.deleteByEmpId(emp.getId());
List<EmpExpr> exprList = emp.getExprList();
if (exprList != null && !exprList.isEmpty()) {
exprList.forEach(expr -> {
expr.setEmpId(emp.getId()); //设置经历对应的员工ID
expr.setId(null); //旧主键置空,避免影响自增插入
});
empExprMapper.insertBatch(exprList);
}
}
mapper
package com.itheima.mapper;
import com.itheima.pojo.EmpExpr;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
@Mapper
public interface EmpExprMapper {
/**
* 批量插入员工工作经历信息
* @param exprList
*/
void insertBatch(List<EmpExpr> exprList);
/**
* 根据员工ID批量删除工作经历
* @param empIds
*/
void deleteByEmpIds(@Param("empIds") List<Integer> empIds);
/**
* 根据员工ID删除工作经历
* @param empId
*/
void deleteByEmpId(Integer empId);
/**
* 根据员工ID查询工作经历列表
* @param empId
*/
List<EmpExpr> getByEmpId(Integer empId);
}
package com.itheima.mapper;
import com.itheima.pojo.EmpExpr;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
@Mapper
public interface EmpExprMapper {
/**
* 批量插入员工工作经历信息
* @param exprList
*/
void insertBatch(List<EmpExpr> exprList);
/**
* 根据员工ID批量删除工作经历
* @param empIds
*/
void deleteByEmpIds(@Param("empIds") List<Integer> empIds);
/**
* 根据员工ID删除工作经历
* @param empId
*/
void deleteByEmpId(Integer empId);
/**
* 根据员工ID查询工作经历列表
* @param empId
*/
List<EmpExpr> getByEmpId(Integer empId);
}
SQL语句
<?xml version="1.0" encoding="UTF-8" ?>
<!--定义Mapper映射文件的约束和基本结构-->
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.itheima.mapper.EmpMapper">
<!-- 分页条件查询员工列表:左连接部门表带出部门名称,根据条件对象动态拼接 where 条件 -->
<select id="list" resultType="com.itheima.pojo.Emp">
select e.*, d.name deptName from emp as e left join dept as d on e.dept_id = d.id
<where>
<if test="name != null and name != ''">
e.name like concat('%',#{name},'%')
</if>
<if test="gender != null">
and e.gender = #{gender}
</if>
<if test="begin != null and end != null">
and e.entry_date between #{begin} and #{end}
</if>
</where>
</select>
<!-- 根据ID批量删除员工:ids 拼接成 id in (...) 条件 -->
<delete id="deleteByIds">
delete from emp where id in
<foreach collection="ids" item="id" open="(" separator="," close=")">
#{id}
</foreach>
</delete>
<!-- 根据ID修改员工:动态 set,只更新非 null 的字段,未传值的字段保持原值 -->
<update id="updateById">
update emp
<set>
<if test="username != null and username != ''">username = #{username},</if>
<if test="password != null and password != ''">password = #{password},</if>
<if test="name != null and name != ''">name = #{name},</if>
<if test="gender != null">gender = #{gender},</if>
<if test="phone != null and phone != ''">phone = #{phone},</if>
<if test="job != null">job = #{job},</if>
<if test="salary != null">salary = #{salary},</if>
<if test="image != null and image != ''">image = #{image},</if>
<if test="entryDate != null">entry_date = #{entryDate},</if>
<if test="deptId != null">dept_id = #{deptId},</if>
<if test="updateTime != null">update_time = #{updateTime},</if>
</set>
where id = #{id}
</update>
</mapper>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.itheima.mapper.EmpExprMapper">
<!-- 批量插入员工工作经历:一条 insert 语句拼接多组 values,emp_id 由 service 层回填 -->
<insert id="insertBatch">
insert into emp_expr (emp_id, begin, end, company, job) values
<foreach collection="exprList" item="expr" separator=",">
(#{expr.empId}, #{expr.begin}, #{expr.end}, #{expr.company}, #{expr.job})
</foreach>
</insert>
<!-- 根据员工ID批量删除工作经历:配合员工批量删除接口,清理多个员工的全部经历 -->
<delete id="deleteByEmpIds">
delete from emp_expr where emp_id in
<foreach collection="empIds" item="empId" open="(" separator="," close=")">
#{empId}
</foreach>
</delete>
<!-- 根据员工ID删除工作经历:配合修改接口,先删旧经历再插新经历 -->
<delete id="deleteByEmpId">
delete from emp_expr where emp_id = #{empId}
</delete>
<!-- 根据员工ID查询工作经历列表:按开始时间降序,用于编辑页面回显 -->
<select id="getByEmpId" resultType="com.itheima.pojo.EmpExpr">
select id, emp_id, begin, end, company, job from emp_expr where emp_id = #{empId} order by begin desc
</select>
</mapper>

2万+

被折叠的 条评论
为什么被折叠?



