MyBatis的JDBC select查询工作不抛出无效的列索引

问题描述 投票:0回答:1

我有在MyBatis的版本3,请帮我找到这个跌破发行。

Java代码:

@Select("SELECT A.PERSON_ID,A.PERSON_ADDRESS, C.CUSTOMER_NAME," + 
            "       B.CUSTOMER_DOB," + 
            "   FROM PERSON A, CUSTOMER B, CUSTOMER_DETAILS C" + 
            "  WHERE A.CUSTOMER_ID=C.CUSTOMER_ID" + 
            "  AND A.CUSTOMER_ID=B.CUSTOMER_ID (+)" + 
            "  AND C.PERSON_NAME='#{personName, jdbcType=VARCHAR,    mode=IN,  javaType=String}'" + 
            "  AND C.CUSTOMER_ID='#{customerID, jdbcType=VARCHAR,    mode=IN,  javaType=String}'")
    @Results(value = {
       @Result(property = "personId", column = "PERSON_ID"),
       @Result(property = "personAddress", column = "PERSON_ADDRESS"),
       @Result(property = "customerName", column = "CUSTOMER_NAME"),
       @Result(property = "customerDOB", column = "CUSTOMER_DOB")
    })
    List<PersonCustomerDetail> getPersonCustomerByID(@Param("personName") String personName,@Param("customerID") String customerID);

异常跟踪:

nested exception is org.apache.ibatis.type.TypeException: 
Could not set parameters for mapping: ParameterMapping{property='personName', mode=IN, javaType=class java.lang.String, jdbcType=VARCHAR, 
numericScale=null, resultMapId='null', jdbcTypeName='null', expression='null'}. 
Cause: org.apache.ibatis.type.TypeException: Error setting non null for parameter #1 with JdbcType VARCHAR . 
Try setting a different JdbcType for this parameter or a different configuration property. Cause: java.sql.SQLException: Invalid column index
java jdbc mybatis spring-mybatis
1个回答
1
投票

问题是与参数传递给查询。

当你使用像#{parameterName}表达式来指定一个参数的MyBatis将其转换为JDBC的参数占位符?,然后通过索引设置参数。对于此查询:

 select * from  a where col = #{param}

通过MyBatis的生成的查询将是:

 select * from a where col = ?

因为你引用这样的参数:

 select * from  a where col = '#{param}'

生成的查询变为:

 select * from  a where col = '?'

这是通过JDBC API视为不带任何参数的查询,所以当MyBatis的尝试使用JDBC API PreparedStatement的错误是,参数指标是无效的设置参数。

要解决此问题删除引号:

@Select("SELECT A.PERSON_ID,A.PERSON_ADDRESS, C.CUSTOMER_NAME," + 
    "       B.CUSTOMER_DOB," + 
    "   FROM PERSON A, CUSTOMER B, CUSTOMER_DETAILS C" + 
    "  WHERE A.CUSTOMER_ID=C.CUSTOMER_ID" + 
    "  AND A.CUSTOMER_ID=B.CUSTOMER_ID (+)" + 
    "  AND C.PERSON_NAME=#{personName, jdbcType=VARCHAR, mode=IN, javaType=String}" + 
    "  AND C.CUSTOMER_ID=#{customerID, jdbcType=VARCHAR, mode=IN, javaType=String}")
© www.soinside.com 2019 - 2024. All rights reserved.