Mybatis映射文件mapper.xml注释问题

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Mybatis映射文件mapper.xml注释问题

web小欣   2022-05-23 我要评论

Mybatis映射文件mapper.xml的注释问题

从昨天夜晚9点到今天中午,一直被项目bug所困惑,中间这段时间一直未解决这个问题,也咨询很多群里大佬,也未能解决

有的说是我代码写的有问题,如mapper文件中没有写入参数类型parameterType,也有说是我项目结构目录构建出错,按照他们的建议进行修正,也是未尽人意,启动项目运行始终报出同一个错误,现在问题解决了,感觉有必要记录这个很不经意的问题,造成这个bug的问题根本原因还是自己编码不规范造成。

报错信息

12:12:11 [http-nio-8081-exec-8] ERROR w.g.z.c.exception.BDExceptionHandler - nested exception is org.apache.ibatis.type.TypeException: Could not set parameters for mapping: ParameterMapping{property='limit', mode=IN, javaType=int, jdbcType=null, numericScale=null, resultMapId='null', jdbcTypeName='null', expression='null'}. Cause: org.apache.ibatis.type.TypeException: Error setting non null for parameter #4 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 (4 > number of parameters, which is 3).
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.type.TypeException: Could not set parameters for mapping: ParameterMapping{property='limit', mode=IN, javaType=int, jdbcType=null, numericScale=null, resultMapId='null', jdbcTypeName='null', expression='null'}. Cause: org.apache.ibatis.type.TypeException: Error setting non null for parameter #4 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 (4 > number of parameters, which is 3).
    at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:79)
    at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:447)
    at com.sun.proxy.$Proxy104.selectList(Unknown Source)
    at org.mybatis.spring.SqlSessionTemplate.selectList(SqlSessionTemplate.java:231)
    at org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:137)
    at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:75)
    at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:59)
    at com.sun.proxy.$Proxy120.list(Unknown Source)

项目报错根本原因在下面的xml文件中

DuesMapper.xml

 <select id="list" resultType="whut.glxiang.zqly.dues.domain.DuesDO">
        /*select `d.user_id`,`su.username`,`d.dues`,`d.status`,`d.total_price`,`d.pay_time` from dues as d left join sys_user as su  where d.user_id=su.user_id and  d.user_id=#{userId}*/
        select d.user_id,
              su.username,
              d.dues,
              d.status,
              d.total_price,
              d.pay_time
        from dues d
        left join sys_user su
        on d.user_id=su.user_id
        <where>
            <if test="userId != null and userId != ''"> and d.user_id = #{userId} </if>
        </where>
        <choose>
            <when test="sort != null and sort.trim() != ''">
                order by ${sort} ${order}
            </when>
            <otherwise>
                order by d.user_id desc
            </otherwise>
        </choose>
        <if test="offset != null and limit != null">
            limit #{offset}, #{limit}
        </if>
    </select>

解决办法

首先检查自己的mapper.xml文件中是否存在注释?xml文件中的注释不能是 /**/,要不然就会报出上面的错误信息,只能以<!开头,和 > 结尾

其次就是检查自己的sql语句是否写的有问题或者映射的实体类属性是否与sql查询的字段一致

总之,项目编码一定要规范,这样才能减少找bug的时间,提高效率,上面项目运行报错就是因为这个xml注释不规范,大家还是多要注意!!! 编码不规范,自己两行泪。

mapper.xml文件中的注释

注释方式

在mapper.xml文件中,注释方式为<!--existence of query content-->,直接采用Java代码方式的注释/*existence of query content*/会报错,尤其是在SQL语句中出现这种注释方式时。

‘无效的列索引’bug和解决

昨天在导入数据时需要对数据进行验证,在mapper文件中对表中数据进行查询,将作废sql注释时选择了Java方式,此时会报错。

<select id="getSeqNameCount" parameterClass="java.util.HashMap" resultClass="java.lang.Integer">
    SELECT COUNT(*) COUN FROM tablename A
        WHERE  A.id=#id#  and  A.name=#name#
    /*SELECT * FROM tablename A
        WHERE  A.id=#id# and    A.name=#name#*/
    </select>

在解析时由于会将参数位置解析为占位符‘?’,所以此时以下的sql会在后台解析成如下,但是传入的参数只有两个,所以这个时候会报 “Caused by: java.sql.SQLException: 无效的列索引”,因为传入的参数和占位符数量不等。

SELECT COUNT(*) COUN FROM tablename A WHERE A.id=? and A.name=?
    /*SELECT * FROM tablename A WHERE  A.id=? and    A.name=?/

小结一下

1、mapper.xml文件中注释方式为<!--existence of query content-->;

2、“Caused by: java.sql.SQLException: 无效的列索引”错误一般由sql语句中占位符引起:

传入参数数量不等与占位符的数量;

SQL语句中的占位符?是中文版;

SQL语句中的占位符?被放在字符串内;

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们