对于加深一对一、一对多的理解
1、一对一
<!--测试用:检索employee为主,及其它两个关联表,一对一对一,员工对部门对银行卡 -->
<resultMap id="testOneToOne" type="com.hywl.entity.Employee">
<id column="empId" property="empId" />
<result column="empName" property="empName" />
<result column="password" property="password" />
<result column="age" property="age" />
<result column="status" property="status" />
<result column="sex" property="sex" />
<result column="job" property="job" />
<result column="status" property="status" />
<result column="remark" property="remark" />
<result column="depId" property="depId" />
<result column="did" property="did" />
<result column="depName" property="depName" />
<!-- property就是Employee中Account的变量名称,如写成result节点那样也可以,下面这种是以集合的形式表现出来 -->
<association property="acc"
javaType="com.hywl.entity.Account">
<id column="empId" property="empId" />
<result column="bankName" property="bankName" />
<result column="bankAccount" property="bankAccount" />
</association>
<!-- <result column="bankName" property="bankName" /> <result column="bankAccount"
property="bankAccount" /> -->
</resultMap>
<select id="testSelectAllEmpAndAccAndDep"
resultMap="testOneToOne" parameterType="Integer">
<![CDATA[
select e.*,e.depId AS did,a.bankName,a.bankAccount,d.depName from emp e
inner join dep d on e.depid=d.depid
left join account a
on e.empId=a.empId
where e.empId = #{empId}
]]>
ORDER BY empId DESC
</select>
2、一对多
<!-- <resultMap id="testOneToMany" type="com.hywl.entity.Department"> -->
<select id="testSelectAllAccAndEmp" parameterType="Integer"
resultMap="testManyToOne">
select a.*,e.* from account a
inner join emp e
on a.empId = e.empId
where a.empId = #{empId}
</select>
<resultMap type="com.hywl.entity.Account" id="testOneToMany"
autoMapping="true">
<!-- 一对多并以集合形式展现 1、sql里必须是多表联查 2、实体类里必须有此list的get方法 3、实体类里的toString方法必须有此list输出) -->
<collection property="empList" autoMapping="true"
javaType="list" ofType="com.hywl.entity.Employee"></collection>
</resultMap>
3、一对多
<!-- 做为test使用 -->
<resultMap id="testOneToMany"
type="com.hywl.entity.Department">
<id column="depId" property="depId" />
<result column="depName" property="depName" />
<!-- 一对多并以集合形式展现 1、sql里必须是多表联查 2、实体类里必须有此list的get方法 3、实体类里的toString方法必须有此list输出) -->
<!--
一对多的配置
1.type:指定集合类型,一般情况下都用list
2.property:对应depVo中的List类型的成员变量
3.ofType:指定List中元素的类型
-->
<collection javaType="list" property="empList"
ofType="com.hywl.entity.Employee">
<id column="empId" property="empId" />
<result column="empName" property="empName" />
<result column="password" property="password" />
<result column="age" property="age" />
<result column="status" property="status" />
<result column="sex" property="sex" />
<result column="job" property="job" />
<result column="status" property="status" />
<result column="remark" property="remark" />
<result column="depId" property="depId" />
</collection>
</resultMap>
<select id="testSelectAllDepAndEmp" resultMap="testOneToMany"
parameterType="Integer">
<![CDATA[
select d.depName,e.* from emp e
inner join dep d on e.depid=d.depid
where d.depId = #{depId}
]]>
ORDER BY depId DESC
</select>