Javaweb-员工管理部分
web后端开发
一、删除员工
1.知识点
参数接收
@DeleteMapping public Result delete(@RequestParam List<Integer> ids){//列表传递需要额外加注解1
2
3
4
* ~~~java
@DeleteMapping
public Result delete(Integer[] ids){ //数组传递直接对应名字即可
2.具体实现
comtroller
@DeleteMapping public Result delete(@RequestParam List<Integer> ids){ log.info("删除员工,参数:{}",ids); empService.delete(ids); return Result.success(); }1
2
3
4
5
6
7
8
9
10
* **service**
* ~~~java
@Override
public void delete(List<Integer> ids) {
empMapper.deleteBatch(ids);
empExprMapper.deleteBatch(ids);
}
mapper
删除员工经历 <delete id="deleteBatch"> delete from emp_expr where emp_id in <foreach collection="ids" item="id" open="(" close=")" separator=","> #{id} </foreach> </delete>1
2
3
4
5
6
7
8
9
* ~~~xml
删除员工
<delete id="deleteBatch">
delete from emp where id in
<foreach collection="ids" item="id" open="(" close=")" separator=",">
#{id}
</foreach>
</delete>

- 成功实现
二、修改员工
1.知识点
多条结果手动封装实现
适用于类型太复杂无法自动封装
<select id="getinfo" resultMap="empResultMap">1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
* **具体定义map**
* ~~~xml
<resultMap id="empResultMap" type="com.itheima.pojo.Emp">
<id column="id" property="id"/>
<result column="username" property="username"/>
<result column="password" property="password"/>
<collection property="exprList" ofType = "com.itheima.pojo.EmpExpr">
<id column="ee_id" property="id"/>
<result column="begin" property="begin"/>
</collection>id属性需要用单独用id封装,集合用collection封装
更新语句设置判断
适用于数据库更新语句
<mapper namespace="com.itheima.mapper.EmpMapper"> <update id="update"> update emp <set> <if test="username != null">username = #{username},</if> <if test="updateTime != null">update_time = #{updateTime},</if> </set> where id = #{id} </update>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
* **set标签可以直接自动补全set语句,同时适配逗号**
### **2.查询回显**
* **具体实现**
* **controller**
* ~~~java
@GetMapping("/{id}")
public Result getEmpById(@PathVariable Integer id){
log.info("根据id查询员工,参数:{}",id);
Emp emp = empService.getEmpById(id);
return Result.success(emp);
}
service
@Override public Emp getEmpById(Integer id) { return empMapper.getinfo(id); }1
2
3
4
5
6
7
8
9
10
11
12
13
14
* **mapper**
* ~~~java
<select id="getinfo" resultMap="empResultMap">
select e.*,
ee.id as ee_id,
ee.begin as expr_begin,
ee.end as expr_end,
ee.company as expr_company,
ee.job as expr_job
from emp e left join emp_expr ee on e.id = ee.emp_id
where e.id = #{id}
</select>
3.修改员工
具体实现
controller
@PutMapping public Result update(@RequestBody Emp emp){ log.info("更新员工,参数:{}",emp); empService.update(emp); return Result.success(); }1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
* **service**
* ~~~java
@Transactional(rollbackFor = Exception.class)
@Override
public void update(Emp emp) {
emp.setUpdateTime(LocalDateTime.now());
empMapper.update(emp);
Integer id = emp.getId();
List<EmpExpr> list = emp.getExprList();
if(!list.isEmpty()){
empExprMapper.deleteBatch(Arrays.asList(id));
list.forEach(item->{
item.setEmpId(id);
});
empExprMapper.insertBatch(list);
}添加事务管理
mapper
<mapper namespace="com.itheima.mapper.EmpMapper"> <update id="update"> update emp <set> <if test="username != null">username = #{username},</if> <if test="password != null">password = #{password},</if> <if test="name != null">name = #{name},</if> <if test="gender != null">gender = #{gender},</if> <if test="phone != null">phone = #{phone},</if> <if test="job != null">job = #{job},</if> <if test="salary != null">salary = #{salary},</if> <if test="image != null">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>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
## 三、全局异常管理器
### 1.异常处理
* <img src="https://chuanchua.oss-cn-beijing.aliyuncs.com/markdown/image-20251106164839467.png" alt="image-20251106164839467" style="zoom:50%;" />
### 2.处理异常
* **寻常tyr-catch可以处理但是太过繁琐**
* **使用全局异常管理器即可**
* ~~~java
@RestControllerAdvice
@Slf4j
public class GlobalExceptionExceptionHandler {
@ExceptionHandler
public Result doException(Exception e){
log.error("全局异常捕获",e);
return Result.error("程序出错啦!!!");
}
}
创建代码如上
3.设置具体提示信息
1 |
|
- 异常匹配规则为由下至上,如上捕获重复异常

四、员工信息统计
- echart制作报表,只需要传递数据参数即可
1.知识点
sql语句查询
如果遇到用数字代表名字使用case关键字
select (case job when 1 then '班主任' when 2 then '讲师' when 3 then '学工主管' when 4 then '教研主任' when 5 then '咨询师' else '其他' end) pos, count(*) num from emp group by job order by num1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
* **when后面也可以写判断表达式**
* **如果查询条件只有俩个,可以使用if关键字**
### 2.职位统计
* **创建bean类**
* ~~~java
@Data
@AllArgsConstructor
@NoArgsConstructor
public class JobOption {
private List jobList;
private List dataList;
}
代码实现
3.性别统计
具体实现
mapper <select id="getEmpGenderData" resultType="java.util.Map"> select if(gender = 1, '男性员工', '女性员工') name, count(*) value from emp group by gender order by value </select>1
2
3
4
5
6
7
8
9
* ~~~java
controller
@GetMapping("empGenderData")
public Result getEmpGenderData(){
log.info("getEmpGenderData");
List<Map<String, Objects>> genderCount = reportService.getEmpGenderData();
return Result.success(genderCount);
}service @Override public List<Map<String, Objects>> getEmpGenderData() { List<Map<String, Objects>> genderCount = empMapper.getEmpGenderData(); return genderCount;
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来源 十七.!