mybatis-plus使用collection一对多查询导致分页信息错误-爱代码爱编程
需要修改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>