mybatis查询resultMap之<collection> < result > <constructor>标签使用
mytatis 一对多 写法多是对象 使用 collection多事基本类型使用collection里面使用构造方法
·
1.场景一
查询的集合对象里面还有对象集合
@Data
public class DatumImageDetail implements Serializable {
private static final long serialVersionUID = -6163133844534388303L;
/**
* 图片类别名称
*/
private String dicValue;
/**
* 图片类别id
*/
private Integer dicSourceId;
private List<TmDatumResourceVo> imgList;
}
@data
public class TmDatumResourceVo implements Serializable{
private static final long serialVersionUID = -6920934492324729614L;
private Integer datumId;
private Byte sourceType;
private Integer dicSourceId;
private String sourceUrl;
private Integer sort;
private String creator;
private Integer ascriptionType;
private List<ImageChannelVo> parentChanelIds;
}
@Data
public class ImageChannelVo implements Serializable {
private static final long serialVersionUID = 2870186255736601470L;
private Integer id;
private String channelName;
}
xml:
<resultMap id="ResourceDetailResultMap" type="com.zqm.vo.datum.img.DatumImageDetail">
<!--
WARNING - @mbg.generated
-->
<result column="dic_source_id" jdbcType="INTEGER" property="dicSourceId"/>
<result column="dic_value" jdbcType="VARCHAR" property="dicValue"/>
<collection property="imgList" ofType="com.zqm.vo.datum.TmDatumResourceVo">
<id column="id" jdbcType="INTEGER" property="id"/>
<result column="datum_id" jdbcType="INTEGER" property="datumId"/>
<result column="source_type" jdbcType="TINYINT" property="sourceType"/>
<result column="dic_source_id" jdbcType="INTEGER" property="dicSourceId"/>
<result column="source_url" jdbcType="VARCHAR" property="sourceUrl"/>
<result column="sort" jdbcType="INTEGER" property="sort"/>
<result column="ascription_type" jdbcType="INTEGER" property="ascriptionType"/>
<collection property="parentChanelVos" ofType="com.zqm.vo.datum.img.ImageChannelVo">
<result column="ascription_type" jdbcType="INTEGER" property="id"/>
<result column="parent_channel_id" jdbcType="INTEGER" property="channelName"/>
</collection>
</collection>
</resultMap>
2.场景二
查询的集合对象里面还有基本类型集合
@Data
public class DatumImageDetail implements Serializable {
private static final long serialVersionUID = -6163133844534388303L;
/**
* 图片类别名称
*/
private String dicValue;
/**
* 图片类别id
*/
private Integer dicSourceId;
private List<TmDatumResourceVo> imgList;
}
@data
public class TmDatumResourceVo implements Serializable{
private static final long serialVersionUID = -6920934492324729614L;
private Integer datumId;
private Byte sourceType;
private Integer dicSourceId;
private String sourceUrl;
private Integer sort;
private String creator;
private Integer ascriptionType;
private List<Integer> parentChanelIds;
}
java代码定义:
xml写法:
<resultMap id="ResourceDetailResultMap" type="com.zqm.vo.datum.img.DatumImageDetail">
<!--
WARNING - @mbg.generated
-->
<result column="dic_source_id" jdbcType="INTEGER" property="dicSourceId"/>
<result column="dic_value" jdbcType="VARCHAR" property="dicValue"/>
<collection property="imgList" ofType="com.zqm.vo.datum.TmDatumResourceVo">
<id column="id" jdbcType="INTEGER" property="id"/>
<result column="datum_id" jdbcType="INTEGER" property="datumId"/>
<result column="source_type" jdbcType="TINYINT" property="sourceType"/>
<result column="dic_source_id" jdbcType="INTEGER" property="dicSourceId"/>
<result column="source_url" jdbcType="VARCHAR" property="sourceUrl"/>
<result column="sort" jdbcType="INTEGER" property="sort"/>
<result column="ascription_type" jdbcType="INTEGER" property="ascriptionType"/>
<collection property="parentChanelIds" ofType="java.lang.Integer" javaType="arraylist">
<!-- 这里使用构造方法注入,integer 没有属性-->
<constructor>
<arg column="parent_channel_id" />
</constructor>
</collection>
</collection>
</resultMap>
补充一个问题: 一对多查出来展示成多个一对一的结果。后面的多没有形成集合的形式
原因:没有设置主键id,要在返回结果resultMap中设置主键映射:
<id column="id" jdbcType="BIGINT" property="id" />
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐

所有评论(0)