由于 hsqldb 无法使用 JAva 8 Time API,我如何将配置文件从 HSQLDB 更改为 POSTGRES?

How can I change profile from HSQLDB to POSTGRES since hsqldb can't work with JAva 8 Time API?

我有一个执行方法的 class,在更改配置文件时出现这样的问题,我意识到 hsqldb 无法使用 Java 8 次 API ,所以我想使用 Profile 与 Postgres 共享执行,我想知道如何做到这一点 这是 class 方法

 @Repository
 public class JdbcMealRepository implements MealRepository {

private static final RowMapper<Meal> ROW_MAPPER = BeanPropertyRowMapper.newInstance(Meal.class);

private final JdbcTemplate jdbcTemplate;

private final NamedParameterJdbcTemplate namedParameterJdbcTemplate;

private final SimpleJdbcInsert insertMeal;

@Autowired
public JdbcMealRepository(JdbcTemplate jdbcTemplate, NamedParameterJdbcTemplate namedParameterJdbcTemplate) {
    this.insertMeal = new SimpleJdbcInsert(jdbcTemplate)
            .withTableName("meals")
            .usingGeneratedKeyColumns("id");

    this.jdbcTemplate = jdbcTemplate;
    this.namedParameterJdbcTemplate = namedParameterJdbcTemplate;
}

@Override
public Meal save(Meal meal, int userId) {
    MapSqlParameterSource map = new MapSqlParameterSource()
            .addValue("id", meal.getId())
            .addValue("description", meal.getDescription())
            .addValue("calories", meal.getCalories())
            .addValue("date_time", meal.getDateTime())
            .addValue("user_id", userId);

    if (meal.isNew()) {
        Number newId = insertMeal.executeAndReturnKey(map);
        meal.setId(newId.intValue());
    } else {
        if (namedParameterJdbcTemplate.update("" +
                "UPDATE meals " +
                "   SET description=:description, calories=:calories, date_time=:date_time " +
                " WHERE id=:id AND user_id=:user_id", map) == 0) {
            return null;
        }
    }
    return meal;
}

@Override
public boolean delete(int id, int userId) {
    return jdbcTemplate.update("DELETE FROM meals WHERE id=? AND user_id=?", id, userId) != 0;
}

@Override
public Meal get(int id, int userId) {
    List<Meal> meals = jdbcTemplate.query(
            "SELECT * FROM meals WHERE id = ? AND user_id = ?", ROW_MAPPER, id, userId);
    return DataAccessUtils.singleResult(meals);
}

@Override
public List<Meal> getAll(int userId) {
    return jdbcTemplate.query(
            "SELECT * FROM meals WHERE user_id=? ORDER BY date_time DESC", ROW_MAPPER, userId);
}

@Override
@Profile(Profiles.POSTGRES_DB)
public List<Meal> getBetweenHalfOpen(LocalDateTime startDateTime, LocalDateTime endDateTime, int userId) {
    return jdbcTemplate.query(
            "SELECT * FROM meals WHERE user_id=?  AND date_time >=  ? AND date_time < ? ORDER BY date_time DESC",
            ROW_MAPPER, userId, startDateTime, endDateTime);
}

}

根据 https://docs.spring.io/spring-boot/docs/current/reference/html/application-properties.html 更改您的 application.properties 并添加 postgresql 到您的 maven/gradle 依赖项。

或者简单地从 hsqldb 更改为 h2。请注意,h2 至少在 jdbc 中支持 OffsetDateTime 但不支持 ZonedDateTime,我不知道如果您尝试将带有 ZonedDateTime 的记录保存到时间戳列,所有转换 Spring 都会执行。