有没有一种很好的方法来处理 Spring Data JDBC 中的软删除?
在 Spring Data JPA 中,我们可以添加@Where(clause="is_active=1")
注释或扩展CrudRepository
或PagingAndSortingRepository
.
由于 Spring Data JDBC 在查询中不支持 SPEL,我们不能像这样以通用方式编写它们:
@NoRepositoryBean
public interface SoftDeleteCrudRepository<T extends BasicEntity, ID extends
Long> extends CrudRepository<T, ID> {
//Override CrudRepository or PagingAndSortingRepository's query method:
@Override
@Transactional(readOnly = true)
@Query("select e from #{#entityName} e where e.deleteFlag=false")
public List<T> findAll();
//Look up deleted entities
@Query("select e from #{#entityName} e where e.deleteFlag=true")
@Transactional(readOnly = true)
public List<T> findAllDeleted();
//Soft delete.
@Query("update #{#entityName} e set e.deleteFlag=true where e.id=?1")
@Transactional
@Modifying
public void softDelete(String id);
...
}
那么扩展CrudRepository
或PagingAndSortingRepository
意味着为每个实体/表的每个存储库编写相同的查询?喜欢
Repository1
@Override
@Transactional(readOnly = true)
@Query("select id, name, value, deleteFlag from table1 e where e.deleteFlag=false")
public List<T> findAll();
....
Repository2
@Override
@Transactional(readOnly = true)
@Query("select id, name, value, deleteFlag from table2 e where e.deleteFlag=false")
public List<T> findAll();
....
感谢您提前回复!