解決mybatis竟然報(bào)Invalid value for getInt()的問(wèn)題
帶你來(lái)看看mybatis為什么報(bào)"Invalid value for getInt()"這個(gè)錯(cuò)誤
背景
使用mybatis
遇到一個(gè)非常奇葩的問(wèn)題,錯(cuò)誤如下:
Cause: org.apache.ibatis.executor.result.ResultMapException: Error attempting to get column 'name' from result set. Cause: java.sql.SQLException: Invalid value for getInt() - 'wo'
場(chǎng)景
還原一下當(dāng)時(shí)的情況:
public interface UserMapper { @Results(value = { @Result(property = "id", column = "id", javaType = Long.class, jdbcType = JdbcType.BIGINT), @Result(property = "age", column = "age", javaType = Integer.class, jdbcType = JdbcType.INTEGER), @Result(property = "name", column = "name", javaType = String.class, jdbcType = JdbcType.VARCHAR) }) @Select("SELECT id, name, age FROM user WHERE id = #{id}") User selectUser(Long id); } @Data @Builder public class User { private Long id; private Integer age; private String name; } public class MapperMain { public static void main(String[] args) throws Exception { MysqlConnectionPoolDataSource dataSource = new MysqlConnectionPoolDataSource(); dataSource.setUser("root"); dataSource.setPassword("root"); dataSource.setUrl("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8"); TransactionFactory transactionFactory = new JdbcTransactionFactory(); Environment environment = new Environment("development", transactionFactory, dataSource); Configuration configuration = new Configuration(environment); configuration.addMapper(UserMapper.class); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration); try (SqlSession session = sqlSessionFactory.openSession()) { UserMapper userMapper = session.getMapper(UserMapper.class); System.out.println(userMapper.selectUser(1L)); } } }
數(shù)據(jù)庫(kù)如下:
上面是一個(gè)很簡(jiǎn)單的例子,就是根據(jù)id
選出用戶的信息,運(yùn)行結(jié)果如下:
User(id=1, age=2, name=3)
沒有任何問(wèn)題,但是我再往數(shù)據(jù)庫(kù)里插入一條數(shù)據(jù),如下:
在MapperMain
類中增加一行代碼,如下:
System.out.println(userMapper.selectUser(2L));
運(yùn)行結(jié)果如下:
User(id=1, age=2, name=3)
### Error querying database. Cause: org.apache.ibatis.executor.result.ResultMapException: Error attempting to get column 'name' from result set. Cause: java.sql.SQLException: Invalid value for getInt() - 'lh'
……
可以看出第一條查詢沒有問(wèn)題,第二條查詢就報(bào)錯(cuò)了
初探
其實(shí)我的直覺告訴我,是不是因?yàn)?code>User類里字段順序和SQL
語(yǔ)句里select字段
的順序不一致導(dǎo)致的,那就來(lái)試一下吧
改一下User
類里字段的順序:
@Data @Builder public class User { private Long id; private String name; private Integer age; }
結(jié)果如下:
User(id=1, name=3, age=2)
User(id=2, name=lh, age=3)
果不其然,直覺還是很6的
或者改一下SQL
語(yǔ)句里select字段
的順序:
@Data @Builder public class User { private Long id; private Integer age; private String name; } public interface UserMapper { @Results(value = { @Result(property = "id", column = "id", javaType = Long.class, jdbcType = JdbcType.BIGINT), @Result(property = "age", column = "age", javaType = Integer.class, jdbcType = JdbcType.INTEGER), @Result(property = "name", column = "name", javaType = String.class, jdbcType = JdbcType.VARCHAR) }) @Select("SELECT id, age, name FROM user WHERE id = #{id}") User selectUser(Long id); }
以我們的直覺,結(jié)果肯定也沒問(wèn)題,果不其然,如下:
User(id=1, age=2, name=3) User(id=2, age=3, name=lh)
再探
其實(shí)到上一步,問(wèn)題已經(jīng)解決了,可以繼續(xù)干活了,但是搞不懂為什么,心里總覺得不踏實(shí)。
找bug
從debug
開始,從下面的入口開始:
追蹤到如下:
可以看出User
這個(gè)類是有構(gòu)造函數(shù)的,而且是包含所有字段的構(gòu)造函數(shù)
利用這個(gè)構(gòu)造函數(shù)創(chuàng)建實(shí)例的時(shí)候,參數(shù)的順序就是SQL語(yǔ)句選擇字段的順序,不會(huì)根據(jù)映射關(guān)系去選擇
所以就出現(xiàn)了類型不匹配
那我們?cè)賮?lái)看一下問(wèn)什么會(huì)有一個(gè)這樣的構(gòu)造函數(shù)產(chǎn)生,直覺告訴我是@Builder
這個(gè)注解
一起來(lái)看一下User
編譯后的結(jié)果:
public class User { private Long id; private String name; private Integer age; User(final Long id, final String name, final Integer age) { this.id = id; this.name = name; this.age = age; } public static User.UserBuilder builder() { return new User.UserBuilder(); } public static class UserBuilder { private Long id; private String name; private Integer age; UserBuilder() { } public User.UserBuilder id(final Long id) { this.id = id; return this; } public User.UserBuilder name(final String name) { this.name = name; return this; } public User.UserBuilder age(final Integer age) { this.age = age; return this; } public User build() { return new User(this.id, this.name, this.age); } } }
果然如此,UserBuilder.build()
方法就是利用這個(gè)構(gòu)造函數(shù)來(lái)生成的。
結(jié)局
最終解決方案就是給User
類加上無(wú)參的構(gòu)造函數(shù)就OK了,如下:
@Builder @AllArgsConstructor @NoArgsConstructor public class User { private Integer age; private String name; private Long id; }
字段順序隨便放,最后再執(zhí)行一下:
User(age=2, name=3, id=1) User(age=3, name=lh, id=2)
到此這篇關(guān)于mybatis竟然報(bào)"Invalid value for getInt()"的文章就介紹到這了,更多相關(guān)mybatis報(bào)Invalid value for getInt()內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
一文徹底弄懂零拷貝原理以及java實(shí)現(xiàn)
零拷貝(英語(yǔ): Zero-copy) 技術(shù)是指計(jì)算機(jī)執(zhí)行操作時(shí),CPU不需要先將數(shù)據(jù)從某處內(nèi)存復(fù)制到另一個(gè)特定區(qū)域,下面這篇文章主要給大家介紹了關(guān)于零拷貝原理以及java實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下2021-08-08Java基于JavaMail實(shí)現(xiàn)向QQ郵箱發(fā)送郵件
這篇文章主要為大家詳細(xì)介紹了Java基于JavaMail實(shí)現(xiàn)向QQ郵箱發(fā)送郵件的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-01-01Java虛擬機(jī)內(nèi)存溢出與內(nèi)存泄漏
這篇文章主要介紹了Java虛擬機(jī)內(nèi)存溢出與內(nèi)存泄漏,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04Java?ThreadPoolExecutor線程池有關(guān)介紹
這篇文章主要介紹了Java?ThreadPoolExecutor線程池有關(guān)介紹,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-09-09Intellij IDEA Debug調(diào)試技巧(小結(jié))
這篇文章主要介紹了Intellij IDEA Debug調(diào)試技巧(小結(jié)),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10java提取字符串中數(shù)字string以及獲取字符串中的整數(shù)或小數(shù)
這篇文章主要給大家介紹了關(guān)于java提取字符串中數(shù)字string以及獲取字符串中的整數(shù)或小數(shù)的相關(guān)資料,需要的朋友可以參考下2023-08-08Java實(shí)現(xiàn)月餅的制作、下單和售賣功能
這篇文章主要介紹了Java實(shí)現(xiàn)月餅的制作、下單和售賣,借此機(jī)會(huì),我們用Lambda實(shí)現(xiàn)一遍月餅制作,下單,售賣的開發(fā)設(shè)計(jì)模式,主要有制作月餅的工廠模式,結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-09-09