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;
-
- @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();
- }
- }
复制代码 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);
- }
复制代码 实现层:
- 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.util.CollectionUtils;
-
- import java.time.LocalDate;
- import java.time.LocalDateTime;
- 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
- */
- @Override
- public void save(Emp emp) {
- //1.补全基础属性
- emp.setCreateTime(LocalDateTime.now());
- emp.setUpdateTime(LocalDateTime.now());
-
- //2.保存员工基本信息
- empMapper.insert(emp);
-
- //3. 保存员工的工作经历信息 - 批量
- Integer empId = emp.getId();
- List<EmpExpr> exprList = emp.getExprList();
- if(!CollectionUtils.isEmpty(exprList)){
- exprList.forEach(empExpr -> empExpr.setEmpId(empId));
- empExprMapper.insertBatch(exprList);
- }
- }
- }
复制代码 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);
- }
复制代码 主键返回:@Options(useGeneratedKeys = true, keyProperty = "id")
由于稍后,我们在保存工作经历信息的时候,需要记录是哪位员工的工作经历。 所以,保存完员工信息之后,是需要获取到员工的ID的,那这里就需要通过Mybatis中提供的主键返回功能来获取。
X层:
- <!--定义Mapper映射文件的约束和基本结构-->
- <!DOCTYPE mapper
- PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
- "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
- <mapper namespace="com.zidiu.mapper.EmpExprMapper">
- <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>
-
- <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
- <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>
- </mapper>
复制代码 X层:
- <!--定义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">
- <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>
- </mapper>
复制代码
JavaWeb(SpringBoot3+vue3)开发+教学管理系统项目实战之条件分页查询
SpringBoot3+Vue3开发综合实战项目:
JavaWeb(SpringBoot3+vue3)开发+教学管理系统项目实战
|