OA system sixteen: leave function two: write Dao (persistence layer) code to achieve interaction with the underlying database; (this blog only writes new features in Dao)

The main purpose of this blog is to create the underlying Dao persistence work related to the "leave process" [LeaveForm leave form, ProcessFlow approval task process table, Notice message notification table] three tables; here is just a new addition The functions (such as query, modification and other functions are not written). When developing specific services later, it is necessary to repeatedly call the Dao layer code written in this blog;
This part is very long and has very little content; the following points need to be noted:
(1) Every time a Dao function module is written, timely unit testing is very important! ! ! This step is very important, step by step; otherwise, in the end, the front desk to the backend have been written and then tested, because the battle line is long, it will be more difficult to locate once an error occurs;
(2) Skills to quickly generate SQL statements with Navicat;
(3) The content of this blog is very long, but there are not many real difficulties; fast browsing is the best;

table of Contents

1. Create a basic entity class (nothing in this part, just a quick glance)

1.LeaveForm class: corresponding to the adm_leave_form table

2. ProcessFlow class: corresponding to the adm_process_flow table

3.Notice class: corresponding to the sys_notice table

2. Writing Dao: including Dao interface and corresponding Mapper XML

1. The Dao interface and Mapper XML related to the adm_leave_form table

(1) LeaveFormDao interface

(2) The corresponding implementation of the LeaveFormDao interface: leave_form.xml

(3) Test interface: Check whether the writing in this part is OK (this step is best done, because writing Dao and XML above is easy to cause minor problems and requires timely unit testing!!!)

2. The Dao interface and Mapper XML related to the adm_process_flow table

(1) ProcessFlowDao interface

(2) Implementation of ProcessFlowDao interface: process_flow.xml

(3) Test interface: see if this part is OK; (this step is strongly recommended to be done!!!)

3. Dao interface and Mapper XML related to sys_notice table

(1) NoticeDao interface

(2) The corresponding implementation of the Notice interface: notice.xml

(3) Test interface: see if this part is OK; (this step is strongly recommended to be done!!!)


1. Create a basic entity class (nothing in this part, just a quick glance)

1.LeaveForm class: corresponding to the adm_leave_form table

LeaveForm class:

package com.imooc.oa.entity; import java.util.Date; /** * 实体类,与adm_leave_form表对应 */public class LeaveForm {    private Long formId;    private Long employeeId;    private Integer formType;    private Date startTime;    private Date endTime;    private String reason;    private Date createTime;    private String state;     public Long getFormId() {        return formId;    }     public void setFormId(Long formId) {        this.formId = formId;    }     public Long getEmployeeId() {        return employeeId;    }     public void setEmployeeId(Long employeeId) {        this.employeeId = employeeId;    }     public Integer getFormType() {        return formType;    }     public void setFormType(Integer formType) {        this.formType = formType;    }     public Date getStartTime() {        return startTime;    }     public void setStartTime(Date startTime) {        this.startTime = startTime;    }     public Date getEndTime() {        return endTime;    }     public void setEndTime(Date endTime) {        this.endTime = endTime;    }     public String getReason() {        return reason;    }     public void setReason(String reason) {        this.reason = reason;    }     public Date getCreateTime() {        return createTime;    }     public void setCreateTime(Date createTime) {        this.createTime = createTime;    }     public String getState() {        return state;    }     public void setState(String state) {        this.state = state;    }}
Description:
(1) There is nothing to say. When writing attributes, compare the column names of the adm_leave_form table, and do not make any mistakes.

………………………………………………

2. ProcessFlow class: corresponding to the adm_process_flow table

ProcessFlow class:

package com.imooc.oa.entity; import java.util.Date; /** * 实体类,与adm_process_form表对应 */public class ProcessFlow {    private Long processId;    private Long formId;    private Long operatorId;    private String action;    private String result;    private String reason;    private Date createTime;    private Date auditTime;    private Integer orderNo;    private String state;    private Integer isLast;     public Long getProcessId() {        return processId;    }     public void setProcessId(Long processId) {        this.processId = processId;    }     public Long getFormId() {        return formId;    }     public void setFormId(Long formId) {        this.formId = formId;    }     public Long getOperatorId() {        return operatorId;    }     public void setOperatorId(Long operatorId) {        this.operatorId = operatorId;    }     public String getAction() {        return action;    }     public void setAction(String action) {        this.action = action;    }     public String getResult() {        return result;    }     public void setResult(String result) {        this.result = result;    }     public String getReason() {        return reason;    }     public void setReason(String reason) {        this.reason = reason;    }     public Date getCreateTime() {        return createTime;    }     public void setCreateTime(Date createTime) {        this.createTime = createTime;    }     public Date getAuditTime() {        return auditTime;    }     public void setAuditTime(Date auditTime) {        this.auditTime = auditTime;    }     public Integer getOrderNo() {        return orderNo;    }     public void setOrderNo(Integer orderNo) {        this.orderNo = orderNo;    }     public String getState() {        return state;    }     public void setState(String state) {        this.state = state;    }     public Integer getIsLast() {        return isLast;    }     public void setIsLast(Integer isLast) {        this.isLast = isLast;    }}
Description:
(1) There is nothing to say. When writing attributes, compare the column names of the adm_process_form table, and do not make any mistakes.

