Mybatis错误:Parameter 'XXX' not found. Available parameters are [1, 0, param1, param2] | 我的日常分享
Mybatis错误:Parameter 'XXX' not found. Available parameters are [1, 0, param1, param2]
发表于2022-01-07更新于2022-01-07字数统计729阅读时长9分
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]
@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 publicvoidtest3(){ 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)); }