解决mybatis竟然报Invalid value for getInt()的问题
带你来看看mybatis为什么报"invalid value for getint()"这个错误
背景
使用mybatis
遇到一个非常奇葩的问题,错误如下:
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'
场景
还原一下当时的情况:
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)); } } }
数据库如下:
上面是一个很简单的例子,就是根据id
选出用户的信息,运行结果如下:
user(id=1, age=2, name=3)
没有任何问题,但是我再往数据库里插入一条数据,如下:
在mappermain
类中增加一行代码,如下:
system.out.println(usermapper.selectuser(2l));
运行结果如下:
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'
……
可以看出第一条查询没有问题,第二条查询就报错了
初探
其实我的直觉告诉我,是不是因为user
类里字段顺序和sql
语句里select字段
的顺序不一致导致的,那就来试一下吧
改一下user
类里字段的顺序:
@data @builder public class user { private long id; private string name; private integer age; }
结果如下:
user(id=1, name=3, age=2)
user(id=2, name=lh, age=3)
果不其然,直觉还是很6的
或者改一下sql
语句里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); }
以我们的直觉,结果肯定也没问题,果不其然,如下:
user(id=1, age=2, name=3) user(id=2, age=3, name=lh)
再探
其实到上一步,问题已经解决了,可以继续干活了,但是搞不懂为什么,心里总觉得不踏实。
找bug
从debug
开始,从下面的入口开始:
追踪到如下:
可以看出user
这个类是有构造函数的,而且是包含所有字段的构造函数
利用这个构造函数创建实例的时候,参数的顺序就是sql语句选择字段的顺序,不会根据映射关系去选择
所以就出现了类型不匹配
那我们再来看一下问什么会有一个这样的构造函数产生,直觉告诉我是@builder
这个注解
一起来看一下user
编译后的结果:
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()
方法就是利用这个构造函数来生成的。
结局
最终解决方案就是给user
类加上无参的构造函数就ok了,如下:
@builder @allargsconstructor @noargsconstructor public class user { private integer age; private string name; private long id; }
字段顺序随便放,最后再执行一下:
user(age=2, name=3, id=1) user(age=3, name=lh, id=2)
到此这篇关于mybatis竟然报"invalid value for getint()"的文章就介绍到这了,更多相关mybatis报invalid value for getint()内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
上一篇: 打糍粑要打到什么程度