MyBatis常见报错问题及解决方案
作者:龙谷情Sinoam
这是一个出错的代码
public interface OrderInfoManageMapper { List<GetOrderInfoManageListReq> selectAllOrder(); void modifyDelivery(int id); void removeOrder(int id); List<GetOrderInfoManageListReq> selectOrderById(@Param("id") Integer id); }
一、报错:
1 There is no getter for property named 'id' in 'class java.lang.Integer'
检查mapper.xml文件,没错,如下:
<select id="selectOrderById" parameterType="java.lang.Integer" resultMap="BaseResultMap"> SELECT DISTINCT order_info.id, user_info.user_name, order_info.order_status, order_info.pay_type, order_info.total_price, ship_address.ship_user_name, ship_address.ship_user_mobile, ship_address.ship_address, order_goods.goods_desc, order_goods.goods_sku, order_goods.goods_icon, order_goods.goods_price FROM order_info, user_info, ship_address, order_goods WHERE order_info.id = order_goods.order_id AND order_info.user_id = user_info.id AND order_info.ship_id = ship_address.id <if test="id!= null and id!= '' "> AND order_info.id = #{id} </if> </select>
检查mapper接口,如下:
public interface OrderInfoManageMapper { List<GetOrderInfoManageListReq> selectAllOrder(); void modifyDelivery(int id); void removeOrder(int id); List<GetOrderInfoManageListReq> selectOrderById(Integer id); }
看似没有问题,但是id的话,需要这么写(看最后一行):
public interface OrderInfoManageMapper { List<GetOrderInfoManageListReq> selectAllOrder(); void modifyDelivery(int id); void removeOrder(int id); List<GetOrderInfoManageListReq> selectOrderById(@Param("id") Integer id); }
二、sql没问题却报错:
java.sql.SQLException: 无效的列类型: 1111
原因是在mybatis中
SELECT C_NAME1, C_NAME2 FROM MY_TABLE WHERE 1=1 AND ID IN ({#param})
param是java传来的字符串 'id1','id2','id3'
此时就会报这个错误,因为使用in条件时不能用#,要使用$,如
SELECT C_NAME1, C_NAME2 FROM MY_TABLE WHERE 1=1 AND ID IN ({$param})
三、java想mybatis中传入‘a','b','c',放在in中
可能会这么写mybatis:
SELECT * FROM TABLE_NAME
WHERE 1=1
<if para!= null && para!="" >
AND PARS in (#{para})
</if>
java传过来的para为
String para = "'a','x','d','g'";
此时预期效果为
SELECT * FROM TABLE_NAME
WHERE 1=1
AND PARS in ('a','x','d','g')
但是判断会通过,值不会传入,会出现:
SELECT * FROM TABLE_NAME
WHERE 1=1
AND PARS in ()
将#换成$才可以。虽然#很安全
SELECT * FROM TABLE_NAME
WHERE 1=1
<if para!=null && para!="" >
AND PARS in (${para})
</if>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。