比如有张学生表student,一张系别表department
其中学生表里面有个DEPARTMENT的外键与department关联
查询学生的ibatIS配置如下:
<resultMap id="queryResultMap" class="com.egula.sms.student.entity.Student">
<result column="ID" property="id" jdbcType="INT" />
<result column="NAME" property="name" jdbcType="VARCHAR" />
<result column="STU_NO" property="stuNo" jdbcType="VARCHAR" />
<result column="GENDER" property="gender" jdbcType="TINYINT" />
<result column="DEPARTMENT" property="department" jdbcType="INT" /><!-- 外键关联 -->
<result column="ADDRESS" property="address" jdbcType="VARCHAR" />
<result column="REGIST_DATE" property="registDate" jdbcType="datetime" />
</resultMap>
默认查询出来DEPARTMENT字段是个int值,如果需要得到系别的名称则需要在实体类里面增加一个属性
private String deptName;然后修改配置文件,增加一行
<result column="deptName" property="deptName" jdbcType="VARCHAR"/>
查询的sql为
<select>
SELECT
t.ID
,t.NAME
,t.STU_NO
,t.GENDER
,t.DEPARTMENT
,d.DART_NAME as deptName
,t.ADDRESS
,t.REGIST_DATE
FROM student t INNER JOIN department d ON t.DEPARTMENT = d.ID
</select>
如果要做修改的时候就会报一个Check the result mapping for the 'deptName' property的错误
解决办法:再增加一个resultMap
<resultMap id="queryResultMap2" class="com.egula.sms.student.entity.Student">
<result column="ID" property="id" jdbcType="INT" />
<result column="NAME" property="name" jdbcType="VARCHAR" />
<result column="STU_NO" property="stuNo" jdbcType="VARCHAR" />
<result column="GENDER" property="gender" jdbcType="TINYINT" />
<result column="ADDRESS" property="address" jdbcType="VARCHAR" />
<result column="REGIST_DATE" property="registDate" jdbcType="datetime" />
</resultMap>