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. macos - mac下docker如何設置代理2. java - 請問在main方法中寫成對象名.屬性()并賦值,與直接參參數賦值輸錯誤是什么原因?3. MySQL數據庫中文亂碼的原因4. 關docker hub上有些鏡像的tag被標記““This image has vulnerabilities””5. docker不顯示端口映射呢?6. docker - 各位電腦上有多少個容器啊?容器一多,自己都搞混了,咋辦呢?7. android studio總是在processes running好久8. angular.js - 關于$apply()9. docker-compose 為何找不到配置文件?10. dockerfile - 我用docker build的時候出現下邊問題 麻煩幫我看一下
