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 - 奇怪的Symbol的問題2. angular.js - angular ng-class里面的引號問題3. javascript - Web微信聊天輸入框解決方案4. docker - 如何修改運行中容器的配置5. html - vue項目中用到了elementUI問題6. 網頁爬蟲 - 用Python3的requests庫模擬登陸Bilibili總是提示驗證碼錯誤怎么辦?7. mysql無法添加外鍵8. javascript - history.replaceState()無法改變query參數9. javascript - react 中綁定事件和阻止事件冒泡10. javascript - es6將類數組轉化成數組的問題
