java - Mybatis查詢數(shù)據(jù)庫(kù)時(shí)出現(xiàn)查詢getInt()的錯(cuò)誤
問題描述
我數(shù)據(jù)庫(kù)表的id是varchar類型,而已這條數(shù)據(jù)也是項(xiàng)目插入到數(shù)據(jù)庫(kù)的,但是在后面查詢的時(shí)候就出現(xiàn)了一下錯(cuò)誤。
嚴(yán)重: 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’
這是錯(cuò)誤信息,以下是相關(guān)代碼
這是查詢映射代碼
我代碼在數(shù)據(jù)庫(kù)里就能查詢出來
望各位看看對(duì)了 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屬性對(duì)應(yīng)的列名都是id,而其中一個(gè)的類型是int。
sql的問題(記得以后貼問題把代碼貼上來,貼截圖讓人家怎么拿你代碼試,手敲嗎?):
select club_t.id, ...club_type_t.id....
這個(gè)查詢結(jié)果有兩列ID。
解決辦法很簡(jiǎn)單,把 club_type_t 列設(shè)置別名,比如添加前綴:
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' />
相關(guān)文章:
1. 關(guān)于nginx location配置的問題,root到底是什么2. docker鏡像push報(bào)錯(cuò)3. angular.js - angular內(nèi)容過長(zhǎng)展開收起效果4. 關(guān)于docker下的nginx壓力測(cè)試5. angular.js - angularjs的自定義過濾器如何給文字加顏色?6. python 怎樣用pickle保存類的實(shí)例?7. 大家好,請(qǐng)問在python腳本中怎么用virtualenv激活指定的環(huán)境?8. python的前景到底有大?如果不考慮數(shù)據(jù)挖掘,機(jī)器學(xué)習(xí)這塊?9. 并發(fā)模型 - python將進(jìn)程池放在裝飾器里為什么不生效也沒報(bào)錯(cuò)10. python2安裝失敗
