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是正确的。
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。