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>

解决办法

  1. 首先检查自己的mapper.xml文件中是否存在注释?xml文件中的注释不能是 /**/,要不然就会报出上面的错误信息,只能以<!开头,和 > 结尾
  2. 其次就是检查自己的sql语句是否写的有问题或者映射的实体类属性是否与sql查询的字段一致

结语

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

  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值