【JavaWeb学习Day19】

news/2025/2/26 6:54:00

Tlias智能学习系统(员工管理)

删除员工:

需求分析:

其实,删除单条数据也是一种特殊的批量删除,所以,删除员工的功能,我们只需要开发一个接口就行了。

三层架构:

Controller:1.接收请求参数(ID值)2.调用Service方法3.响应结果

具体实现:

方式一:

在Controller方法中通过数组来接收

java">/**
 * 删除员工 - 数组
 */
@DeleteMapping
public Result delete(Integer[] ids){
    log.info("删除员工:{}", Arrays.toString(ids));
    return Result.success();
}

方式二:

在Controller方法中通过集合来接收

java">/**
 * 删除员工 - 集合
 */
@DeleteMapping
public Result delete(@RequestParam List<Integer> ids){
    log.info("删除员工:{}", ids);
    return Result.success();
}

Service:1.批量删除员工基本信息2.批量删除员工的工作经历信息

java">/**
 * 删除员工
 * @param ids
 */
@Transactional(rollbackFor = {Exception.class})
@Override
public void delete(List<Integer> ids) {
    //1.批量删除员工信息
    empMapper.deleteByIds(ids);
​
    //2.批量删除员工工作经历信息
    empExprMapper.deleteByEmpIds(ids);
​
}

SQL:

-- 删除员工
delete from emp where id in (1,2,3);
delete from emp_expr where emp_id in (1,2,3);
/**
 *根据员工ID删除员工基本信息
 */
void deleteByIds(List<Integer> ids);
<delete id="deleteByIds">
    delete from emp where id in
    <foreach collection="ids" item="id" open="(" close=")" separator=",">
        #{id}
    </foreach>
</delete>
java">/**
 * 根据员工id批量删除员工工作经历
 */
void deleteByEmpIds(List<Integer> empIds);
<delete id="deleteByEmpIds">
    delete from emp_expr where emp_id in
    <foreach collection="empIds" item="empId" separator="," close=")" open="(">
        #{empId}
    </foreach>

修改员工信息:

查询回显:

三层架构:

Controller:1.接收请求参数(ID值)2.调用Service层方法3.响应结果

java">/**
 * 根据ID查询员工信息
 */
@GetMapping("/{id}")
public Result getInfo(@PathVariable Integer id){
    log.info("根据ID查询员工信息:{}",id);
    Emp emp = empService.getInfo(id);
    return Result.success(emp);
}

Service:调用Mapper查询员工详细信息(基本信息,工作经历信息)

java">    @Override
    public Emp getInfo(Integer id) {
        return  empMapper.getById(id);
    }

Mapper:

-- 根据ID查询员工基本信息(emp)以及员工工作经历信息(emp_expr)
-- 外连接
select
    e.*,
    ee.id ee_id,
    ee.emp_id ee_empid,
    ee.begin ee_begin,
    ee.end ee_end,
    ee.company ee_company,
    ee.job ee_job
from emp e left join tlias.emp_expr ee on e.id = ee.emp_id
where e.id=37;
java">/**
 * 根据员工ID查询员工信息
 */
Emp getById(Integer id);
<!--自定义结果集ResultMap-->
<resultMap id="empResultMap" type="com.itheima.pojo.Emp">
    <id column="id" property="id" />
    <result column="username" property="username" />
    <result column="password" property="password" />
    <result column="name" property="name" />
    <result column="gender" property="gender" />
    <result column="phone" property="phone" />
    <result column="job" property="job" />
    <result column="salary" property="salary" />
    <result column="image" property="image" />
    <result column="entry_date" property="entryDate" />
    <result column="dept_id" property="deptId" />
    <result column="create_time" property="createTime" />
    <result column="update_time" property="updateTime" />
