标签:实体类 映射 did 查询 dept 级联 emp 员工 赋值
通过级联属性赋值解决多对一的映射关系
员工 <--> 部门
多对一:
多个员工属于同一个部门,这叫多对一关系
一对多:
一个部门有多个员工,这叫一对多关系
解决多对一的方法:
首先,在数据库中员工表和部门表是有关系的,可以通过连表查询来同时获取员工信息和部门信息,
那么员工实体类和部门实体类也是有关系的,要求员工实体类要包含一个部门对象属性
private Dept dept; 可以不设置构造器,但是要有getter和setter方法
Dept对象的赋值
数据库通过连表查询出来的是具体的部门信息,对饮的是部门实体类的属性,而不是部门实体类本身。
1.级联属性赋值
<resultMap id="getEmpAndDept_" type="com.simple.simple1.Emp">
<id property="eid" column="id"></id>
<result property="ename" column="username"></result>
<result property="password" column="password"></result>
<result property="age" column="age"></result>
<result property="sex" column="sex"></result>
<result property="email" column="email"></result>
<result property="dept.did" column="did"></result>
<result property="dept.dname" column="dname"></result>
</resultMap>
<select id="getEmpAndDept" resultMap="getEmpAndDept_">
select * from emp left join dept on dept.did = emp.id;
</select>
<result property="dept.did" column="did"></result>
<result property="dept.dname" column="dname"></result> 这两句将查询结果中的部门信息和部门实体类属性建立了映射关系
2.association标签
<resultMap id="getEmpAndDept_" type="com.simple.simple1.Emp">
<id property="eid" column="id"></id>
<result property="ename" column="username"></result>
<result property="password" column="password"></result>
<result property="age" column="age"></result>
<result property="sex" column="sex"></result>
<result property="email" column="email"></result>
<association property="dept" javaType="com.simple.simple1.Dept"></association>
</resultMap>
<select id="getEmpAndDept" resultMap="getEmpAndDept_">
select * from emp left join dept on dept.did = emp.id;
</select>
<association property="dept" javaType="com.simple.simple1.Dept">
<id property="did" column="did"></id>
<result property="dname" column="dname"></result>
</association> association标签
<result property="dept.did" column="did"></result>
<result property="dept.dname" column="dname"></result> 级联赋值
3.分步查询:
第一次查询获取到did,第二次根据did再去查询dept表,并返回一个dept对象给员工实体类的dept属性
<resultMap id="getEmpAndDept_" type="com.simple.simple1.Emp">
<id property="eid" column="id"></id>
<result property="ename" column="username"></result>
<result property="password" column="password"></result>
<result property="age" column="age"></result>
<result property="sex" column="sex"></result>
<result property="email" column="email"></result>
<association property="dept" select="com.simple.simple1.pojo.mapper.UserMapper.getDept" column="did"></association>
</resultMap>
<select id="getEmpAndDept" resultMap="getEmpAndDept_">
select * from emp;
</select>
<select id="getEmpAndDept" resultMap="getEmpAndDept_">
select * from emp;
</select>
分步查询的第一步,获取到员工信息,并利用resultMap将员工信息与Emp实体属性建立映射关系,此时还获取到了did,用于第二步,查询部门信息
<association property="dept" select="com.simple.simple1.pojo.mapper.UserMapper.getDept" column="did"></association>
通过sql的唯一表示,映射文件的namespace+id去执行第二步查询,此时的column是第二步查询的条件,查询的结果需要是一个dept实体类
Dept getDept(@Param("did") String did); 这个sql被唯一标识com.simple.simple1.pojo.mapper.UserMapper.getDept调用,did就是上一步传进来的
<select id="getDept" resultType="com.simple.simple1.Dept">
select * from dept where did = #{did}
</select> 返回值是一个dept实体类,将被赋值到员工实体类的dept属性
标签:实体类,映射,did,查询,dept,级联,emp,员工,赋值 来源: https://www.cnblogs.com/new228666/p/16417659.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。