一对多字段映射
<resultMap id="SubjectNameOTD" type="com.tj.vo.SubjectNameOTD">
<id column="subject_code" jdbcType="VARCHAR" property="subjectCode" />
<association property="subjectName" javaType="com.tj.vo.SubjectName">
<result column="dept_code" jdbcType="VARCHAR" property="deptCode" />
<result column="dept_name" jdbcType="VARCHAR" property="deptName" />
<result column="subject_code" jdbcType="VARCHAR" property="subjectCode" />
<result column="subject_name" jdbcType="VARCHAR" property="subjectName" />
</association>
<collection property="subItemsNameList" ofType="com.tj.vo.SubItemsName" column="subject_code" select="selectChildren">
</collection>
</resultMap>
主查询
<select id="selectDirectoryItemsList02" resultMap="SubjectNameOTD">
select id,dept_code,dept_name,subject_code,subject_name,subitem_code,subitem_name
from directory_listing_code
<where>
<if test="subjectName!=null and subjectName!=''">
subject_name like '%' #{subjectName} '%' or subitem_name like '%' #{subjectName} '%'
</if>
<if test="deptCode!=null and deptCode!=''">
and dept_code like '%' #{deptCode} '%'
</if>
</where>
group by subject_code
</select>
子查询
<select id="selectChildren" parameterType="java.lang.String" resultType="com.tj.vo.SubItemsName">
select subitem_code as subitemCode,subitem_name as subitemName
from directory_listing_code
where subject_code=#{subjectCode}
</select>
分为两次查询主查询和子查询,在主查询进行分页处理,再通过column传递外键关系进行子查询,就不会出现分页数据丢失情况。