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

 更新时间:2022年01月11日 15:00:32   作者:web小欣  
这篇文章主要介绍了解决Mybatis映射文件mapper.xml中的注释问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教。

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语句中的占位符?被放在字符串内;

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

相关文章

  • idea插件之mybatis log plugin控制台sql的问题

    idea插件之mybatis log plugin控制台sql的问题

    这篇文章主要介绍了idea插件之mybatis log plugin控制台sql,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-09-09
  • Mybatis Plus Wrapper查询某几列的方法实现

    Mybatis Plus Wrapper查询某几列的方法实现

    MybatisPlus中,使用Wrapper的select和notSelect方法可以精确控制查询的字段,本文就来介绍一下Mybatis Plus Wrapper查询某几列的方法实现,感兴趣的可以了解一下
    2024-10-10
  • java实现飞机大战小游戏

    java实现飞机大战小游戏

    这篇文章主要为大家详细介绍了java实现飞机大战小游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-06-06
  • SpringBoot3安全管理操作方法

    SpringBoot3安全管理操作方法

    这篇文章主要介绍了SpringBoot3安全管理,在实际开发中,最常用的是登录验证和权限体系两大功能,在登录时完成身份的验证,加载相关信息和角色权限,在访问其他系统资源时,进行权限的验证,保护系统的安全,文中有详细的操作步骤,需要的朋友可以参考下
    2023-08-08
  • 教你怎么实现java语言的在线编译

    教你怎么实现java语言的在线编译

    这篇文章主要介绍了教你怎么实现java语言的在线编译,文中有非常详细的代码示例,对正在学习java的小伙伴们有非常好的帮助,需要的朋友可以参考下
    2021-04-04
  • MybatisPlus实现分页效果并解决错误问题:cant found IPage for args

    MybatisPlus实现分页效果并解决错误问题:cant found IPage for args

    这篇文章主要介绍了MybatisPlus实现分页效果并解决错误:cant found IPage for args,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-02-02
  • Spring的Ioc模拟实现详细介绍

    Spring的Ioc模拟实现详细介绍

    这篇文章主要介绍了Spring的Ioc模拟实现详细介绍,具有一定参考价值,需要的朋友可以了解下。
    2017-11-11
  • 关于application.yml基础配置以及读取方式

    关于application.yml基础配置以及读取方式

    这篇文章主要介绍了关于application.yml基础配置以及读取方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-07-07
  • SpringBoot 使用hibernate validator校验

    SpringBoot 使用hibernate validator校验

    这篇文章主要介绍了SpringBoot 使用hibernate validator校验,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-11-11
  • 详解Struts2动态方法调用

    详解Struts2动态方法调用

    这篇文章主要介绍了详解Struts2动态方法调用,涉及调用方法的代码,具有一定参考价值,需要的朋友可以了解下。
    2017-09-09

最新评论