In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "how to solve the problem of mybatis newspaper Invalid value for getInt ()". In the daily operation, I believe many people have doubts about how to solve the problem of mybatis newspaper Invalid value for getInt (). The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts of "how to solve the problem of mybatis newspaper Invalid value for getInt ()"! Next, please follow the editor to study!
Background
There is a very strange problem with using mybatis. The error is as follows:
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'
Scene
To restore the situation at that time:
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@Builderpublic 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));}
The database is as follows:
The above is a very simple example, which selects the information of a user based on id. The running result is as follows:
User (id=1, age=2, name=3)
There is no problem, but I insert another piece of data into the database, as follows:
Add a line of code to the MapperMain class as follows:
System.out.println (userMapper.selectUser (2L))
The running results are as follows:
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'
……
We can see that there is no problem with the first query, but the second query is wrong.
Preliminary study
In fact, my intuition tells me whether it is because the order of the fields in the User class is inconsistent with the order of the select fields in the SQL statement. Let's give it a try.
Change the order of the fields in the User class:
@ Data@Builderpublic class User {private Long id; private String name; private Integer age;}
The results are as follows:
User (id=1, name=3, age=2)
User (id=2, name=lh, age=3)
Sure enough, the intuition is still very 6.
Or change the order of the select fields in the SQL statement:
@ Data@Builderpublic 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) }
According to our intuition, the result must be no problem, and sure enough, it is as follows:
Further Exploration of User (id=1, age=2, name=3) User (id=2, age=3, name=lh)
In fact, to the last step, the problem has been solved, can continue to work, but do not understand why, I always feel uneasy.
The search for bug starts with debug and at the following entrance:
Traced as follows:
You can see that the class User has a constructor and is a constructor that contains all fields
When you create an instance using this constructor, the order of the parameters is the order in which the fields are selected by the SQL statement, not according to the mapping relationship.
So there is a type mismatch.
Well, let's take a look at why such a constructor is generated. My intuition tells me that it is @ Builder.
Let's take a look at the compiled result of 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);}
Sure enough, the UserBuilder.build () method is generated using this constructor.
Ending
The ultimate solution is to add a no-argument constructor to the User class and OK, as follows:
@ Builder@AllArgsConstructor@NoArgsConstructorpublic class User {private Integer age; private String name; private Long id;}
Put the fields in random order, and then execute them again:
User (age=2, name=3, id=1) User (age=3, name=lh, id=2) at this point, the study on "how to solve the problem of mybatis newspaper Invalid value for getInt ()" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.