前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Jpa配置实体类创建时间更新时间自动赋值,@CreateDate,@LastModifiedDate

Jpa配置实体类创建时间更新时间自动赋值,@CreateDate,@LastModifiedDate

作者头像
天涯泪小武
发布2019-01-17 11:43:28
4.8K0
发布2019-01-17 11:43:28
举报
文章被收录于专栏:SpringCloud专栏

操作数据库映射实体类时,通常需要记录createTime和updateTime,如果每个对象新增或修改去都去手工操作创建时间、更新时间,会显得比较繁琐。

Springboot jpa提供了自动填充这两个字段的功能,简单配置一下即可。@CreatedDate、@LastModifiedDate、@CreatedBy、@LastModifiedBy前两个注解就是起这个作用的,后两个是设置修改人和创建人的,这里先不讨论。

首先,我们的很多实体类都是需要创建时间和更新时间的,我们不想在每个实体类里都去定义这两个字段,那么我们把它抽取到基类中,让实体类去继承它。

代码语言:javascript
复制
package com.tianyalei.testautotime.entity;

import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;

/**
 * Created by wuwf on 17/4/21.
 */
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public abstract class BaseEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    protected Integer id;

    @CreatedDate
    private Long createTime;

    @LastModifiedDate
    private Long updateTime;

    public Integer getId() {
        return id;
    }

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

    public Long getCreateTime() {
        return createTime;
    }

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

    public Long getUpdateTime() {
        return updateTime;
    }

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

AuditingEntityListener标签开启后,下面的时间标签才会生效。

然后还需要在启动类加上@EnableJpaAuditing注解。

做完这些,我们来测试一下,新建个Springboot项目,配置一下数据库信息

代码语言:javascript
复制
spring:
  jpa:
    database: mysql
    show-sql: true
    hibernate:
      ddl-auto: update
  datasource:
      type: com.alibaba.druid.pool.DruidDataSource
      driver-class-name: com.mysql.jdbc.Driver
      url: jdbc:mysql://localhost:3306/test
      username: root
      password:

新建个普通的实体类。

代码语言:javascript
复制
package com.tianyalei.testautotime.entity;

import javax.persistence.Entity;

@Entity
public class Post extends BaseEntity {

    private String title;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
}

测试类:

代码语言:javascript
复制
import com.tianyalei.testautotime.entity.Post;
import com.tianyalei.testautotime.repository.PostRepository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class TestautotimeApplicationTests {
	@Autowired
	PostRepository postRepository;

	@Test
	public void save() {
		Post post = new Post();
		post.setTitle("title0");
		postRepository.save(post);
	}

//	@Test
//	public void update() {
//		Post post = postRepository.findOne(1);
//		post.setTitle("title1");
//		postRepository.save(post);
//	}
}

先试试新增。

可以看到已经被自动赋值了。

然后试试update,将上面的update的注释放开。

可以看到更新时间也自动修改了。

需注意,如果你没有修改任何字段的值的话,即便走了save方法,updateTime也是不会更改的。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017年09月11日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档