JdbcTemplate BeanPropertyRowMapper问题:int/double字段值为0而非实际值
Hey there, let's figure out why your numeric fields (int/double) are always coming back as 0 or 0.0 even though your database columns and entity fields match perfectly. I’ve hit this exact snag a few times, so here are the most common fixes to check step by step:
1. Double-check your getter/setter methods
BeanPropertyRowMapper relies strictly on JavaBean naming conventions. That means for a field like int age, you need a getAge() getter and setAge(int age) setter. Even tiny typos (like getage() with lowercase 'a', or setAGE() with uppercase letters) will break the mapping—Spring won’t be able to find the methods to populate the fields.
Example of correct vs incorrect:
// Correct for field 'int age' public int getAge() { return age; } public void setAge(int age) { this.age = age; } // Incorrect (typo in method name) public int getage() { return age; } // Lowercase 'g' breaks the mapping
2. Verify field access modifiers
If your entity fields are marked private (which is best practice), make sure you have public setters for them. If you’re using package-private access (no modifier), Spring’s classloader might not be able to access the fields directly—stick with private fields + public getters/setters to avoid this.
3. Confirm your SQL query includes all required columns
It sounds obvious, but it’s easy to accidentally omit columns in your SELECT statement. If your query only fetches non-numeric fields (like SELECT name, email FROM users), then the int/double fields in your entity will default to their primitive values (0 for int, 0.0 for double). Double-check that your SQL explicitly selects every column you need to map.
4. Ensure you’re initializing BeanPropertyRowMapper correctly
Don’t forget to specify your entity class when creating the RowMapper. If you use the empty constructor (new BeanPropertyRowMapper()) without setting the target class, Spring won’t know how to map the result set to your entity, leading to default values.
Correct initialization:
// Option 1: Using generic constructor BeanPropertyRowMapper<YourEntity> rowMapper = new BeanPropertyRowMapper<>(YourEntity.class); // Option 2: Using static factory method BeanPropertyRowMapper<YourEntity> rowMapper = BeanPropertyRowMapper.newInstance(YourEntity.class);
5. Rule out subtle type mismatches
Even if you think types match, double-check:
- If your database column is
BIGINT, usingintin your entity might cause issues (uselonginstead) - If your column is
DECIMAL, ensure your entity field isdouble(orBigDecimalfor better precision) - Primitive types (
int,double) will default to 0/0.0 if the database value isNULL—if you need to handle nulls, switch to wrapper types (Integer,Double)
Example of working code
Here’s a quick example of how everything should come together:
Entity class:
public class User { private int id; private String username; private int age; private double accountBalance; // Getters and setters (follow JavaBean conventions!) public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public double getAccountBalance() { return accountBalance; } public void setAccountBalance(double accountBalance) { this.accountBalance = accountBalance; } }
JdbcTemplate call:
@Autowired private JdbcTemplate jdbcTemplate; public List<User> getUsers() { // Make sure column names here exactly match your entity fields String sql = "SELECT id, username, age, accountBalance FROM users"; return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(User.class)); }
Start with the first two checks (getters/setters and SQL query)—those are the most frequent culprits for this issue.
内容的提问来源于stack exchange,提问作者rahul shalgar

