您的位置:首頁技術文章
文章詳情頁

java - Mybatis查詢數據庫時出現查詢getInt()的錯誤

瀏覽:106日期:2023-12-21 18:47:07

問題描述

我數據庫表的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’

這是錯誤信息,以下是相關代碼

java - Mybatis查詢數據庫時出現查詢getInt()的錯誤

java - Mybatis查詢數據庫時出現查詢getInt()的錯誤這是查詢映射代碼

我代碼在數據庫里就能查詢出來

java - Mybatis查詢數據庫時出現查詢getInt()的錯誤

望各位看看對了 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' />

標簽: java
相關文章:
国产综合久久一区二区三区