当前位置 博文首页 > 一个搬砖的农民工的博客:mybatis中映射一对多关系
其中columnPrefix是给字段加前缀,在查出的数据id重命名为express_id加上前缀即可
<resultMap id="getDetailsResultMap" type="StudentArchivesInfoExpressDetailsDTO">
<id column="id" property="id"></id>
<result column="student_id" property="studentId"></result>
<collection property="express" columnPrefix="express_" ofType="ExpressDTO">
<id column="id" property="id"></id>
<result column="content" property="content"></result>
<result column="time" property="time"></result>
</collection>
</resultMap>
<select id="getDetails" resultMap="getDetailsResultMap">
SELECT
a.id,
a.student_id,
b.id AS express_id,
b."content" AS express_content,
b."time" AS express_time
FROM
t_student_archives_info_express AS a
LEFT JOIN t_express_message AS b
ON a.id = b.relation_id AND b.relation_type = 1
</select>
//实体类
public class StudentArchivesInfoExpressDetailsDTO{
private Long id;
private Long studentId;
private List<ExpressDTO> express;
}
cs