mybatis映射字段为对象,association多层嵌套查询
有三个对象类,分别对应三个表。Project类中有个属性是客户对象属性public class Project {private int id;/** 项目名/标签名 */private String state;/** 客户对象*/private Custom custom;..省略其他字段.省略get set,必须写不然映射不进来}Custom类中有个属性是客户类型对象属性public cla
·
有三个对象类,分别对应三个表。
Project类中有个属性是客户对象属性
public class Project {
private int id;
/** 项目名/标签名 */
private String state;
/** 客户对象*/
private Custom custom;
.
.省略其他字段
.省略get set,必须写不然映射不进来
}
Custom 类中有个属性是客户类型对象属性
public class Custom {
/** id */
private int id;
/** 客户类型对象*/
private CustomType customType;
.
.省略其他字段
.省略get set,必须写不然映射不进来
}
public class CustomType extends BaseEntity {
private static final long serialVersionUID = 1L;
/** */
private Long id;
/** 客户类型编号 */
private Long customTypeCode;
/** 客户类型 */
private String customTypeName;
.
.省略其他字段
.省略get set,必须写不然映射不进来
}
<resultMap type="Project" id="ProjectResult">
<result property="id" column="proid" />
<result property="state" column="state" />
<association property="custom" column="custom_id"
javaType="Custom" resultMap="CustomResult" />
</resultMap>
<resultMap type="Custom" id="CustomResult">
<result property="id" column="id" />
<result property="customTypeId" column="custom_type_id" />
<association property="customType" column="custom_type_id"
javaType="CustomType" resultMap="CustomTypeResult" />
</resultMap>
<resultMap type="CustomType" id="CustomTypeResult">
<result property="id" column="tyid" />
<result property="customTypeCode" column="custom_type_code" />
<result property="customTypeName" column="custom_type_name" />
</resultMap>
<sql id="selectProjectVo">
select a.id proid, a.state,
a.custom_id,
cus.*,ty.id tyid,ty.custom_type_code,ty.custom_type_name
from
t_p_project a left
join
t_p_custom
cus on a.custom_id=cus.id
left join t_p_custom_type ty on cus.custom_type_id=ty.id
</sql>
<select id="selectProjectList" resultMap="ProjectResult">
<include refid="selectProjectVo" />
</select>
如果查询的字段中有相同的名字,最好查询的时候取别名区分
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐



所有评论(0)