【问题标题】:Springboot saves date to database 1 day offSpring Boot 将数据保存到数据库中休息 1 天
【发布时间】:2019-08-28 04:20:01
【问题描述】:

问题

我正在使用 SpringBoot 编写一个应用程序,使用休眠将数据写入 MySQL 数据库。我使用 thymeleaf 从 HTML 表单中获取实体的数据。 当我将记录保存到数据库时,它们落后了 1 天,因此 2019-04-09 变为 2019-04-08。

到目前为止的效果

到目前为止,我发现解决此问题的唯一解决方案是将系统时间更改为 UTC,但显然这对于​​我正在为其编写应用程序的人来说会有所不同。我在互联网上看到了很多关于这方面的话题,但大多数时候它是使用 JVM hacks 修复的,如果可能的话,我想以适当的方式来做。

什么让我感到困惑

我正在使用 java.time.LocalDate,它据称不使用时区,并将此日期保存到 SQL 数据库列中,类型为“日期”。我尝试将我的数据库时区更改为 UTC,但这似乎没有什么不同。唯一不同的是将我的窗口时间更改为 UTC。

application.properties

spring.datasource.url=jdbc:mysql://localhost:3306/boxbaza?serverTimezone=UTC
spring.datasource.username=box
spring.datasource.password=box
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.database-platform=org.hibernate.dialect.MySQL55Dialect
hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create

休眠实体

@DateTimeFormat(pattern = "yyyy-MM-dd")
@Column(name = "dataUmowy")
private LocalDate dataUmowy;

控制器

@SessionAttributes("ofwca")
@Controller
public class OfwcaController {

@Autowired
private OfwcaDao ofwcaDao;

@RequestMapping(value = "/panelOfwca/daneOfwca", method = RequestMethod.POST)
public String daneOfwca(Model model, @ModelAttribute("ofwca") Ofwca ofwca){

    ofwcaDao.save(ofwca);

    model.addAttribute("title", "Dane " + ofwca.getImie() + " " + ofwca.getNazwisko());
    model.addAttribute("ofwca", ofwca);
    return "panelOfwca/daneOfwca";
}

数据访问对象

@Transactional
@Repository
public interface OfwcaDao extends CrudRepository<Ofwca, Integer> {

public List<Ofwca> findAll();

public Ofwca findByIdOfwca(Integer idOfwca);

}

html 表单

<form role="form" method="post" th:action="@{/panelOfwca/daneOfwca}" th:object="${ofwca}">

  <div class="form-group col-xs-4">
    <label for="dataUmowy">Data umowy</label>
    <input type="date" id="dataUmowy" th:field="*{dataUmowy}" class="form-control">
  </div>
</form>

【问题讨论】:

  • 数据库中的列类型是什么?
  • 在 MySQL 中类型是 'DATE'

标签: java mysql hibernate spring-boot jvm


【解决方案1】:

您的 jdbc URL 出现此问题:

spring.datasource.url=jdbc:mysql://localhost:3306/boxbaza?serverTimezone=UTC

你必须按如下方式更改 jdbc 连接 URL:

spring.datasource.url=jdbc:mysql://localhost:3306/boxbaza?serverTimezone=Europe/Berlin

我已经解决了我落后 1 天的问题,并进行了上述更换。

【讨论】:

    【解决方案2】:

    虽然我已经很长时间没有使用 Hibernate(尤其是其中的 java.time),但您的问题看起来与您的 MySQL 服务器期望具有 UTC 时区和本地服务器的“日期”这一事实非常相关在Europe/Berlin 时区运行,该时区(至少)是 UTC+1。 这意味着,任何日期,例如我猜 2019-08-04 是前一天保存的(例如 2019-07-04) - 这是因为时区之间有 1-2 小时的差异。

    在大多数情况下,最好将所有没有时区的日期/时间值默认视为具有 UTC 时区。 如果这不可能,那么您应该调整您的数据库设置以使用与您的 Java 应用程序相同的时区。

    进一步阅读:

    【讨论】:

      【解决方案3】:

      我现在通过使用不同的休眠时区(我的本地服务器所在的时区)解决了我的问题,但我不确定将来在将应用程序部署到云时它将如何工作。可能需要再次根据网络服务器更改参数。

      serverTimezone=Europe/Berlin
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-02-12
        • 2018-05-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-30
        • 2018-06-03
        • 1970-01-01
        相关资源
        最近更新 更多