SpringBoot 2.1.0,mybatis1.3.1,做一个简单的压测时,出现了一个和压力测试无关的其他的常见的问题:
org.apache.ibatis.binding.BindingException: Parameter 'stock' not found. Available parameters are [arg1, arg0, param1, param2]
at org.apache.ibatis.binding.MapperMethod$ParamMap.get(MapperMethod.java:202) ~[mybatis-3.4.5.jar:3.4.5]
at org.apache.ibatis.reflection.wrapper.MapWrapper.get(MapWrapper.java:45) ~[mybatis-3.4.5.jar:3.4.5]
at org.apache.ibatis.reflection.MetaObject.getValue(MetaObject.java:122) ~[mybatis-3.4.5.jar:3.4.5]
dao层
@Repository
public interface ProductStockDao {
ProductStock getById(Integer id);
void updateStockById(@Param("id") Integer id,@Param("stock")Integer stock);
}
mapper.xml
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.java4all.dao.ProductStockDao">
<resultMap id="resMap" type="com.java4all.entity.ProductStock">
<id column="id" property="id" />
<result column="name" property="name" />
<result column="stock" property="stock" />
</resultMap>

<select id="getById" resultMap="resMap">
SELECT * from product_stock where id = #{id}
</select>
<update id="updateStockById">
UPDATE product_stock set stock = #{stock} where id = #{id};
</update>
</mapper>
这个update方法,一直执行报错,参数对应看着没问题。但是解决不了问题,后来改为如下,在mapper.xml中把参数用下标来获取,下标从0开始记,竟然好了。。。暂时不知道什么原因,可能是mysql,mybatis,springboot其中哪一个的特定版本的坑。

修改后:

<update id="updateStockById">
UPDATE product_stock set stock = #{arg1} where id = #{arg0};
</update>