meethigher-JPA实体监听器-@EntityListeners

2 篇文章 0 订阅

参考文章JPA实体类监听器@EntityListeners注解使用实例_疯狂的蜗牛-CSDN博客_entitylisteners

本文源码

这也是来源于工作中的一个小需求,因为产品迭代时,需要给前端创建人,但是由于创建人是在操作记录的表里记录的,如果每次都要进行查询,效率很低,所以统一在数据表里加上创建人名称和创建人id,为了达到方便、批量的目的,就使用了jpa的实体监听器。

创建springboot项目,导入pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>top.meethigher</groupId>
    <artifactId>jpa-entitylistener-test</artifactId>
    <version>1.0.0</version>
    <name>jpa-entitylistener-test</name>
    <description>chenchuancheng&apos;s demo</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!--web-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--jpa-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <!--sqlite-->
        <dependency>
            <groupId>org.xerial</groupId>
            <artifactId>sqlite-jdbc</artifactId>
            <version>3.34.0</version>
        </dependency>
        <dependency>
            <groupId>com.github.gwenn</groupId>
            <artifactId>sqlite-dialect</artifactId>
            <version>0.1.2</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

创建配置文件application.yml

#logging:
#  config: classpath:logback.xml
server:
  port: 9090
  ssl:
    enabled: false
spring:
  datasource:
    driver-class-name: org.sqlite.JDBC
    url: jdbc:sqlite:D:/test.db
  jpa:
    database-platform: org.sqlite.hibernate.dialect.SQLiteDialect
    hibernate:
      ddl-auto: update
      naming:
        physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
    show-sql: true
  mvc:
    async:
      request-timeout: 30000

创建实体,@EntityListeners里面配置的是监听器对象

@Entity
@EntityListeners(TestEntityListener.class)
public class TestEntity {
    @Id
    @Column(name = "id", length = 36, nullable = false)
    //这个是hibernate的注解/生成32位UUID
    @GenericGenerator(name = "idGenerator", strategy = "uuid")
    @GeneratedValue(generator = "idGenerator")
    private String id;

    private String name;

    @Embedded
    private CommonField commonField;


    public String getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public CommonField getCommonField() {
        return commonField;
    }

    public void setCommonField(CommonField commonField) {
        this.commonField = commonField;
    }

    @Override
    public String toString() {
        return "TestEntity{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", commonField=" + commonField +
                '}';
    }
}

创建实体公共创建人对象

@Embeddable
public class CommonField {
    private Date createTime;

    private Date updateTime;

    private String createUserId;

    private String createUserName;

    public Date getCreateTime() {
        return createTime;
    }

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

    public Date getUpdateTime() {
        return updateTime;
    }

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

    public String getCreateUserId() {
        return createUserId;
    }

    public void setCreateUserId(String createUserId) {
        this.createUserId = createUserId;
    }

    public String getCreateUserName() {
        return createUserName;
    }

    public void setCreateUserName(String createUserName) {
        this.createUserName = createUserName;
    }

    @Override
    public String toString() {
        return "CommonField{" +
                "createTime=" + createTime +
                ", updateTime=" + updateTime +
                ", createUserId='" + createUserId + '\'' +
                ", createUserName='" + createUserName + '\'' +
                '}';
    }
}

创建jpa实体监听器,里面使用的这四个注解,顾名思义即可。

public class TestEntityListener {

    @PrePersist
    public void PrePersist(Object entity) {
        System.out.println("进行insert之前");
        if(entity instanceof TestEntity) {
            System.out.println(entity.toString());
            CommonField commonField = ((TestEntity) entity).getCommonField();
            if(ObjectUtils.isEmpty(commonField)) {
                commonField=new CommonField();
                commonField.setCreateTime(new Date());
                commonField.setUpdateTime(new Date());
                commonField.setCreateUserId("111");
                commonField.setCreateUserName("ccc");
                ((TestEntity) entity).setCommonField(commonField);
            }
            System.out.println(entity.toString());
        }

    }

    @PostPersist
    public void PostPersist(Object entity) {
        System.out.println("进行insert之后");
    }

    @PreUpdate
    public void PreUpdate(Object entity) {
        System.out.println("进行update之前");
    }

    @PostUpdate
    public void PostUpdate(Object entity) {
        System.out.println("进行update之后");
    }
}
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值