JdbcTemplate 映射器空值

JdbcTemplate mapper null value

POJO 与 Lombak:

@NoArgsConstructor
@AllArgsConstructor
@Getter @Setter
public class Order {
    private int Id;
    private int OrderId;
    private String Status;
}

使用JdbcTemplate我select记录:

String sql = "SELECT Id, OrderId, Stataus" +
                "ORDER BY t.Sort";
        return jdbcTemplate.query(sql, new Object[]{IDU}, new BeanPropertyRowMapper<>(Order .class));

由于部分 OrderId 是 null,我得到这个错误:

Failed to convert property value of type 'null' to required type 'int' for property 'OrderId';

我可以不使用 custom mapper 解决这个问题吗?

只需将其提取到 Integer 而不是 int

int是Java中的原语,不能是null

BeanPropertyRowMapper 无法将 null 转换为 int,因此抛出异常。

要解决此问题,您可以:

但请记住

if you use the values from the generated bean to update the database the primitive value will have been set to the primitive's default value instead of null.