解決Mybatis映射文件mapper.xml中的注釋問題
Mybatis映射文件mapper.xml的注釋問題
從昨天夜晚9點(diǎn)到今天中午,一直被項(xiàng)目bug所困惑,中間這段時間一直未解決這個問題,也咨詢很多群里大佬,也未能解決
有的說是我代碼寫的有問題,如mapper文件中沒有寫入?yún)?shù)類型parameterType,也有說是我項(xiàng)目結(jié)構(gòu)目錄構(gòu)建出錯,按照他們的建議進(jìn)行修正,也是未盡人意,啟動項(xiàng)目運(yùn)行始終報出同一個錯誤,現(xiàn)在問題解決了,感覺有必要記錄這個很不經(jīng)意的問題,造成這個bug的問題根本原因還是自己編碼不規(guī)范造成。
報錯信息
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)
項(xiàng)目報錯根本原因在下面的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文件中的注釋不能是 /**/,要不然就會報出上面的錯誤信息,只能以<!開頭,和 > 結(jié)尾
其次就是檢查自己的sql語句是否寫的有問題或者映射的實(shí)體類屬性是否與sql查詢的字段一致
總之,項(xiàng)目編碼一定要規(guī)范,這樣才能減少找bug的時間,提高效率,上面項(xiàng)目運(yùn)行報錯就是因?yàn)檫@個xml注釋不規(guī)范,大家還是多要注意?。?! 編碼不規(guī)范,自己兩行淚。
mapper.xml文件中的注釋
注釋方式
在mapper.xml文件中,注釋方式為<!--existence of query content-->,直接采用Java代碼方式的注釋/*existence of query content*/會報錯,尤其是在SQL語句中出現(xiàn)這種注釋方式時。
‘無效的列索引’bug和解決
昨天在導(dǎo)入數(shù)據(jù)時需要對數(shù)據(jù)進(jìn)行驗(yàn)證,在mapper文件中對表中數(shù)據(jù)進(jìn)行查詢,將作廢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>
在解析時由于會將參數(shù)位置解析為占位符‘?’,所以此時以下的sql會在后臺解析成如下,但是傳入的參數(shù)只有兩個,所以這個時候會報 “Caused by: java.sql.SQLException: 無效的列索引”,因?yàn)閭魅氲膮?shù)和占位符數(shù)量不等。
SELECT COUNT(*) COUN FROM tablename A WHERE A.id=? and A.name=? ?? ?/*SELECT * FROM tablename A WHERE ?A.id=? and ? ?A.name=?/
小結(jié)一下
1、mapper.xml文件中注釋方式為<!--existence of query content-->;
2、“Caused by: java.sql.SQLException: 無效的列索引”錯誤一般由sql語句中占位符引起:
傳入?yún)?shù)數(shù)量不等與占位符的數(shù)量;
SQL語句中的占位符?是中文版;
SQL語句中的占位符?被放在字符串內(nèi);
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
- Mybatis映射文件詳解之mapper.xml文件
- Java?Mybatis的初始化之Mapper.xml映射文件的詳解
- mybatis中映射文件(mapper)中的使用規(guī)則
- mybatis整合spring實(shí)現(xiàn)開啟mapper.xml映射文件掃描
- myBatis的mapper映射文件之批量處理方式
- mybatis映射文件mapper.xml的具體寫法
- 解決Mybatis在IDEA中找不到mapper映射文件的問題
- Mybatis中Mapper映射文件使用詳解
- 詳解mybatis通過mapper接口加載映射文件
- MyBatis Mapper映射文件配置的實(shí)現(xiàn)
相關(guān)文章
idea插件之mybatis log plugin控制臺sql的問題
這篇文章主要介紹了idea插件之mybatis log plugin控制臺sql,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09Mybatis Plus Wrapper查詢某幾列的方法實(shí)現(xiàn)
MybatisPlus中,使用Wrapper的select和notSelect方法可以精確控制查詢的字段,本文就來介紹一下Mybatis Plus Wrapper查詢某幾列的方法實(shí)現(xiàn),感興趣的可以了解一下2024-10-10java實(shí)現(xiàn)飛機(jī)大戰(zhàn)小游戲
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)飛機(jī)大戰(zhàn)小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-06-06MybatisPlus實(shí)現(xiàn)分頁效果并解決錯誤問題:cant?found?IPage?for?args
這篇文章主要介紹了MybatisPlus實(shí)現(xiàn)分頁效果并解決錯誤:cant?found?IPage?for?args,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-02-02Spring的Ioc模擬實(shí)現(xiàn)詳細(xì)介紹
這篇文章主要介紹了Spring的Ioc模擬實(shí)現(xiàn)詳細(xì)介紹,具有一定參考價值,需要的朋友可以了解下。2017-11-11關(guān)于application.yml基礎(chǔ)配置以及讀取方式
這篇文章主要介紹了關(guān)于application.yml基礎(chǔ)配置以及讀取方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-07-07SpringBoot 使用hibernate validator校驗(yàn)
這篇文章主要介紹了SpringBoot 使用hibernate validator校驗(yàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-11-11