​
    <!--封装exprList-->
    <collection property="exprList" ofType="com.itheima.pojo.EmpExpr">
        <id column="ee_id" property="id"/>
        <result column="ee_company" property="company"/>
        <result column="ee_job" property="job"/>
        <result column="ee_begin" property="begin"/>
        <result column="ee_end" property="end"/>
        <result column="ee_empid" property="empId"/>
    </collection>
</resultMap>
​
<!--根据ID查询员工的详细信息-->
<select id="getById" resultMap="empResultMap">
    select e.*,
           ee.id ee_id,
           ee.emp_id ee_empid,
           ee.begin ee_begin,
           ee.end ee_end,
           ee.company ee_company,
           ee.job ee_job
    from emp e left join emp_expr ee on e.id = ee.emp_id
    where e.id = #{id}
</select>

(注意:如果查询返回的字段与实体的属性名可以直接对上,用resultType,对不上,或者实体属性比较复杂,可以通过resultMap手动封装)

修改数据:

三层架构:

Controller:1.接收请求参数2.调用Service方法3.响应结果

java">/**
 * 修改员工数据
 */
​
@PutMapping
public Result update(@RequestBody Emp emp){
    log.info("修改员工数据:{}",emp);
    empService.update(emp);
    return Result.success();
}

Service:1.根据ID修改员工的基本信息2.根据ID修改员工工作经历信息(先删再添加)

java">/**
 * 修改员工信息
 */
@Override
public void update(Emp emp) {
    //1.根据Id修改员工的基本信息
    emp.setUpdateTime(LocalDateTime.now());
    empMapper.updateById(emp);
    //2.根据ID修改员工的工作经历信息
    //2.1先删除
    empExprMapper.deleteByEmpIds(Arrays.asList(emp.getId()));
    //2.2再添加
    List<EmpExpr> exprList = emp.getExprList();
    if(!CollectionUtils.isEmpty(exprList)){
        exprList.forEach(empExpr -> empExpr.setEmpId(emp.getId()));
        empExprMapper.insertBatch(exprList);
    }
}

Mapper:

java">/**
 *根据ID更新员工的基本信息
 */
void updateById(Emp emp);
    <!--根据ID更新员工信息-->
<!--    set标签:会自动生成set关键字;会自动的删除掉更新字段后多余的,-->
    <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>
异常处理:

全局异常处理器:

java">/**
 * 全局异常处理器
 */
@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);
        String[] arr =  errMsg.split(" ");
        return Result.error(arr[2]+ "已存在");
    }
}
员工信息统计:
职位统计:

三层架构:

Controller:1.接收请求2.调用Service方法3.响应结果

java">/**
 * 统计员工职位人数
 */
@GetMapping("/empJobData")
public Result getEmpJobDate(){
​
    log.info("统计员工职位人数");
    JobOption jobOption=  reportService.getEmpJobDate();
    return Result.success(jobOption);
}

Service:1.调用mapper方法获取职位统计数据2.解析封装统计结果(JobOption)

  
java">  @Override
    public JobOption getEmpJobDate() {
​
        //1.调用mapper接口,获取统计数据
        List<Map<String,Object>> list = empMapper.countEmpJobData();//map:pos = 教研主管,num = 1
        //2.组装结果,并返回
        List<Object> jobList = list.stream().map(dataMap->dataMap.get("pos")).toList();
        List<Object> numList = list.stream().map(dataMap->dataMap.get("num")).toList();
​
        return new JobOption(jobList,numList);
    }

Mapper:

-- 统计每一种职位对应的人数
-- case函数:case表达式 when val1 then result1 when val2 then result2 .... 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;
-- 
-- case函数:case when 表达式 1 then result1 when 表达式 2 then result2 .... else ... end
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;
java">/**
 * 统计员工职位人数
 */
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>
</mapper>
性别统计:

三层架构:

Controller:1.接收请求2.调用Service方法3.响应结果

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

Service:1.调用mapper方法获取职位统计数据2.解析封装统计结果(JobOption)

java">    @Override
    public List<Map<String, Object>> getEmpGenderData() {
        return empMapper.countEmpGenderDate();
    }

