gpt4 book ai didi

java - 带有 BigDecimal 的 Spring JDBC queryForObject 失败

转载 作者:搜寻专家 更新时间:2023-11-01 01:54:28 24 4
gpt4 key购买 nike

Java 1.7/Spring 3.1

看看下面的代码。

BigDecimal value = queryAsObject (BigDecimal.class, 
"select balance from financial.accounts where account_id = ?", accountId);

其中 queryAsObject 来自一个抽象父类,它基本上执行 CRUD 操作。

public <T> T queryAsObject(Class<T> modelClass, String sql, Object... args) {
return jdbcTemplate.queryForObject(sql, new HawkBeanPropertyRowMapper<T>(modelClass), args);
}

非常直接的 spring jdbc 调用。但是它会导致以下异常:

org.springframework.web.util.NestedServletException: Request processing failed; 
nested exception in org.springframework.beans.BeanInstantiationException:
Could not instantiate bean class [java.math.BigDecimal]:
Is it an abstract class?; nested exception is
java.lang.InstantiationException: java.math.BigDecimal

根本原因:

org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [java.math.BigDecimal]: Is it an abstract class?; nested exception is java.lang.InstantiationException: java.math.BigDecimal
org.springframework.beans.BeanUtils.instantiate(BeanUtils.java:81)
org.springframework.jdbc.core.BeanPropertyRowMapper.mapRow(BeanPropertyRowMapper.java:233)
org.springframework.jdbc.core.RowMapperResultSetExtractor.extractData(RowMapperResultSetExtractor.java:92)
org.springframework.jdbc.core.RowMapperResultSetExtractor.extractData(RowMapperResultSetExtractor.java:1)
org.springframework.jdbc.core.JdbcTemplate$1.doInPreparedStatement(JdbcTemplate.java:649)
org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:587)
org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:637)
org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:666)
org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:674)
org.springframework.jdbc.core.JdbcTemplate.queryForObject(JdbcTemplate.java:734)

这是什么意思?

最佳答案

我认为JdbcTemplate.queryForObject(String sql, RowMapper<T> rowMapper, Object... args)用于将 ResultSet 绑定(bind)到 Beans/POJO。

您需要其他重载版本 queryForObject(String sql, Class<T> requiredType, Object... args) ,即

public <T> T queryAsScalar(Class<T> scalarClass, String sql, Object... args) {
return jdbcTemplate.queryForObject(sql, scalarClass, args);
}

关于java - 带有 BigDecimal 的 Spring JDBC queryForObject 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14023483/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com