zoukankan      html  css  js  c++  java
  • SPRING BOOT:Hibernate实现的JPA中使用joda-time

    SPRING BOOT:Hibernate实现的JPA中使用joda-time

    joda-time在java8出现之前很长时间作为Java中处理日期时间的事实标准,但是想要在JPA实体类中使用joda-time需要做以下配置

    dependencies

    pom.xml文件中添加

    <dependency>
    	<groupId>joda-time</groupId>
    	<artifactId>joda-time</artifactId>
    </dependency>
    <dependency>
    	<groupId>org.jadira.usertype</groupId>
    	<artifactId>usertype.extended</artifactId>
    	<version>5.0.0.GA</version>
    </dependency>
    

    application.properties

    src/main/resources/application.properties配置文件中添加

    spring.jpa.properties.jadira.usertype.autoRegisterUserTypes = true
    
    

    JPA实体类

    // 映射为 DATETIME 类型字段 (MySQL)
    private DateTime createTime;
    
    // 映射为 DATE 类型字段 (MySQL)
    private LocalDate birthdayDate;
    

    绑定json数据

    pom.xml中添加

    <dependency>
      <groupId>com.fasterxml.jackson.datatype</groupId>
      <artifactId>jackson-datatype-joda</artifactId>
    </dependency>
    

    在controller中接收json请求

    @RequestMapping("/add")
    @ResponseBody
    public User addUser(@RequestBody User user) {
     // ....
    }
    

    发送一个Content-Type为application/json的请求, addUser方法将接收一个User实例

    {"createTime":"1970-01-01T00:00:00","birthdayDate":"1970-01-01"}
    

    完整代码:http://git.oschina.net/kevinlieu/spring-boot-jpa-joda-time

  • 相关阅读:
    空心杯 电机
    scikit learn 安装
    python fromkeys() 创建字典
    python 清空列表
    mac最常用快捷键
    php while循环
    php 获取某个日期n天之后的日期
    php 添加时间戳
    php 格式化时间
    php 数值数组遍历
  • 原文地址:https://www.cnblogs.com/darkdog/p/7215550.html
Copyright © 2011-2022 走看看