………………………………………………

3.Notice class: corresponding to the sys_notice table

Notice class:

package com.imooc.oa.entity; import java.util.Date; /** * 实体类,与sys_notice表对应 */public class Notice {    private Long noticeId;    private Long receiverId;    private String content;    private Date createTime;     public Long getNoticeId() {        return noticeId;    }     public void setNoticeId(Long noticeId) {        this.noticeId = noticeId;    }     public Long getReceiverId() {        return receiverId;    }     public void setReceiverId(Long receiverId) {        this.receiverId = receiverId;    }     public String getContent() {        return content;    }     public void setContent(String content) {        this.content = content;    }     public Date getCreateTime() {        return createTime;    }     public void setCreateTime(Date createTime) {        this.createTime = createTime;    }}
Description:
(1) There is nothing to say. When writing attributes, compare the column names of the adm_process_form table, and do not make any mistakes.

2. Writing Dao: including Dao interface and corresponding Mapper XML

(1) LeaveFormDao interface

LeaveFormDao interface:

package com.imooc.oa.dao; import com.imooc.oa.entity.LeaveForm; /** * 操作adm_leave_form表的接口 */public interface LeaveFormDao {    /**     * 向adm_leave_form表中插入数据的方法声明;     * @param leaveForm     */    public void insert(LeaveForm leaveForm);}

(2) The corresponding implementation of the LeaveFormDao interface: leave_form.xml

leave_form.xml:

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mapper        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"        "http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.imooc.oa.dao.LeaveFormDao">    <insert id="insert" parameterType="com.imooc.oa.entity.LeaveForm"        useGeneratedKeys="true" keyProperty="formId" keyColumn="form_id">        INSERT INTO adm_leave_form(employee_id, form_type, start_time, end_time, reason, create_time, state)        VALUES (#{employeeId},#{formType},#{startTime},#{endTime},#{reason},#{createTime},#{state})    </insert> </mapper>
Description:
(1) A quicker way of writing:
Because there are many fields in the adm_leave_form table, manually writing insert statements in leave_form.xml will be slow and error-prone. Here is a quicker method:
The copied insert statement is:
INSERT INTO `imooc-oa`.`adm_leave_form`(`form_id`, `employee_id`, `form_type`, `start_time`, `end_time`, `reason`, `create_time`, `state`) VALUES (1, 4, 1, '2020-09-16 09:01:39', '2020-09-20 09:01:53', '回老家买房', '2020-09-14 09:02:35', 'processing');
However, there are several adjustments above:
● There are many single quotes in the insert statement above, which are unnecessary;
● The logical space of the database can not be written
● The primary key field is automatically generated, remove
● Delete the specific value part of values ​​and change it to parameter
● It is best not to add the semicolon at the end of the SQL statement
Specifically why it is not recommended to add a semicolon at the end can be an analogy reference: MyBatis advanced five: PageHelper paging plug-in; the last part, and insert syntax when inserting multiple data ( MySQL basic operation on data two: INSERT statement (INSERT statement dialect, IGNORE keyword, sub-query query the entire record and insert) ); At the same time, you will need to perform primary key backfilling. Naturally, it is best not to use semicolons here, otherwise SQL syntax errors will occur;
(2) If you can’t remember the backfill part of the primary key, you can refer to: MyBatis Introduction 10: Mybatis data insertion, modification, and deletion 2: The difference between the <selectKey> tag and the useGeneratedKeys attribute; (a relatively independent knowledge point) ;
(3) Similarly, long-winded, remember to register leave_form.xml in mybatis-config.xml:

(3) Test interface: Check whether the writing in this part is OK ( this step is best done, because writing Dao and XML above is easy to cause minor problems and requires timely unit testing!!! )

LeaveFormDaoTest:

