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

目录

  • Mybatis映射文件mapper.xml的注释问题
    • 报错信息
    • 解决办法
  • mapper.xml文件中的注释
    • 注释方式
    • ‘无效的列索引’bug和解决
    • 小结一下

Mybatis映射文件mapper.xml的注释问题 从昨天夜晚9点到今天中午,一直被项目bug所困惑,中间这段时间一直未解决这个问题,也咨询很多群里大佬,也未能解决
【解决Mybatis映射文件mapper.xml中的注释问题】有的说是我代码写的有问题,如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 `d.user_id`,`su.username`,`d.dues`,`d.status`,`d.total_price`,`d.pay_time` from dues as d left join sys_user as suwhere d.user_id=su.user_id andd.user_id=#{userId}*/select d.user_id,su.username,d.dues,d.status,d.total_price,d.pay_timefrom dues dleft join sys_user suon d.user_id=su.user_id and d.user_id = #{userId} order by ${sort} ${order}order by d.user_id desclimit #{offset}, #{limit}


解决办法
首先检查自己的mapper.xml文件中是否存在注释?xml文件中的注释不能是 /**/,要不然就会报出上面的错误信息,只能以<!开头,和 > 结尾
其次就是检查自己的sql语句是否写的有问题或者映射的实体类属性是否与sql查询的字段一致
总之,项目编码一定要规范,这样才能减少找bug的时间,提高效率,上面项目运行报错就是因为这个xml注释不规范,大家还是多要注意!!! 编码不规范,自己两行泪。

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

‘无效的列索引’bug和解决
昨天在导入数据时需要对数据进行验证,在mapper文件中对表中数据进行查询,将作废sql注释时选择了Java方式,此时会报错。
SELECT COUNT(*) COUN FROM tablename AWHEREA.id=#id#andA.name=#name#/*SELECT * FROM tablename AWHEREA.id=#id# andA.name=#name#*/

在解析时由于会将参数位置解析为占位符‘?’,所以此时以下的sql会在后台解析成如下,但是传入的参数只有两个,所以这个时候会报 “Caused by: java.sql.SQLException: 无效的列索引”,因为传入的参数和占位符数量不等。
SELECT COUNT(*) COUN FROM tablename A WHERE A.id=? and A.name=?/*SELECT * FROM tablename A WHEREA.id=? andA.name=?/


小结一下
1、mapper.xml文件中注释方式为;
2、“Caused by: java.sql.SQLException: 无效的列索引”错误一般由sql语句中占位符引起:
传入参数数量不等与占位符的数量;
SQL语句中的占位符?是中文版;
SQL语句中的占位符?被放在字符串内;
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

    推荐阅读