JAVAWEB+SpringBoot3+vue3开发+教学管理系统项目实战之查询回显更新修改员工信息
C层:
- package com.zidiu.controller;
-
- import com.zidiu.pojo.Emp;
- import com.zidiu.pojo.EmpQueryParam;
- import com.zidiu.pojo.PageResult;
- import com.zidiu.pojo.Result;
- import com.zidiu.service.EmpService;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.format.annotation.DateTimeFormat;
- import org.springframework.web.bind.annotation.*;
-
- import java.time.LocalDate;
- import java.util.Arrays;
- import java.util.List;
-
- @RequestMapping("/emps")
- @Slf4j
- @RestController
- public class EmpController {
- @Autowired
- private EmpService empService;
-
- /**
- * 查询员工列表带分页
- */
- @GetMapping
- public Result page(EmpQueryParam empQueryParam) {
- log.info("查询请求参数: {}", empQueryParam);
- PageResult pageResult = empService.page(empQueryParam);
- return Result.success(pageResult);
- }
-
- /**
- * 添加员工
- */
- @PostMapping
- public Result save(@RequestBody Emp emp){
- log.info("请求参数emp: {}", emp);
- empService.save(emp);
- return Result.success();
- }
-
- /**
- * 批量删除员工
- * 在Controller方法中通过数组来接收
- */
- // @DeleteMapping
- // public Result delete(Integer[] ids){
- // log.info("批量删除部门: ids={} ", Arrays.asList(ids));
- // return Result.success();
- // }
-
- /**
- * 批量删除员工
- * 在Controller方法中通过集合来接收,推荐
- */
- @DeleteMapping
- public Result delete(@RequestParam List<Integer> ids){
- log.info("批量删除部门: ids={} ", ids);
- empService.deleteByIds(ids);
- return Result.success();
- }
-
- /**
- * 查询回显
- */
- @GetMapping("/{id}")
- public Result getInfo(@PathVariable Integer id){
- log.info("根据id查询员工的详细信息");
- Emp emp = empService.getInfo(id);
- return Result.success(emp);
- }
-
- /**
- * 更新员工信息
- */
- @PutMapping
- public Result update(@RequestBody Emp emp){
- log.info("修改员工信息, {}", emp);
- empService.update(emp);
- return Result.success();
- }
- }
复制代码 S层:
- package com.zidiu.service;
-
- import com.zidiu.pojo.Emp;
- import com.zidiu.pojo.EmpQueryParam;
- import com.zidiu.pojo.PageResult;
-
- import java.time.LocalDate;
- import java.util.List;
-
- public interface EmpService {
- /**
- * 分页查询
- */
- PageResult<Emp> page(EmpQueryParam empQueryParam);
-
- /**
- * 添加员工
- * @param emp
- */
- void save(Emp emp);
-
- /**
- * 批量删除员工
- */
- void deleteByIds(List<Integer> ids);
-
- /**
- * 根据ID查询员工的详细信息
- */
- Emp getInfo(Integer id);
-
- /**
- * 修改员工信息
- */
- void update(Emp emp);
- }
复制代码 实现层:
- package com.zidiu.service.impl;
-
- import com.github.pagehelper.Page;
- import com.github.pagehelper.PageHelper;
- import com.zidiu.mapper.EmpExprMapper;
- import com.zidiu.mapper.EmpMapper;
- import com.zidiu.pojo.Emp;
- import com.zidiu.pojo.EmpExpr;
- import com.zidiu.pojo.EmpQueryParam;
- import com.zidiu.pojo.PageResult;
- import com.zidiu.service.EmpService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.annotation.Transactional;
- import org.springframework.util.CollectionUtils;
-
- import java.time.LocalDate;
- import java.time.LocalDateTime;
- import java.util.Arrays;
- import java.util.List;
-
- @Service
- public class EmpServiceImpl implements EmpService {
- @Autowired
- private EmpMapper empMapper;
- @Autowired
- private EmpExprMapper empExprMapper;
-
- /**
- * 查询员工列表
- */
- @Override
- public PageResult page(EmpQueryParam empQueryParam) {
- //1. 设置PageHelper分页参数
- PageHelper.startPage(empQueryParam.getPage(), empQueryParam.getPageSize());
- //2. 执行查询
- List<Emp> empList = empMapper.list(empQueryParam);
- //3. 封装分页结果
- Page<Emp> p = (Page<Emp>)empList;
- return new PageResult(p.getTotal(), p.getResult());
- }
-
- /**
- * 添加员工
- * @param emp
- */
- @Transactional
- @Override
- public void save(Emp emp) {
- //1.补全基础属性
- emp.setCreateTime(LocalDateTime.now());
- emp.setUpdateTime(LocalDateTime.now());
-
- //2.保存员工基本信息
- empMapper.insert(emp);
复制代码 M层:
- package com.zidiu.mapper;
-
- import com.zidiu.pojo.Emp;
- import com.zidiu.pojo.EmpQueryParam;
- import org.apache.ibatis.annotations.Insert;
- import org.apache.ibatis.annotations.Mapper;
- import org.apache.ibatis.annotations.Options;
- import org.apache.ibatis.annotations.Select;
-
- import java.time.LocalDate;
- import java.util.List;
-
- @Mapper
- public interface EmpMapper {
- /**
- * 查询所有的员工及其对应的部门名称
- */
- List<Emp> list(EmpQueryParam empQueryParam);
-
- /**
- * 新增员工数据
- */
- @Options(useGeneratedKeys = true, keyProperty = "id")
- @Insert("insert into emp(username, name, gender, phone, job, salary, image, entry_date, dept_id, create_time, update_time) " +
- "values (#{username},#{name},#{gender},#{phone},#{job},#{salary},#{image},#{entryDate},#{deptId},#{createTime},#{updateTime})")
- void insert(Emp emp);
-
- /**
- * 批量删除员工
- */
- void deleteByIds(List<Integer> ids);
-
- /**
- * 根据ID查询员工信息
- */
- Emp getById(Integer id);
-
- /**
- * 修改员工信息
- */
- void updateById(Emp emp);
- }
复制代码 XML层:
- <!--定义Mapper映射文件的约束和基本结构-->
- <!DOCTYPE mapper
- PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
- "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
- <mapper namespace="com.zidiu.mapper.EmpMapper">
- <!--根据ID更新员工信息-->
- <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>
- <!--批量删除员工信息-->
- <delete id="deleteByIds">
- delete from emp where id in
- <foreach collection="ids" item="id" open="(" close=")" separator=",">
- #{id}
- </foreach>
- </delete>
- <select id="list" resultType="com.zidiu.pojo.Emp">
- select e.*, d.name deptName from emp as e left join dept as d on e.dept_id = d.id order by e.id desc
- <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>
- <!--自定义结果集ResultMap-->
- <resultMap id="empResultMap" type="com.zidiu.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.zidiu.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>
- </mapper>
复制代码 <set>:替换set关键字,去除字段之后多余的逗号(,)
SpringBoot3+Vue3开发综合实战项目:
JavaWeb(SpringBoot3+vue3)开发+教学管理系统项目实战
|