java - Mybatis查詢數(shù)據(jù)庫時出現(xiàn)查詢getInt()的錯誤
問題描述
我數(shù)據(jù)庫表的id是varchar類型,而已這條數(shù)據(jù)也是項目插入到數(shù)據(jù)庫的,但是在后面查詢的時候就出現(xiàn)了一下錯誤。
嚴(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’
這是錯誤信息,以下是相關(guān)代碼
這是查詢映射代碼
我代碼在數(shù)據(jù)庫里就能查詢出來
望各位看看對了 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屬性對應(yīng)的列名都是id,而其中一個的類型是int。
sql的問題(記得以后貼問題把代碼貼上來,貼截圖讓人家怎么拿你代碼試,手敲嗎?):
select club_t.id, ...club_type_t.id....
這個查詢結(jié)果有兩列ID。
解決辦法很簡單,把 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. mysql - 表名稱前綴到底有啥用?2. 致命錯誤: Class ’appfacadeTest’ not found3. 老師們php,插入數(shù)據(jù)庫mysql,都是空的,要怎么解決4. 求大神支招,php怎么操作在一個html文件的<head>標(biāo)記內(nèi)添加內(nèi)容?5. php點贊一天一次怎么實現(xiàn)6. 怎么php怎么通過數(shù)組顯示sql查詢結(jié)果呢,查詢結(jié)果有多條,如圖。7. PHP類屬性聲明?8. sql語句 - 如何在mysql中批量添加用戶?9. phpstady在win10上運行10. 在應(yīng)用配置文件 app.php 中找不到’route_check_cache’配置項