Mapper:

-- 统计员工性别人数
-- if(条件,true_value,false_value)
-- ifnull(expr,val1):如果expr不为null,取自身,否则取val1
select
    if(gender = 1,'男性员工','女性员工') name,
    count(*) value
from emp group by gender ;
java">/**
 * 统计员工性别人数
 */
List<Map<String, Object>> countEmpGenderDate();
<select id="countEmpGenderDate" resultType="java.util.Map">
    select
        (case when gender = 1 then'男性员工'when gender = 2 then '女性员工' end) name,
        count(*) value
    from emp group by gender ;
</select>


http://www.niftyadmin.cn/n/5865115.html

相关文章

Oracle Fusion Middleware更改weblogic密码

前言 当用户忘记weblogic密码时&#xff0c;且无法登录到web界面中&#xff0c;需要使用服务器命令更改密码 更改方式 1、备份 首先进入 weblogic 安装目录&#xff0c;备份三个文件&#xff1a;boot.properties&#xff0c;DefaultAuthenticatorInit.ldift&#xff0c;Def…

【Microsoft PowerPoint for Mac】2分钟配置-MAC一键删除PPT中的所有备注

MAC一键删除PPT中的所有备注 1.搜索自动操作2.点击快速操作3.搜索并运行AppleScript4.输入代码&#xff0c;并选择只应用于Microsoft PowerPoint for Mac【右上角】5. CRTLS保存为“清除当前文稿中的所有备注”&#xff0c;PPT中应用。 MAC没自带&#xff0c;需要自己配置 1.搜…

Tag标签的使用

一个非常适合运用在vue项目中的组件&#xff1a;Tag标签。 目录 一、准备工作 1、安装element-plus库 2、配置element-plus库 二、Tag标签入门 1、打开element官网&#xff0c;搜索tag标签 2、体验Tag标签的基础用法 三、Tag标签进阶训练1 1、定义一个数组&#xff0c;…

分库分表中间件开源

根据你的需求&#xff0c;以下是一些可以实现分库分表功能的中间件&#xff0c;这些项目可以帮助你管理分布式数据库环境中的数据分片和路由&#xff1a; 1. ShardingSphere ShardingSphere 是一个开源的分布式数据库中间件&#xff0c;提供了分库分表、读写分离、分布式事务…

如何解决 Django 网站登录人数过多导致的性能问题

引言 随着用户量的增加,Django 开发的网站可能会面临登录人数过多导致的性能问题。这些问题可能包括数据库压力大、响应时间变长、服务器负载过高等。本文将详细分析这些问题的根源,并提供一系列解决方案,帮助你优化 Django 网站的性能。 1. 问题分析 当登录人数过多时,D…

clickhouse--表引擎的使用

表引擎决定了如何存储表的数据。包括&#xff1a; 数据的存储方式和位置&#xff0c;写到哪里以及从哪里读取数据。(默认是在安装路径下的 data 路径)支持哪些查询以及如何支持。&#xff08;有些语法只有在特定的引擎下才能用&#xff09;并发数据访问。索引的使用&#xff0…

Trae IDE Remote-SSH不能连接问题解决办法

一、问题现象描述 安装Remote - SSH for Trae后&#xff0c;发现无法连接访问ssh服务器。 二、解决办法 先说解决办法&#xff1a; 下载: trae-remote-ssh-script.zip 链接: https://pan.baidu.com/s/1yO6ny8huafv4L3x-pYKSxw?pwd445r 提取码: 445r 然后将这个包传输到远程…

Css3重点知识讲解

选择器 优先级&#xff1a; id 选择器 > 类选择器 > 标签选择器 类选择器&#xff1a; .myClass {color: blue; }id 选择器&#xff08;全局唯一&#xff09;&#xff1a; #myId {color: green; }标签选择器&#xff1a; p {color: red; }层次选择器&#xff1a; /…