springboot jpa 创建、更新时间戳

本文介绍如何使用SpringBoot JPA实现实体类中创建时间和更新时间的自动记录。通过@CreationTimestamp和@UpdateTimestamp注解,可以轻松地在数据库操作时自动设置这些时间戳字段,无需手动编写额外的代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


springboot jpa 创建、更新时间戳

 

****************************

相关注解

 

@CreationTimestamp

@ValueGenerationType(
    generatedBy = CreationTimestampGeneration.class
)
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.METHOD})
public @interface CreationTimestamp {
}

 

@UpdateTimestamp

@ValueGenerationType(
    generatedBy = UpdateTimestampGeneration.class
)
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.METHOD})
public @interface UpdateTimestamp {
}

 

 

***************************

示例

 

****************

pojo 层

 

Person

@Entity
public class Person {
    private Integer id;
    private String name;
    private Integer age;
    private Timestamp createTime;
    private Timestamp updateTime;

    @Id
    @Column(name = "id", nullable = false)
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    @Basic
    @Column(name = "name", nullable = false, length = 12)
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Basic
    @Column(name = "age", nullable = false)
    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Basic
    @Column(name = "create_time", nullable = true)
    @CreationTimestamp
    public Timestamp getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Timestamp createTime) {
        this.createTime = createTime;
    }

    @Basic
    @Column(name = "update_time", nullable = true)
    @UpdateTimestamp
    public Timestamp getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(Timestamp updateTime) {
        this.updateTime = updateTime;
    }

    。。。。

}

 

****************

controller层

 

PersonController

@RestController
public class PersonController {

    @Autowired
    private PersonRepository personRepository;

    @RequestMapping("/save")
    public String save(){
        for(int i=0;i<10;i++){
            Person person=new Person();
            person.setId(i+1);
            person.setName("瓜田李下"+i);
            person.setAge(i);

            personRepository.save(person);
        }

        return "success";
    }

    @RequestMapping("/update")
    public Person update(){
        Person person=personRepository.getOne(1);
        person.setName("海贼王");
        personRepository.save(person);

        return person;
    }
}

 

 

****************************

执行/save,/update后,数据库显示

       

                    

             

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值