代码编织梦想

需要修改collection写法,将collection由一次查询,改为两次查询

例:一位用户有多个地块,一个地块由多个坐标点(经纬度)组成,查询当前用户下的所有土地信息及坐标点

Entity:

FarmerPlotDTO.java

@ApiModelProperty(value = "地块id")
private Long id;

@ApiModelProperty(value = "面积")
private BigDecimal area;

@ApiModelProperty(value = "周长")
private BigDecimal perimeter;

@ApiModelProperty(value = "坐标点")
private List<FarmerPlotPointDTO> positions;
FarmerPlotPointDTO.java

@ApiModelProperty(value = "经度")
private BigDecimal longitude;

@ApiModelProperty(value = "纬度")
private BigDecimal latitude;

修改前一次查询:

<resultMap id="FarmerPlotMap" type="FarmerPlotDTO">
    <id column="id" property="id" />
    <result column="area" property="area" />
    <result column="perimeter" property="perimeter" />
    <collection property="positions" ofType="FarmerPlotPointDTO">
        <result column="latitude" property="latitude"/>
        <result column="longitude" property="longitude"/>
    </collection>
</resultMap>

修改后两次查询:

collection增加select和column属性

说明:column中的值来自第一次查询,作为第二次查询的入参

<resultMap id="FarmerPlotMap" FarmerPlotDTO">
    <id column="id" property="id" />
    <result column="area" property="area" />
    <result column="perimeter" property="perimeter" />
    <collection property="positions" ofType="FarmerPlotPointDTO"
                select="getPointById" column="id">
        <result column="latitude" property="latitude"/>
        <result column="longitude" property="longitude"/>
    </collection>
</resultMap>
<select id="getPointById" resultType="FarmerPlotPointDTO">
    SELECT longitude, latitude FROM farmer_plot_point WHERE plot_id = #{id}
</select>

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_42015412/article/details/130840852