1

maven

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>

配置软删除

@Entity
@Table(name="demo")
@SQLDelete(sql = "update demo set deleted = 1 where id = ?")
@Where(clause = "deleted = 0")
public class Demo{

    @javax.persistence.Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column(name="deleted")
    private Integer deleted = 0;

    private String userName;

    @Override
    public Long getId() {
        return id;
    }

    @Override
    public void setId(Long id) {
        this.id = id;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public Integer getDeleted() {
        return deleted;
    }

    public void setDeleted(Integer deleted) {
        this.deleted = deleted;
    }
}

测试删除及查询

    @Test
    public void testDelete(){
        demoDao.delete(10L);
    }

    @Test
    public void testQuery(){
        List<Demo> demos = demoDao.findAll();
        System.out.println(demos);
    }

带版本

如果entity带了version,则需要改SQLDELETE为

@SQLDelete(sql = "update demo set deleted = 1 where id = ? and version = ? ")

支持audit

如果有支持audit的话,需要去掉这个SQLDELETE的sql,自己手工update,这样可以确保lastModifiedBy是正确的。

doc


codecraft
11.9k 声望2k 粉丝

当一个代码的工匠回首往事时,不因虚度年华而悔恨,也不因碌碌无为而羞愧,这样,当他老的时候,可以很自豪告诉世人,我曾经将代码注入生命去打造互联网的浪潮之巅,那是个很疯狂的时代,我在一波波的浪潮上留下...