一对一,和一对多 。 mybatis写法
一对多:
DepEmpInfoDao.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.icss.dao.DepEmpInfoDao">
<select id="getDepEmpByDepId" parameterType="int" resultMap="DepMap">
select * from department d INNER JOIN employee e on e.d_id =e.d_id and
e.d_id = #{id}
</select>
<resultMap type="Dep" id="DepMap">
<id column="d_id" property="d_id" />
<result column="dname" property="d_name" />
<!-- 装配一个集合 collection 申明集合中的元素类型ofType=-->
<!-- collection 一对多 注意;ofType 一定要指明这个集合里的成员类型,不然报nullPorionException-->
<collection property="employee" ofType="Emp">
<id column="e_id" property="e_id" />
<result column="empName" property="e_name" />
<result column="job" property="e_job" />
<result column="sal" property="sal" />
<!-- <result column="d_id" property="d_id" /> -->
</collection>
</resultMap>
</mapper>