昨天剛學了下mybatis,用的是3.2.2的版本,在使用過程中遇到了些小問題,現總結如下,會不斷更新.
1.沒有在configuration.xml配置對應的sql配置文件
錯誤:
Error updating database. Cause: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for ***Mapper.*** Cause: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for ***Mapper.***
解決方法:在configuration.xml配置文件中引用對應的sql配置文件
2.同一sql配置文件中id重複
錯誤:
Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: java.lang.RuntimeException: Error parsing Mapper XML. Cause: java.lang.IllegalArgumentException: Mapped Statements collection already contains value for GeographyMapper.getByCode
兩個sql語句的id重複了,就會報這個錯.
解決方法:修改其中的一個id就ok了.
3.mapper配置文件中為bean類配置的屬性沒有在對應的bean類中找到對應的字段
錯誤:
Error updating database. Cause: org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'education' in 'class esd.bean.Personal'
Cause: org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'education' in 'class esd.bean.Personal'
education = #{education},
解決方法:在mapper配置文件中刪除多配置的屬性/或則在自己的bean類中補全該字段
4.
錯誤:
The entity name must immediately follow the '&' in the entity reference
解析不了 && 是什麼東東
update Geography
code=#{code},
name=#{name},
where id= #{id}
解決方法:使用正確的語法咯,別用&&,用and
update Geography
code=#{code},
name=#{name},
where id= #{id}
5.返回結果類型寫錯
由於我的項目配置原因, debug 輸出信息中沒有報錯, 但是項目就是啟動不起來, 所以沒能截到錯誤信息提示, 以後遇到會補上
具體說就是: 如果自定義了返回的結果集, 返回的類型一定要是resultMap類型,(下面是錯誤示例) 如下
常用在表連接查詢中.如上例中, 我定義了'ResultParameter'的結果集, 那麼下面表連接查詢的時候,如果返回的是表連接查詢結果, 那麼一定要使用 resultMap="ResultParameter", 否則萬一提示信息中沒有顯示出來, 要費些時間才能找出來.
6. 標籤順序寫錯
錯誤:
The content of element type "resultMap" must match "(constructor?,id*,result*,association*,collection*,discriminator?)".
下午的時候無意中遇到這個錯誤, 下面是錯誤代碼:
在標籤, 一定要放在這組標籤最下面. 如下:
7.多寫了if判斷
錯誤:
org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'year' in 'class java.lang.String'
下面是代碼:
dao層方法
/**
* 查詢審核報表情況--按公司類型查
* @return
*/
ListretrieveReportByCompanyType(String year);
mapper配置文件
一般這個錯是指參數parameterType類型的字段 沒有get, set方法, 我只是傳個String類型的參數, so 剛遇到這個問題的時候, 有點摸不著頭腦, 怎麼會報這個錯呢? 原來問題出現在配置文件中:
mybatis自動將if判斷中的字段默認為傳進來的parameterType類型的字段了... so....只要將這個if判斷去掉即可.
那問題出來了, 我怎麼判斷傳進來的單個字符串是否為空值呢? 這是否是mybatis的一個小bug呢? 答案尚在查找中, 有了會第一時間貼上來的.
閱讀更多 java執行官 的文章