实体类
@Data
public class DeptVO implements Serializable {
private static final long serialVersionUID = -7763442841172048580L;
private String code;
private String name;
/**
* 一对多映射的字段
*/
private List<Integer> tid;
}
xml文件
<resultMap id="vo" type="com.lgy.entity.DeptVO">
<result property="code" column="dept_code"/>
<result property="name" column="dept_name"/>
<collection property="tid" ofType="java.lang.Integer"
javaType="java.util.List">
<result column="id"/>
</collection>
</resultMap>
<select id="getCollection" resultMap="vo">
select d.dept_code, d.dept_name, t.id
from t_dept d
LEFT JOIN t_test t
ON d.dept_code = t.username
</select>
备注
在Mybatis的collection标签中,javaType用来指定实体类属性类型,ofType用来指定实体类中集合属性中-属性的类型(以上实体类举例,ofType用来映射DeptVO 中 tid 集合属性中 的 int 属性类型)
MyBatis中collection的一对多映射配置解析
本文介绍了在MyBatis中如何进行一对多映射配置,包括实体类的设计,XML文件的编写,特别是如何使用javaType和ofType属性来指定集合属性类型。
1206

被折叠的 条评论
为什么被折叠?



