在mysql中,处理关系型数据库主要通过一点一和一对多的方式,主要通过<association>标签和<collection>标签来实现。
association:关联
collection:收集
一对一
<!-- 一对一或一对多查询需要制定映射方式 resultMap -->
<resultMap id="staffAndDep" type="com.easy.bean.Staff">
<association column="dep_id" select="getStaffDep" property="dep" ></association>
</resultMap>
<select id="getStaffDep" resultType="com.easy.bean.Department">
select * from department where id=#{dep_id}
</select>
<select id="getStaffAndDep" resultMap="staffAndDep" >
select * from staff
</select>
一对多
<resultMap id="department" type="com.easy.bean.Department">
<!-- <id column="id" property="depId"></id>
<result coumn="name" property="depname"></result> -->
<result column="id" property="id"></result>
<collection column="id" select="getDepStaff" property="staffList"></collection>
</resultMap>
<select id="getDepStaff" resultType="com.easy.bean.Staff">
select * from staff where dep_id=#{id}
</select>
<select id="getDep" resultMap="department">
select * from department
</select>