java - Mybatis查詢數據庫時出現查詢getInt()的錯誤
問題描述
我數據庫表的id是varchar類型,而已這條數據也是項目插入到數據庫的,但是在后面查詢的時候就出現了一下錯誤。
嚴重: Servlet.service() for servlet [SpringMVC] in context with path [/cims-ssm] threw exception [Request processing failed; nested exception is org.springframework.dao.TransientDataAccessResourceException: Error attempting to get column ’id’ from result set. Cause: java.sql.SQLException: Invalid value for getInt() - ’x41D6z93’; SQL []; Invalid value for getInt() - ’x41D6z93’; nested exception is java.sql.SQLException: Invalid value for getInt() - ’x41D6z93’] with root causejava.sql.SQLException: Invalid value for getInt() - ’x41D6z93’
這是錯誤信息,以下是相關代碼
這是查詢映射代碼
我代碼在數據庫里就能查詢出來
望各位看看對了 model中 club中的id屬性是string的
問題解答
回答1:mapping的問題:
<resultMap type='com.uiyllong.cims.model.ClubType'> <id column='id' jdbcType='INTEGER' property='id' /> <!-- 省略 --></resultMap><resultMap type='com.uiyllong.cims.model.Club'> <id column='id' jdbcType='VARCHAR' property='id' /> <!-- 省略 --> <association column='club_type_id' jdbcType='INTEGER'property='clubType' javaType='com.uiyllong.cims.model.ClubType'resultMap='resultClubType' /></resultMap>
resultClub和resultClubType的id屬性對應的列名都是id,而其中一個的類型是int。
sql的問題(記得以后貼問題把代碼貼上來,貼截圖讓人家怎么拿你代碼試,手敲嗎?):
select club_t.id, ...club_type_t.id....
這個查詢結果有兩列ID。
解決辦法很簡單,把 club_type_t 列設置別名,比如添加前綴:
select club_t.id, ...club_type_t.id ct_id, club_type_t.typeName ct_type_name
利用mybatis的columnPrefix:
<association property='clubType' columnPrefix='ct_' notNullColumn='ct_id' resultMap='resultClubType' />
相關文章:
1. javascript - js 有什么優雅的辦法實現在同時打開的兩個標簽頁間相互通信?2. css3 - 在sublime text里, 如何讓emmet生成的帶前綴css屬性垂直對齊?3. mac連接阿里云docker集群,已經卡了2天了,求問?4. 想找個php大神仿個網站。5. html - CSS如何處理圖片縮放問題?6. css3 - Typecho 后臺部分表單按鈕在 Chrome 下出現靈異動畫問題,求解決7. html5和Flash對抗是什么情況?8. javascript - 一個抽獎的效果(如圖)?9. javascript - weex和node,js到底是怎樣一個關系呢?10. javascript - 求解答,koa-bodyparser獲取到的參數是空對象,為什么?????
