Day48(18)-F:\硕士阶段\Java\课程代码\后端\web-ai-code\web-ai-project02\tlias-web-management

异常管理

image-20251125124023856

image-20251125124254183

package com.itheima.exception;


import com.itheima.pojo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理器
 */

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler
    public Result handleException(Exception e){
        log.error("程序出错啦:",e);
        return Result.error("出错啦,请联系管理员");
    }
    @ExceptionHandler
    public Result handleDuplicateKeyException(DuplicateKeyException e){
        log.error("程序出错啦:",e);
        String message = e.getMessage();
        int i = message.indexOf("Duplicate entry");
        String errMsg = message.substring(i);//Duplicate entry '18809091212' for key 'emp.phone'
        String[] arr = errMsg.split(" ");
        return Result.error(arr[2]+"已存在");
    }
}

@ResponseBody将方法的返回值响应给前端,如果方法的返回值是集合或对象,那么会讲方法的返回值先转换为json再响应

image-20251125132103316

信息统计

图形报表插件echarts

image-20251125134506905

-- 统计每一种职位对应的人数
-- case函数:case表达式 when val1 then res1 when val2 then res2 ... else...end
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 num;

select
    (case  when job=1 then '班主任'
        when job=2 then '讲师'
        when job=3 then '学工主管'
        when job=4 then '教研主管'
        when job=5 then '咨询师'
        else '其他' end) pos,
    count(*) num
from emp group by job order by num;9
package com.itheima.controller;


import com.itheima.pojo.JobOption;
import com.itheima.pojo.Result;
import com.itheima.service.ReportService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/report")
public class ReportController {
    @Autowired
    private ReportService reportService;
    @GetMapping("/empJobData")
    public Result getEmpJobData(){
        log.info("统计员工职位人数");
       com.itheima.pojo.JobOption jobOption = reportService.getEmpJobData();
       return Result.success(jobOption);
    }
}
package com.itheima.service.impl;


import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.JobOption;
import com.itheima.service.ReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class ReportServiceImpl implements ReportService {
    @Autowired
    private EmpMapper empMapper;
    /**
     * 统计员工职位人数
     */
    @Override
    public JobOption getEmpJobData() {
        //1. 掉用mapper接口,获取统计数据
        List<Map<String, Object>> list = empMapper.countEmpJobData();//map: pos=教研主管,num=1
        //2.组装结果,并返回
        List<Object> jobList = list.stream().map(dataMap -> {
            return dataMap.get("pos");
        }).toList();
        List<Object> dataList = list.stream().map(dataMap -> dataMap.get("num")).toList();

        return new JobOption(jobList,dataList);
    }
}
/**
 * 统计员工职位人数
 * @return
 */
List<Map<String, Object>> countEmpJobData();
<!--    统计员工职位人数-->
    <select id="countEmpJobData" resultType="java.util.Map">
        select
            (case  when job=1 then '班主任'
                   when job=2 then '讲师'
                   when job=3 then '学工主管'
                   when job=4 then '教研主管'
                   when job=5 then '咨询师'
                   else '其他' end) pos,
            count(*) num
        from emp group by job order by num;
    </select>

image-20251125152407378

image-20251125152448821

image-20251125152838457

package com.itheima.controller;


import com.itheima.pojo.GenderOption;
import com.itheima.pojo.JobOption;
import com.itheima.pojo.Result;
import com.itheima.service.ReportService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/report")
public class ReportController {
    @Autowired
    private ReportService reportService;
    @GetMapping("/empJobData")
    public Result getEmpJobData(){
        log.info("统计员工职位人数");
       com.itheima.pojo.JobOption jobOption = reportService.getEmpJobData();
       return Result.success(jobOption);
    }

    /**
     * 统计员工性别人数
     * @return
     */
    @GetMapping("/empGenderData")
    public Result getEmpGenderData(){
        log.info("统计员工性别人数");
        //GenderOption genderOption = reportService.getEmpGenderData();
        List<Map<String,Object>> genderList = reportService.getEmpGenderData();
        //return Result.success(genderOption);
        return Result.success(genderList);
    }
}
package com.itheima.service.impl;


import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.GenderOption;
import com.itheima.pojo.JobOption;
import com.itheima.service.ReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class ReportServiceImpl implements ReportService {
    @Autowired
    private EmpMapper empMapper;
    /**
     * 统计员工职位人数
     */
    @Override
    public JobOption getEmpJobData() {
        //1. 掉用mapper接口,获取统计数据
        List<Map<String, Object>> list = empMapper.countEmpJobData();//map: pos=教研主管,num=1
        //2.组装结果,并返回
        List<Object> jobList = list.stream().map(dataMap -> {
            return dataMap.get("pos");
        }).toList();
        List<Object> dataList = list.stream().map(dataMap -> dataMap.get("num")).toList();

        return new JobOption(jobList,dataList);
    }

    @Override
    //public GenderOption getEmpGenderData() {
    public List<Map<String,Object>> getEmpGenderData() {
        //List<Map<String, Object>> list = empMapper.countEmpGenderData();
//        List<Object> name = list.stream().map(dataMap -> dataMap.get("name")).toList();
//        List<Object> value = list.stream().map(dataMap -> dataMap.get("value")).toList();
        //return new GenderOption(name,value);
        //return list;
        //简化
        return empMapper.countEmpGenderData();
    }
}
<!--    统计员工性别人数-->
    <select id="countEmpGenderData" resultType="java.util.Map">
        select
            if(gender=1,'男性员工','女性员工') name,
            count(*) value
        from emp group by gender;
    </select>
posted @ 2025-11-25 20:37  David大胃  阅读(3)  评论(0)    收藏  举报