未找到参数 'status'。可用参数在 Mybatis 中为 [0, 1, param1, param2] 和 Spring

Parameter 'status' not found. Available parameters are [0, 1, param1, param2] in Mybatis and Spring

我正在使用mybatis和spring,我想比较2个日期时间然后更新数据到数据库

我的服务和映射器:

 @Override
 public void insertOrUpdateStatusOfSendSMS(BigDecimal smsId, String status) {
     smsBrandingListMapper.insertOrUpdateStatusOfSendSMS(smsId, status);
 }

这是我的代码(控制器class):

smsBrandingList = smsService.selectListSMSBranding(params);
DateTimeFormatter dtFormatter = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm");

    for(int i=0 ; i< smsBrandingList.size() ; i++) {

        System.out.println(smsBrandingList.get(i).get("scheduled"));
        System.out.println(smsBrandingList.get(i).get("smsId"));

        BigDecimal smsId = (BigDecimal) smsBrandingList.get(i).get("smsId");
        System.out.println(smsId);

        String smsSendStatusScheduled = (String) smsBrandingList.get(i).get("scheduled");
        LocalDateTime localDateTime = LocalDateTime.parse(smsSendStatusScheduled , dtFormatter) ;

        // get current time now
        LocalDateTime currentTimeNow = LocalDateTime.now();

        System.out.println(localDateTime);
        System.out.println(currentTimeNow);


        if (currentTimeNow.isEqual(localDateTime) ) {

            String status = "Sent";
            smsService.insertOrUpdateStatusOfSendSMS(smsId, status);
            logger.info("CompareTo data and Date -> {}" + SMSConstant.STATUS_SENT);
        }

        else if (currentTimeNow.isAfter(localDateTime) ) {

            String status = "Sent";

            smsService.insertOrUpdateStatusOfSendSMS(smsId, status);
            logger.info("CompareTo data and Date -> {}" + SMSConstant.STATUS_SENT);
        }

        else if (currentTimeNow.isBefore(localDateTime) ) {

            String status = "Waiting";

            smsService.insertOrUpdateStatusOfSendSMS(smsId, status);
            logger.info("CompareTo data and Date -> {}" + SMSConstant.STATUS_WAITING);
        }

        else {
            String status = "Unknown";
            logger.error("CompareTo data and Date -> {3}" + status);
            throw new IllegalStateException("CompareTo data and Date -> {3}" + SMSConstant.STATUS_UNKNOWN);
        }

这是我的查询:

<update id="insertOrUpdateStatusOfSendSMS" parameterType="Map">
 UPDATE MSC0053M
    SET STATUS = #{status}
    WHERE SMS_ID = #{smsId}
</update>

当我比较日期时间时,基于 if 的条件,它会将 status 更新为 smsId,但我 运行 并调试它时发生以下异常:

2021-08-03 18:09:37,707 ERROR [com.coway.trust.config.handler.GlobalExceptionHandler] [defaultException]ex : {}
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.binding.BindingException: Parameter 'status' not found. Available parameters are [0, 1, param1, param2]
  at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:76) ~[mybatis-spring-1.2.3.jar:1.2.3]
  at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:399) ~[mybatis-spring-1.2.3.jar:1.2.3]
  at com.myservice.SMSBrandingListServiceImpl.insertOrUpdateStatusOfSendSMS(SMSBrandingListServiceImpl.java:90) ~[classes/:?]
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_144]
  at com.myservice.SMSBrandingListServiceImpl.insertOrUpdateStatusOfSendSMS(SMSBrandingListServiceImpl.java:90) ~[classes/:?]
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_144]

我该如何解决这个问题?

你使用了sqlmap的parameterType作为Map,所以你必须用Map传递mapper方法的参数。 但你没有。

正如@planben 评论的那样,如果不将 map 作为映射器的参数传递,您应该定义 <sql />,但它太复杂了。

请尝试下面的代码。

@Override
 public void insertOrUpdateStatusOfSendSMS(BigDecimal smsId, String status) {

     Map<String, Object> mp = Maps.newHashMap();

     mp.put("smsId", smsId);
     mp.put("status", status);

     smsBrandingListMapper.insertOrUpdateStatusOfSendSMS(mp);
 }