JAVA实战项目笔记02

一、新增员工

1.接口设计

2.设计数据库的employee表

3.设计DTO

DTO:封装前端提交过来的数据

实体类如下:包含更多属性

java 复制代码
public class Employee implements Serializable {

    private static final long serialVersionUID = 1L;

    private Long id;

    private String username;

    private String name;

    private String password;

    private String phone;

    private String sex;

    private String idNumber;

    private Integer status;

    //@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime createTime;

    //@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime updateTime;

    private Long createUser;

    private Long updateUser;

}

4.开发

在EmployeeController.java中添加:

java 复制代码
   /**
     * 新增员工
     * @param employeeDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增员工")
    public Result save(EmployeeDTO employeeDTO) {
        log.info("新增员工:{}", employeeDTO);
        employeeService.save(employeeDTO);
        return Result.success();
    }

在EmployeeService.java中添加:

java 复制代码
   /**
     * 新增员工
     * @param employeeDTO
     */
    void save(EmployeeDTO employeeDTO);

在实现类EmployeeServicelmpl.java中添加:

java 复制代码
/**
     * 新增员工
     * @param employeeDTO
     */
    public void save(EmployeeDTO employeeDTO) {
        Employee employee = new Employee();

        //对象属性拷贝
        BeanUtils.copyProperties(employeeDTO, employee);

        //设置账号的状态,默认状态1表示正常,0表示锁定
        employee.setStatus(StatusConstant.ENABLE);

        //设置密码,默认密码为123456
        employee.setPassword(DigestUtils.md5DigestAsHex(PasswordConstant.DEFAULT_PASSWORD.getBytes()));

        //设置当前记录的创建时间和修改时间
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());

        //设置当前记录创建人id和修改人id
        //TODO 后期需要改为当前登录用户的id
        employee.setCreateUser(10L);
        employee.setUpdateUser(10L);
    }

在EmployeeMapper.java中添加:

java 复制代码
 /**
     * 插入员工数据
     * @param employee
     */
    @Insert("insert into employee (name, username, password, phone, sex, id_number, create_time, update_time, create _user, update_user, status) " +
            "values " +
            "(#{name}, #{username}, #{password}, #{phone}, #{sex}, #{idNumber}, #{createTime}, #{updateTime}, #{createUser}, #{updateUser}, #{status})")

            void insert(Employee employee);

5.功能测试

(1)通过前后端联调测试

由于前后端并行开发,后端开发完之后,前端可能没有开发完成,因此此方法局限性大,不常用

(2)通过接口文档测试
相关推荐
清风一徐3 小时前
禅道从18.3升级到21.7.6版本
笔记
Jack___Xue3 小时前
LangChain实战快速入门笔记(六)--LangChain使用之Agent
笔记·langchain·unix
零度@3 小时前
SQL 调优全解:从 20 秒到 200 ms 的 6 步实战笔记(附脚本)
数据库·笔记·sql
im_AMBER4 小时前
Leetcode 78 识别数组中的最大异常值 | 镜像对之间最小绝对距离
笔记·学习·算法·leetcode
其美杰布-富贵-李5 小时前
HDF5文件学习笔记
数据结构·笔记·学习
d111111111d6 小时前
在STM32函数指针是什么,怎么使用还有典型应用场景。
笔记·stm32·单片机·嵌入式硬件·学习·算法
静小谢6 小时前
前后台一起部署,vite配置笔记base\build
前端·javascript·笔记
ask_baidu7 小时前
Doris笔记
android·笔记
IMPYLH8 小时前
Lua 的 IO (输入/输出)模块
开发语言·笔记·后端·lua
2301_783360138 小时前
【学习笔记】关于RNA_seq和Ribo_seq技术的对比和BAM生成
笔记·学习