登录
首页 >  数据库 >  MySQL

springboot 2.1.7 + mysql5.6 弃用 Calendar类型字段

来源:SegmentFault

时间:2023-01-18 19:36:30 142浏览 收藏

大家好,今天本人给大家带来文章《springboot 2.1.7 + mysql5.6 弃用 Calendar类型字段》,文中内容主要涉及到MySQL、springboot、calendar,如果你对数据库方面的知识点感兴趣,那就请各位朋友继续看下去吧~希望能真正帮到你们,谢谢!

原因:在使用

package com.example.demo;

import org.hibernate.annotations.CreationTimestamp;

import javax.persistence.*;
import java.sql.Timestamp;
import java.util.Calendar;

@Entity
public class Student {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String name;
    
    @CreationTimestamp
    private Calendar applyTime;
    
    @CreationTimestamp
    private Timestamp applyTimestamp;

    // 省略构造函数及setter/getter
}

为了测试代码更简单,我们为

package com.example.demo;

import org.springframework.data.repository.CrudRepository;

public interface StudentRepository extends CrudRepository {
}

测试代码

package com.example.demo;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@SpringBootTest
@RunWith(SpringRunner.class)
public class StudentRepositoryTest {
    private static final Logger logger = LoggerFactory.getLogger(StudentRepositoryTest.class);
    @Autowired StudentRepository studentRepository;

    @Test
    public void test() {
        Student student = new Student();
        studentRepository.save(student);

        Student student1 = studentRepository.findById(student.getId()).get();
        System.out.println(student1.getApplyTime().getTimeInMillis());
        System.out.println(student1.getApplyTimestamp().getTime());
   }
}

结果如下:

    @CreationTimestamp
    private Timestamp applyTime;

老数据-8时处理

update student set apply_time = SUBTIME(apply_time, '8:00');

终于介绍完啦!小伙伴们,这篇关于《springboot 2.1.7 + mysql5.6 弃用 Calendar类型字段》的介绍应该让你收获多多了吧!欢迎大家收藏或分享给更多需要学习的朋友吧~golang学习网公众号也会发布数据库相关知识,快来关注吧!

声明:本文转载于:SegmentFault 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>
评论列表