Using RowMapper for LocalDate



我有一个类RowMapperFactory,它可以特别返回Employee

new Employee(
new BigInteger("7499"),
new FullName("JOHN", "ALLEN", "MARIA"),
Position.SALESMAN,
LocalDate.of(1981, 2, 20),
new BigDecimal("1600")
)

但我想创建一个通用形式。对于new BigIntegernew FullNamePositionnew BigDecimal,我做了,但对于LocalDate,如何做。LocalDate必须采用hired列中的日期。我试着LocalDate.from((TemporalAccessor) resultSet.getDate("hired")),但写入该hired column not found。如何解决?

public class RowMapperFactory {
public RowMapper<Employee> employeeRowMapper() {
return new RowMapper<Employee>() {
@Override
public Employee mapRow(ResultSet resultSet) throws SQLException {
return new Employee(
new BigInteger(String.valueOf(resultSet.getInt("id"))),
new FullName(resultSet.getString("firstname"),resultSet.getString("lastName"),resultSet.getString("middleName") ),
Position.valueOf(resultSet.getString("position")),
LocalDate.from((TemporalAccessor) resultSet.getDate("hired")),
new BigDecimal(resultSet.getInt("salary"))
);
}
};
}

}

如果columnName"hired"不起作用,请尝试使用索引,看看是否能找到您要查找的列。

找到它后,您可以以LocalDate格式检索它,如下所示:

LocalDate date = resultSet.getObject(colIndex, LocalDate.class);

相关内容

  • 没有找到相关文章

最新更新