在MyBatis中,当使用多个集合映射的时候,如果集合映射中的id属性与父对象的id属性相同,会导致数据错乱的问题。解决这个问题的方法是使用关联查询(association)来映射集合对象,而不是使用集合映射(collection)。

首先,需要将父对象的id属性和集合映射中的id属性改为不同的名字,以避免重复。例如,可以将父对象的id属性名改为parentID,集合映射中的id属性名改为childID。

然后,使用关联查询(association)来映射集合对象。在resultMap中,使用association标签来映射集合对象的属性,指定关联查询的select语句和对应的结果映射。在集合对象的select语句中,使用where条件来关联父对象和集合对象。例如,可以使用父对象的parentID和集合对象的childID进行关联。

示例代码如下:

<resultMap id="BaseResultMap" type="org.jeecg.modules.odsBatchService.vo.OdsBatchPage">
    <result column="Material" property="material" jdbcType="VARCHAR"/>
    <result column="ManufactureDate" property="manufactureDate" jdbcType="DATE"/>
    <result column="Batch" property="batch" jdbcType="VARCHAR"/>
    <result column="BatchExtWhseMgmtInternalId" property="batchExtWhseMgmtInternalId" jdbcType="VARCHAR"/>
    <result column="ClassInternalID" property="ClassInternalID" jdbcType="VARCHAR"/>
    <collection property="odsBatchCharcList" ofType="org.jeecg.modules.odsBatchService.entity.OdsBatchCharc" javaType="list">
        <id column="CharcInternalID" property="charcInternalID" jdbcType="VARCHAR"/>
        <result column="CharcValueDependency" property="CharcValueDependency" jdbcType="VARCHAR"/>
        <result column="CharcFromDecimalValue" property="CharcFromDecimalValue" jdbcType="VARCHAR"/>
    </collection>
    <collection property="odsBatchPlantList" ofType="org.jeecg.modules.odsBatchService.entity.OdsBatchPlant"  javaType="list">
        <id column="Plant" property="plant" />
    </collection>
</resultMap>

注意,上述示例代码中,将集合映射的id属性改为CharcInternalID,而不是Material和Batch。这样就可以避免与父对象的id属性重复。

使用关联查询(association)来映射集合对象可以避免id属性重复导致的数据错乱问题

resultMap id=BaseResultMap type=orgjeecgmodulesodsBatchServicevoOdsBatchPage result column=Material property=material jdbcType=VARCHAR result column=ManufactureDate property=manufactureD

原文地址: http://www.cveoy.top/t/topic/iHuW 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录