Mybatis xml中配置一对一关系association&一对多关系collection

Mybatis xml中配置一对一关系association&一对多关系collection

今天在配置一对一关系映射以及一对多关系映射的时候,把collection中应该使用的ofType配置成了javaType。并且没有及时发现错误,浪费了很多时间去查找此配置问题,特此记一笔。

<resultMap id="DeviceMap" type="com.model.iot.DeviceDto">
	<id property="id" column="id"/>
	<result property="name" column="name"/>
	<result property="sn" column="sn"/>
	 <!-- 配置一对一关系 -->
	<association property="deviceType" javaType="com.model.iot.DeviceTypeDto" columnPrefix="type_">
		<id property="id" column="id"/>
		<result property="name" column="name"/>
		<!-- 嵌套属性一对多的关系 -->
		<collection property="typeExts" ofType="com.model.iot.DeviceTypeExtDto" columnPrefix="ext_">
			<id property="id" column="id"/>
			<result property="attribute" column="attribute"/>
			<result property="dataType" column="dataType"/>
			<result property="unit" column="unit"/>
		</collection>
	</association>
</resultMap>
   <select id="deviceList" resultMap="DeviceMap">
	SELECT
		d.id,
		d.NAME,
		d.sn,
		dt.id AS "type_id",
		dt.name AS "type_name",
		dte.id AS "type_ext_id",
		dte.attribute AS "type_ext_attribute",
		dte.dataType AS "type_ext_dataType",
		dte.unit AS "type_ext_unit"
	FROM
		t_device d
		LEFT JOIN t_device_type dt ON d.typeId = dt.id
		LEFT JOIN t_device_type_ext dte ON dt.id = dte.typeId
</select>
@Data
public class DeviceDto {
    private Long id;
    private String name;
    private String sn;
    private DeviceTypeDto deviceType;
}

@Data
public class DeviceTypeDto {
    private Long id;
    private String name;
    private List<DeviceTypeExtDto> typeExts;
}

@Data
public class DeviceTypeExtDto {
    private Long id;
    private String attribute;
    private String dataType;
    private String unit;
}

一对一关系中association 使用的是 javaType
::对象对应对象
一对多关系中collection 使用的是ofType
::对象对应集合中的对象

注意columnPrefix的使用方法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值