记录一个问题,mybatis 使用association联级查询,不返回第二张表的数据
SELECT *
FROM student
LEFT JOIN score ON student.id = score.student_id;
- Score实体类 作为Studen的一个属性,利用 association,在mapper.xml中设置resultMap,代码如下
<resultMap id="BaseResultMap" type="com.jgybzx.model.Student">
<id column="id" jdbcType="VARCHAR" property="id"/>
<result column="name" jdbcType="VARCHAR" property="name"/>
<result column="sex" jdbcType="VARCHAR" property="sex"/>
<result column="department" jdbcType="VARCHAR" property="department"/>
<result column="address" jdbcType="VARCHAR" property="address"/>
<result column="birthday" jdbcType="VARCHAR" property="birthday"/>
<result column="datetime_test" jdbcType="TIMESTAMP" property="datetimeTest"/>
<result column="test" jdbcType="DATE" property="test"/>
<association property="score" javaType="com.jgybzx.model.Score">
<id column="score_id" jdbcType="VARCHAR" property="scoreId"/>
<result column="student_id" jdbcType="VARCHAR" property="studentId"/>
<result column="math" jdbcType="INTEGER" property="math"/>
<result column="chinese" jdbcType="INTEGER" property="chinese"/>
<result column="english" jdbcType="INTEGER" property="english"/>
<result column="total_score" jdbcType="INTEGER" property="totalScore"/>
</association>
</resultMap>
- 遇到的问题,返回的时候,score属性一直是null

- 经过查询,问题出现在,应该使用的是 resultMap,而不是resultType
由于使用的 MybatisX 插件生成的语句,默认生成的是resultType,而正确的应该是改为我们自己定义的resultMap
错误代码

正确代码

正确返回
