无法设置用于映射ParameterMapping {property,modeIN,javaTypeclass java.util.Date的参数

时间:2018-12-24 08:51:33

标签: java spring mybatis ibatis

错误消息:

  

嵌套的异常是org.apache.ibatis.type.TypeException:无法设置>映射参数:ParameterMapping {property ='signIn',mode = IN,> javaType = class java.util.Date,jdbcType = null, numericScale = null,> resultMapId ='null',jdbcTypeName ='null',expression ='null'}。原因:org.apache.ibatis.type.TypeException:使用JdbcType null为参数#4设置非null时出错。尝试为此参数或其他配置属性设置不同的JdbcType。原因:java.sql.SQLException:参数索引超出范围(4>参数数量,即3)。

xml

    <select id="selectSignRecordList" parameterType="SignRecord" resultMap="SignRecordResult">
        <include refid="selectSignRecordVo"/>
        <where>  
            <if test="id != null  and id != '' "> and id_ = #{id}</if>
             <if test="promiseId != null  and promiseId != '' "> and promise_id = #{promiseId}</if>
             <if test="userId != null  and userId != '' "> and user_id = #{userId}</if>
             <if test="signIn != null">
              and sign_in =#{signIn,jdbcType=DATE}
                 --and sign_in  = date_format(  #{signIn}  , '%Y-%m-%d' )
--                  AND date_format(sign_in,'%y%m%d') = date_format(#{signIn},'%y%m%d')
--                  <![CDATA[   and DATE_FORMAT(sign_in, '%Y-%m-%d')=  DATE_FORMAT(#{signIn}, '%Y-%m-%d')   ]]>
--                  AND date(sign_in) = date(#{signIn,jdbcType=DATE})
              </if>
             <if test="signStatus != null  and signStatus != '' "> and sign_status = #{signStatus}</if>
             <if test="createBy != null  and createBy != '' "> and create_by = #{createBy}</if>
             <if test="createTime != null "> and create_time = #{createTime}</if>
             <if test="updateBy != null  and updateBy != '' "> and update_by = #{updateBy}</if>
             <if test="updateTime != null "> and update_time = #{updateTime}</if>
             <if test="remark != null  and remark != '' "> and remark = #{remark}</if>
         </where>
    </select>

登录名是java.util.Date

当我传入参数错误

1 个答案:

答案 0 :(得分:0)

最可能的问题是--是SQL中的注释开始,但是mybatis并不知道。此行(以及所有类似的行):

--           AND date(sign_in) = date(#{signIn,jdbcType=DATE})

引起问题,因为mybatis尝试在此处的预处理语句中设置参数,但是RDBMS忽略了这一部分查询,因此该参数不存在。因此,错误消息表明参数的实际数量(数据库已知)小于客户端(mybatis)试图设置的数量。

要解决问题use xml comments而不是像这样的SQL注释:

    <select id="selectSignRecordList" parameterType="SignRecord" resultMap="SignRecordResult">
    <include refid="selectSignRecordVo"/>
    <where>  
        <if test="id != null  and id != '' "> and id_ = #{id}</if>
         <if test="promiseId != null  and promiseId != '' "> and promise_id = #{promiseId}</if>
         <if test="userId != null  and userId != '' "> and user_id = #{userId}</if>
         <if test="signIn != null">
          and sign_in =#{signIn,jdbcType=DATE}
             <!-- and sign_in  = date_format(  #{signIn}  , '%Y-%m-%d' )
              AND date_format(sign_in,'%y%m%d') = date_format(#{signIn},'%y%m%d')
              <![CDATA[   and DATE_FORMAT(sign_in, '%Y-%m-%d')=  DATE_FORMAT(#{signIn}, '%Y-%m-%d')   ]]>
              AND date(sign_in) = date(#{signIn,jdbcType=DATE})
             --> 
          </if>
         <if test="signStatus != null  and signStatus != '' "> and sign_status = #{signStatus}</if>
         <if test="createBy != null  and createBy != '' "> and create_by = #{createBy}</if>
         <if test="createTime != null "> and create_time = #{createTime}</if>
         <if test="updateBy != null  and updateBy != '' "> and update_by = #{updateBy}</if>
         <if test="updateTime != null "> and update_time = #{updateTime}</if>
         <if test="remark != null  and remark != '' "> and remark = #{remark}</if>
     </where>
</select>

在这种情况下,mybatis根本不会将注释的片段包括在SQL查询中,并且不会尝试设置参数。