vo类 public class ArealRainfallVo implements Serializable { private static final long serialVersionUID = 1L; @ApiModelProperty(value = "区域编码") private String code; @ApiModelProperty(value = "雨量总和") private BigDecimal value; } Mapper类 public interface WeightBetweenObjectAndRainfallStationMapper extends BaseMapper<WeightBetweenObjectAndRainfallStation> { List<ArealRainfallVo> ListbySumList (List<PointRainfallSumVO> list); } mapper配置类 <mapper namespace="com.ndwp.jboot.bootstrap.mapper.WeightBetweenObjectAndRainfallStationMapper"> <select id="ListbySumList" resultType="com.ndwp.jboot.bootstrap.entity.vo.ArealRainfallVo"> SELECT code, SUM(CASE <foreach collection="list" item = "item"> WHEN rainfall_station_code = '${item.getCode()}' THEN weight * ${item.getValue()} </foreach> END ) AS value FROM bootstrap.weight_between_object_and_rainfall_station GROUP BY code; </select> </mapper> 服务类 public interface IWeightBetweenObjectAndRainfallStationService extends BaseService<WeightBetweenObjectAndRainfallStation> { List<ArealRainfallVo> listAreaRainfall(Date start, Date end); } 服务实现类 @Service public class WeightBetweenObjectAndRainfallStationServiceImpl extends BaseServiceImpl<WeightBetweenObjectAndRainfallStationMapper, WeightBetweenObjectAndRainfallStation> implements IWeightBetweenObjectAndRainfallStationService { @Autowired IDataHistoricRainfallService dataHistoricRainfallService; @Override public List<ArealRainfallVo> listAreaRainfall(Date start, Date end) { List<PointRainfallSumVO> pointRainfallSumVOS = dataHistoricRainfallService.listAllPointRainfallByTime(start, end); return baseMapper.ListbySumList(pointRainfallSumVOS); } } 控制台打印的sql语句能够正常运行,结合代码分析一下为何会返回四个空对象?
时间: 2024-04-13 09:30:41 浏览: 130
java类中serialVersionUID详解.pdf
根据你提供的代码,我注意到你在mapper配置文件中使用了foreach循环来动态生成SQL语句中的条件部分。然而,你在foreach循环中使用了`${item.getCode()}`和`${item.getValue()}`来获取集合中每个元素的属性值,这是不正确的。
在MyBatis中,`${}`语法用于替换普通的字符串,它不会对参数进行预处理或转义。所以在你的SQL语句中,`${item.getCode()}`和`${item.getValue()}`会被直接替换为对应元素的`getCode()`和`getValue()`方法的返回值,而不是参数值。
为了修复这个问题,你可以改用`#{}`语法来将参数作为预处理参数传递给SQL语句。修改后的mapper配置文件如下所示:
```xml
<mapper namespace="com.ndwp.jboot.bootstrap.mapper.WeightBetweenObjectAndRainfallStationMapper">
<select id="ListbySumList" resultType="com.ndwp.jboot.bootstrap.entity.vo.ArealRainfallVo">
SELECT code, SUM(CASE
<foreach collection="list" item="item" separator=" ">
WHEN rainfall_station_code = #{item.code} THEN weight * #{item.value}
</foreach> END ) AS value
FROM bootstrap.weight_between_object_and_rainfall_station
GROUP BY code;
</select>
</mapper>
```
修改后重新运行代码,应该能够正确地获取到数据库中的数据并返回非空对象。
阅读全文