项目具体介绍请看我上一期文章(doge)这里只做部分代码展示
上一期文章
部门模块的Mybatis实现:
<mapper namespace="com.gupt.test_springboot3.mapper.DepartmentMapper">
<select id="retrieveDepartments" resultMap="RMDepartment">
select department_id,department_name from department
</select>
<select id="retrieveDepartmentById" resultMap="RMDepartment">
select department_id,department_name from department where department_id = #{departmentId}
</select>
<!--表字段与实体类不匹配的映射-->
<resultMap id="RMDepartment" type="Department">
<id property="departmentId" column="department_id"></id>
<result property="departmentName" column="department_name"></result>
</resultMap>
</mapper>
员工模块的Mybatis实现:
<mapper namespace="com.gupt.test_springboot3.mapper.EmployeeMapper">
<insert id="addEmploy" parameterType="Employee">
insert into employ(department_id,last_name,email,gender,date)
values(#{departmentId},#{lastName},#{email},#{gender},#{date});
</insert>
<delete id="delEmployee" parameterType="int">
delete from employ where emp_id = #{id}
</delete>
<update id="updateEmployee" parameterType="Employee">
update employ set
department_id = #{departmentId},last_name = #{lastName},
email = #{email},gender = #{gender},date = #{date}
where emp_id = #{empId}
</update>
<select id="retrieveEmployee" resultMap="RMEmployee">
select e.emp_id,
e.department_id,
e.last_name,
e.email,
e.gender,
e.date,
d.department_id,
d.department_name
from
employ e,department d where e.department_id = d.department_id
</select>
<select id="retrieveEmployeeById" resultMap="RMEmployee">
select e.emp_id,
e.department_id,
e.last_name,
e.email,
e.gender,
e.date,
d.department_id,
d.department_name
from
employ e,department d where e.department_id = d.department_id and emp_id = #{id}
</select>
<!-- association 为一对一:一个员工对应一个部门 -->
<resultMap id="RMEmployee" type="Employee">
<id property="empId" column="emp_id"></id>
<result property="departmentId" column="department_id"></result>
<result property="lastName" column="last_name"></result>
<result property="email" column="email"></result>
<result property="date" column="date"></result>
<result property="gender" column="gender"></result>
<association property="department" javaType="Department">
<id property="departmentId" column="department_id"></id>
<result property="departmentName" column="department_name"></result>
</association>
</resultMap>
数据库设计:
📣 有源码 点击头像私信 me 点击我
📣 有源码 点击头像私信 me 点击我
📣 有源码 点击头像私信 me 点击我