正在回答
抱歉同学,resultMap是employee的话,只是通过部门编号查询员工信息,没有查询部门。如果同学在部门类中增加List<Employee>,这里resultMap应该是department,查询部门信息,包括部门下的员工信息。
测试同学的代码,同学的sql语句有点问题,
同学使用左连接查询,如果有多个员工,将会出现多次部门信息,如:
这里建议同学使用如下sql语句:
select * from department where sn=#{sn}
通过部门编号查询部门信息,所有的部门信息都可以查询出来,包括部门下的员工信息。
祝:学习愉快~
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.4//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.san.dao.DepartmentDao">
<resultMap id="department" type="Department">
<id property="sn" column="sn" javaType="String"/>
<result property="name" column="name" javaType="String"/>
<result property="address" column="address" javaType="String"/>
<collection property="employees" column="sn"
select="com.san.dao.EmployeeDao.select"/>//一对多
</resultMap>
<insert id="insert" parameterType="Department">
insert into department (sn, name, address) values (#{sn},#{name},#{address});
</insert>
<update id="update" parameterType="Department">
update department set name =#{name},address=#{address} where sn=#{sn} ;
</update>
<delete id="delete" parameterType="String">
delete from department where sn=#{sn}
</delete>
<select id="select" parameterType="String" resultMap="department">
select d.*,e.sn,e.name,e.post from department d left join employee e on e.department_sn=d.sn
where d.sn=#{sn};
</select>
<select id="selectAll" resultMap="department">
select * from department;
</select>
</mapper>
部门实体类增加了:List<Employee> employees;
- 参与学习 人
- 提交作业 323 份
- 解答问题 8263 个
本阶段将带你学习主流框架SSM,以及SpringBoot ,打通成为Java工程师的最后一公里!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星