spring CrudRepository 是否支持列表的 deleteBy?

Does spring CrudRepository support deleteBy of a list?

Spring 数据 CrudRepository 是否提供通过非主键的属性删除条目列表?

public interface MyRepository extends CrudRepository<MyEntity, Long> {
    @Modifying
    @Transactional
    public void deleteByName(List<String> names);
}

是的,这是可能的,documentation 用下面的例子解释了 In 关键字。示例进一步表明,列表参数不必是实体主键的类型:

In -> findByAgeIn(Collection<Age> ages)

In and NotIn also take any subclass of Collection as parameter as well as arrays or varargs.

然后这又可以应用于您的删除查询:

In addition to query methods, query derivation for both count and delete queries, is available.

类似于:

void deleteByNamesIn(List<String> names);

可以通过非主键的属性进行删除。 对于列表参数,我认为您正在寻找 in 运算符,因此请尝试以下操作:

void deleteByNamesIn(List<String> names);

(我可能会补充的另一个小评论:默认情况下,接口中定义的方法是 public 因为接口声明中的私有方法没有什么意义。所以在我看来这是很好的风格在此处删除 public)