package com.imooc.oa.dao; import com.imooc.oa.entity.LeaveForm;import com.imooc.oa.utils.MybatisUtils;import org.junit.Test; import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date; import static org.junit.Assert.*; public class LeaveFormDaoTest {     @Test    public void insert() {        MybatisUtils.executeUpdate(sqlSession -> {            LeaveFormDao leaveFormDao = sqlSession.getMapper(LeaveFormDao.class);            LeaveForm form = new LeaveForm();            form.setEmployeeId(4l); //员工编号            form.setFormType(1); //事假            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");            Date startTime = null;//起始时间            Date endTime = null;//结束时间            try {                startTime = sdf.parse("2020-03-25 08:00:00");                endTime = sdf.parse("2020-04-01 18:00:00");            } catch (ParseException e) {                e.printStackTrace();            }            form.setStartTime(startTime);            form.setEndTime(endTime);            form.setReason("回家探亲");//请假事由            form.setCreateTime(new Date());//创建时间            form.setState("processing");//当前状态            leaveFormDao.insert(form);            return null;        });    }}

After running:

………………………………………………

(1) ProcessFlowDao interface

ProcessFlowDao interface:

package com.imooc.oa.dao; import com.imooc.oa.entity.ProcessFlow; /** * 操作adm_process_flow表的相关方法; */public interface ProcessFlowDao {    /**     * 向adm_process_flow表插入数据;     * @param processFlow     */    public void insert(ProcessFlow processFlow);}

(2) Implementation of ProcessFlowDao interface: process_flow.xml

process_flow.xml:

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mapper        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"        "http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.imooc.oa.dao.ProcessFlowDao">    <insert id="insert" parameterType="com.imooc.oa.entity.ProcessFlow"        useGeneratedKeys="true" keyColumn="process_id" keyProperty="processId">        INSERT INTO adm_process_flow(form_id, operator_id, action, result, reason, create_time, audit_time, order_no, state, is_last)        VALUES (#{formId},#{operatorId},#{action},#{result},#{reason},#{createTime},#{auditTime},#{orderNo},#{state},#{isLast})    </insert></mapper>
Description:
(1) There is nothing to say, just remember to register in mybatis-config.xml;

ProcessFlowDaoTest:

package com.imooc.oa.dao; import com.imooc.oa.entity.ProcessFlow;import com.imooc.oa.utils.MybatisUtils;import org.junit.Test; import java.util.Date; import static org.junit.Assert.*; public class ProcessFlowDaoTest {     @Test    public void insert() {        MybatisUtils.executeUpdate(sqlSession -> {            ProcessFlowDao processFlowDao = sqlSession.getMapper(ProcessFlowDao.class);            ProcessFlow flow = new ProcessFlow();            flow.setFormId(3l);            flow.setOperatorId(2l);            flow.setAction("audit");            flow.setReason("approved");            flow.setReason("同意");            flow.setCreateTime(new Date());            flow.setAuditTime(new Date());            flow.setOrderNo(1);            flow.setState("ready");            flow.setIsLast(1);            processFlowDao.insert(flow);            return null;        });    }}

running result:

………………………………………………

(1) NoticeDao interface

package com.imooc.oa.dao; import com.imooc.oa.entity.Notice; /** * 操作sys_notice表的相关方法 */public interface NoticeDao {    /**     * 插入方法     * @param notice     */    public void insert(Notice notice);}

(2) The corresponding implementation of the Notice interface: notice.xml

notice.xml:

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mapper        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"        "http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.imooc.oa.dao.NoticeDao">    <insert id="insert" parameterType="com.imooc.oa.entity.Notice"        useGeneratedKeys="true" keyColumn="notice_id" keyProperty="noticeId">        INSERT INTO sys_notice(receiver_id, content, create_time)        VALUES (#{receiverId},#{content},#{createTime})    </insert></mapper>
Description:
(1) There is nothing to say, just remember to register in mybatis-config.xml;

NoticeDaoTest:

package com.imooc.oa.dao; import com.imooc.oa.entity.Notice;import com.imooc.oa.utils.MybatisUtils;import org.junit.Test; import java.util.Date; import static org.junit.Assert.*; public class NoticeDaoTest {     @Test    public void insert() {        MybatisUtils.executeUpdate(sqlSession -> {            Notice notice = new Notice();            notice.setReceiverId(2l);            notice.setCreateTime(new Date());            notice.setContent("这是一条测试的说明");            sqlSession.getMapper(NoticeDao.class).insert(notice);            return null;        });    }}

operation result: