今天用mybatis写了个查询代码,发现怎么样都报错,提示: uncategorized SQLException for SQL []; SQL state [null]; error code [0]; sql injection violation, syntax error: EOF : select * from\n \n\t\tinbound_detail\n\t \n where id in; nested exception is java.sql.SQLException: sql injection violation, syntax error: EOF : select * from\n \n\t\tinbound_detail\n\t \n where id in"
我的xml如下:
<select id="queryByIdList" resultMap="inboundDetailMap">
select * from
<include refid="TABLE_NAME"/>
where id in
<foreach collection="idList" item="item" index="index" open="(" separator="," close=")">
#{item}
</foreach>
</select>
java代码如下:
List<InboundDetailUniqueCodeDO> uniqueCodeDOList = inboundDetailUniqueCodeMapper.queryByUniqueCodeList(uniqueCodeList);
Map<Long,InboundDetailDO> inboundDetailIdMap = inboundDetailMapper.queryByIdList(uniqueCodeDOList.stream().map(InboundDetailUniqueCodeDO::getInboundDetailId).collect(Collectors.toList())
).stream().collect(Collectors.toMap(InboundDetailDO::getId,inboundDetailDO -> inboundDetailDO));
1
2
3
后面终于发现是因为当 uniqueCodeDOList结果为空的时候,会导致后面的in里面的条件为空导致有问题,改为下面这样就没问题了
List<InboundDetailUniqueCodeDO> uniqueCodeDOList = inboundDetailUniqueCodeMapper.queryByUniqueCodeList(uniqueCodeList);
Map<Long,InboundDetailDO> inboundDetailIdMap = MapUtils.EMPTY_MAP;
if(CollectionUtils.isNotEmpty(uniqueCodeDOList)){
inboundDetailIdMap = inboundDetailMapper.queryByIdList(uniqueCodeDOList.stream().map(InboundDetailUniqueCodeDO::getInboundDetailId).collect(Collectors.toList())).stream().collect(Collectors.toMap(InboundDetailDO::getId,inboundDetailDO -> inboundDetailDO));
}