Java返回值为List<String>,resultType写什么
时间: 2024-03-19 22:44:06 浏览: 21
如果 Java 方法返回的是 List<String> 对象,那么在 MyBatis 的映射文件中,可以使用 resultType 属性来指定返回的数据类型。resultType 属性的值应该是 List 中包含的元素类型。因为 List<String> 中包含的元素类型是 String 类型,所以可以将 resultType 属性设置为 java.lang.String。示例代码如下:
```xml
<select id="getStringList" resultType="java.lang.String">
SELECT name FROM user
</select>
```
相关问题
如果方法的返回值是Map<String, Object>而不是List<Map<String, Object>> 则resultType 应该是什么类型
如果方法的返回值是Map<String, Object>,则resultType应该是java.util.Map类型。示例配置如下:
```xml
<select id="selectMap" resultType="java.util.Map">
SELECT * FROM my_table
</select>
```
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语句能够正常运行,结合代码分析一下为何会返回四个空对象?
根据你提供的代码,我注意到你在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>
```
修改后重新运行代码,应该能够正确地获取到数据库中的数据并返回非空对象。