赞
踩
如果mybatis查询返回的对象中里面还嵌套着其它对象,那么就需要结果集映射。
例如我们要从数据库表中查询ExportOperationsVo的id, operationTime, operator.userId和receiver.userId。
实体类:
public class ExportOperationsVo {
private String id;
private ExportUserVo operator; //嵌套ExportUserVo对象
private Date operationTime;
private ExportUserVo receiver; //嵌套ExportUserVo对象
}
public class ExportUserVo {
private String userId;
private String username;
private String departmentId;
private String departmentName;
}
dao层接口:
public ExportOperationsVo getExportOperationsVoById(@Param("id") String id);
XML:
<select id="getExportOperationsVoById" resultMap="ExportOperationMap"> select id, src_operator_id, des_operator_id, operation_time, from operation_table where id=#{id} </select> <resultMap id="ExportOperationMap" type="ExportOperationsVo的全限定类名"> <result column="id" property="id" /> <!--operation_time为数据库表字段,对应ExportOperationsVo的operationTime属性--> <result column="operation_time" property="operationTime" /> <association property="receiver" javaType="ExportUserVo全限定类名"> <result column="des_operator_id" property="userId" /> </association> <!--des_operator_id为数据库表字段,对应receiver的userId属性--> <association property="operator" javaType="ExportUserVo全限定类名"> <result column="src_operator_id" property="userId" /> </association> </resultMap>
注意resultMap中要按照 的顺序写,不然会报错
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。