我的 MySQL 表中有 3 列,2 列是 INT,1 列是 VARCHAR。
我收到类转换异常:java.lang.ClassCastException:java.lang.Long 无法转换为 java.lang.Integer。这是我的代码:
List<Map<String, Object>> rows = getJdbcTemplate().queryForList(sql);
for (Map row : rows) {
Customer customer = new Customer();
// throws ClassCastException
// customer.setCustId((Integer)(row.get("CUST_ID")));
customer.setCustId((Long)(row.get("CUST_ID"))); // had to change custId field in bean to long
customer.setName((String)row.get("NAME"));
// work around
customer.setAge(((Long)row.get("AGE")).intValue());
customers.add(customer);
}
我的问题是为什么我必须将它转换为 Long?是不是因为 MySQL 允许的最大 INT 值和 java 允许的最大 int 值不一样?