Cause: org.apache.ibatis.type.TypeException: Error setting non null for parameter #1 with JdbcType null
连着好几次遇到这个错误,今天记录一下,这个错误主要是由于mybatis的配置文件引起的,这块mybatis,是一个很优秀的关联数据库的框架,但是我学的时候遇到的错误却是最多的,主要就是对于配置的映射不熟悉造成的,这个框架的底层很大程度的利用了反射机制,去映射到实体类,十分方便,但是一定要搞清楚映射关系。
<insert id="saveOrderItem" parameterType="OrderItem">
insert into book.t_order_item(`name`,`price`,`total_money` ,`count`,`order_id` ) values(#{name},#{price},#{total_money},#{count},#{order_id})
</insert>
<select id="queryOrderDetaiByOrderId" parameterType="int" resultType="OrderItem">
select * from book.t_order_item where order_id=#{orderId}
</select>
就拿上面两个例子来说,parameterType是传入的参数类型,如果写错了就会报上面的错误,由于映射出错,还有就是resultType也不要写错了,这个是返回参数的类型。如果传入参数类型很多就可以使用万能Map
Map<String ,Object> map = new HashMap<>();
map.put("username",username);
map.put("password",password);
map.put("hobby",hobby);
<select id="queryOrderDetaiByOrderId" parameterType="Map" resultType="OrderItem">
select * from book.t_order_item where username=#{username} and password=#{password} and hobby = #{hobby.ball}
</select>
这里的Map是大写的M ,如果小写也会出错,可能改参数有点麻烦,但是不用全改,只改Mapper层的参数,在service层的实现类中创建Map,把参数填入就行。map是万能的基本大多数情况都很好用。