springboot mybatis常见异常及处理方法

1.in导致的异常

 Data truncation: Truncated incorrect DOUBLE value:

异常过程:

mapper接口如下:

public int updateBatchId(@Param("batchId")String batchId,@Param("idStr")String idStr);

xml中sql如下:

    <update id="updateBatchId" parameterType="java.lang.String">
        update pdm_description_error_msg set batch_id = #{batchId},status = "1",process_num = process_num +1
        where id in (#{idStr})
    </update>

原因分析:

mybatis中in不能这样写,需要使用foreach,不然就会报如上的错误。

解决办法:(这里的idStr是以逗号分隔的,形式是这样的:1,2,3,4)

    <update id="updateBatchId" parameterType="java.lang.String">
        update pdm_description_error_msg set batch_id = #{batchId},status = "1",process_num = process_num +1
        where id in
        <foreach item="item" index="index" collection="idStr.split(',')" open="(" separator="," close=")">
            '${item}'
        </foreach>
    </update>

异常总结

之前写sql时是使用注解的方式写的,通过那种方式,在java代码中是可以直接使用以逗号分隔的字符串的,例子如下:

    public String updateBatchId(String batchId,String idStr){
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("update pdm_error_msg set batch_id = '" + batchId + "',status=1,process_num=process_num+1 where " +
                " (status=0 or status = 3) and id in ("+idStr+")");
        return stringBuilder.toString();
    }

这个是在java中直接拼接的sql可以,可以直接使用带逗号的字符串,在xml不行。

猜你喜欢

转载自www.cnblogs.com/gunduzi/p/11890761.html