使用MyBatis遇到的问题及解决方法(一)(持续更新)

这是一个出错的代码

1 public interface OrderInfoManageMapper {
2     List<GetOrderInfoManageListReq> selectAllOrder();
3     void modifyDelivery(int id);
4     void removeOrder(int id);
5     List<GetOrderInfoManageListReq> selectOrderById(@Param("id") Integer id);
6 }

 

一、报错:

1  There is no getter for property named 'id' in 'class java.lang.Integer'

检查mapper.xml文件,没错,如下:

 1 <select id="selectOrderById" parameterType="java.lang.Integer" resultMap="BaseResultMap">
 2         SELECT DISTINCT
 3         order_info.id,
 4         user_info.user_name,
 5         order_info.order_status,
 6         order_info.pay_type,
 7         order_info.total_price,
 8         ship_address.ship_user_name,
 9         ship_address.ship_user_mobile,
10         ship_address.ship_address,
11         order_goods.goods_desc,
12         order_goods.goods_sku,
13         order_goods.goods_icon,
14         order_goods.goods_price
15         FROM
16         order_info,
17         user_info,
18         ship_address,
19         order_goods
20         WHERE
21         order_info.id = order_goods.order_id
22         AND order_info.user_id = user_info.id
23         AND order_info.ship_id = ship_address.id
24         <if test="id!= null and id!= '' ">
25             AND order_info.id = #{id}
26         </if>
27     </select>

检查mapper接口,如下:

1 public interface OrderInfoManageMapper {
2     List<GetOrderInfoManageListReq> selectAllOrder();
3     void modifyDelivery(int id);
4     void removeOrder(int id);
5     List<GetOrderInfoManageListReq> selectOrderById(Integer id);
6 }

看似没有问题,但是id的话,需要这么写(看最后一行):

 

1 public interface OrderInfoManageMapper {
2     List<GetOrderInfoManageListReq> selectAllOrder();
3     void modifyDelivery(int id);
4     void removeOrder(int id);
5     List<GetOrderInfoManageListReq> selectOrderById(@Param("id") Integer id);
6 }

 二、sql没问题却报错:

java.sql.SQLException: 无效的列类型: 1111

原因是在mybatis中

 1 SELECT
 2 C_NAME1,
 3 C_NAME2
 4 FROM
 5 MY_TABLE
 6 WHERE 
 7 1=1
 9 AND ID IN ({#param})

param是java传来的字符串 'id1','id2','id3'

此时就会报这个错误,因为使用in条件时不能用#,要使用$,如

 1 SELECT
 2 C_NAME1,
 3 C_NAME2
 4 FROM
 5 MY_TABLE
 6 WHERE 
 7 1=1
 9 AND ID IN ({$param})

 三、java想mybatis中传入‘a’,'b','c',放在in中

可能会这么写mybatis:

1 SELECT * FROM TABLE_NAME
2 WHERE 1=1
3 <if para!= null && para!="" >
4     AND PARS in (#{para})
5 </if>

java传过来的para为

1 String para = "'a','x','d','g'";

此时预期效果为

1 SELECT * FROM TABLE_NAME
2 WHERE 1=1
3 AND PARS in ('a','x','d','g')

但是判断会通过,值不会传入,会出现:

1 SELECT * FROM TABLE_NAME
2 WHERE 1=1
3 AND PARS in ()

将#换成$才可以。虽然#很安全

1 SELECT * FROM TABLE_NAME
2 WHERE 1=1
3 <if para!=null && para!="" >
4     AND PARS in (${para})
5 </if>

 

posted @ 2019-05-15 11:14  龙谷情Sinoam  阅读(1350)  评论(0编辑  收藏  举报
Smiley face