【问题标题】:Could not set parameters for mapping in Mybatis when using Springboot frameworkMybatis使用Springboot框架时无法设置映射参数
【发布时间】:2022-01-10 22:48:16
【问题描述】:

我是 Java web 的新手,我在 Stack Overflow 中尝试了很多方法来解决问题,但都失败了。你能帮帮我吗?

我代码中的接口是:

public List<Answer> selectAnswerByUser(@Param("user") User user,  @Param("id") Integer id);

AnswerUser 是我定义的两个类。

映射器是:

<select id="selectAnswerByUser" parameterType="java.util.Map" resultType="Answer">
    select * from answer where exercise_id=#{id} and user_email='#{user.email}'
</select>

其中 email 是 Class User 的一个变量。

它会抛出:

nested exception is org.apache.ibatis.type.TypeException: Could not set parameters for mapping: ParameterMapping{property='user.email', mode=IN, javaType=class java.lang.Object, jdbcType=null, numericScale=null, resultMapId='null', jdbcTypeName='null', expression='null'}. 
Cause: org.apache.ibatis.type.TypeException: Error setting non null for parameter #2 with JdbcType null . Try setting a different JdbcType for this parameter or a different configuration property. 
Cause: org.apache.ibatis.type.TypeException: Error setting non null for parameter #2 with JdbcType null . Try setting a different JdbcType for this parameter or a different configuration property. 
Cause: java.sql.SQLException: Parameter index out of range (2 > number of parameters, which is 1).

我不知道这个异常。

【问题讨论】:

  • 去掉单引号。 MyBatis 将#{} 替换为java.sql.PreparedStatement 的占位符?,但是,如果占位符用单引号括起来,驱动程序会将其识别为字符串文字。

标签: java spring-boot mybatis


【解决方案1】:

当您传递两个参数 User 和 Integer 时,您正在将 parameterType 配置为 parameterType="java.util.Map"。删除parameterType="java.util.Map",它应该基于@Param 注释“自动”映射parameterTypes。或者试试parameterType="map"

【讨论】:

    猜你喜欢
    • 2019-03-24
    • 1970-01-01
    • 1970-01-01
    • 2018-07-26
    • 2021-09-13
    • 2019-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多