Mybatis错误:Parameter 'XXX' not found. Available parameters are [1, 0, param1, param2] | 我的日常分享

Mybatis错误:Parameter 'XXX' not found. Available parameters are [1, 0, param1, param2]

Mybatis错误:Parameter ‘XXX’ not found. Available parameters are [1, 0, param1, param2]

一、问题回顾

在写接口的时候,在OrderRoomMapper.java中写了一个方法。

1
2
3
4
5
@Select("select a.number,a.user_id as userId,a.room_id as roomId,a.phone_number as phoneNumber,a.amount,a.check_in_person as checkInPerson,a.arrival_time as arrivalTime,a.start_time as startTime,a.end_time as endTime,a.remark,a.status,a.gmt_create, "+
"b.photo as photo,b.name as roomName,b.instruction as roomInstruction,b.price "+
"from order_room as a left join room as b on a.room_id = b.id "+
"where a.status=#{status} and a.user_id=#{userid} order by a.gmt_create desc")
List<OrderList> selectOrderListByStatus(String userid,Short status);

调试发现报错:Available parameters are [arg1, arg0, param1, param2]

1
2
3
4
5
@Test
public void test3() {
List<OrderList> list = orderRoomMapper.selectOrderListByStatus("9dc34582a4804bfba7baa537d1133bca", (short) 3);
System.out.println(new Gson().toJson(list));
}

image-20220104144714822

image-20220104144752107

二、解决方案

发现在OrderRoomMapper.java中定义的方法的参数大于一个的时候就会报如上错误。

原来这与Mybatis的参数匹配机制有关,当传递多个参数的时候,映射机制并不清楚如何匹配到正确的参数。

  • 方案1:使用@Param()修饰

修改后代码:

1
2
3
4
5
@Select("select a.number,a.user_id as userId,a.room_id as roomId,a.phone_number as phoneNumber,a.amount,a.check_in_person as checkInPerson,a.arrival_time as arrivalTime,a.start_time as startTime,a.end_time as endTime,a.remark,a.status,a.gmt_create, "+
"b.photo as photo,b.name as roomName,b.instruction as roomInstruction,b.price "+
"from order_room as a left join room as b on a.room_id = b.id "+
"where a.status=#{status} and a.user_id=#{userid} order by a.gmt_create desc")
List<OrderList> selectOrderListByStatus(@Param("userid")String userid,@Param("status")Short status);
  • 方案二:指定参数位置

注意这里使用的mybatis的版本号

在MyBatis3.4.4版不能直接使用#{0}要使用#{arg0}#{param0},否则会报错Parameter '0' not found. Available parameters are [arg1, arg0, param1, param2]

修改后代码:

1
2
3
4
5
@Select("select a.number,a.user_id as userId,a.room_id as roomId,a.phone_number as phoneNumber,a.amount,a.check_in_person as checkInPerson,a.arrival_time as arrivalTime,a.start_time as startTime,a.end_time as endTime,a.remark,a.status,a.gmt_create, "+
"b.photo as photo,b.name as roomName,b.instruction as roomInstruction,b.price "+
"from order_room as a left join room as b on a.room_id = b.id "+
"where a.status=#{arg1} and a.user_id=#{arg0} order by a.gmt_create desc")
List<OrderList> selectOrderListByStatus(String userid,Short status);
  • 方案三:使用map方式传参

map 的键名对应sql中的字段名称,实质与@Param类似。

1
2
3
4
5
@Select("select a.number,a.user_id as userId,a.room_id as roomId,a.phone_number as phoneNumber,a.amount,a.check_in_person as checkInPerson,a.arrival_time as arrivalTime,a.start_time as startTime,a.end_time as endTime,a.remark,a.status,a.gmt_create, "+
"b.photo as photo,b.name as roomName,b.instruction as roomInstruction,b.price "+
"from order_room as a left join room as b on a.room_id = b.id "+
"where a.status=#{status} and a.user_id=#{userid} order by a.gmt_create desc")
List<OrderList> selectOrderListByStatus(Map<String, Object> map);
1
2
3
4
5
6
7
8
@Test
public void test3() {
Map<String, Object> map = new HashMap<String, Object>();
map.put("userid", "9dc34582a4804bfba7baa537d1133bca");
map.put("status","3");
List<OrderList> list = orderRoomMapper.selectOrderListByStatus(map);
System.out.println(new Gson().toJson(list));
}

运行结果:image-20220104145125808

image-20220104145111480