<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爲java
@SQLDelete(sql = "update demo set deleted = 1 where id = ? and version = ? ")
若是有支持audit的話,須要去掉這個SQLDELETE的sql,本身手工update,這樣能夠確保lastModifiedBy是正確的。spring