

public interface BookDistributionRepository {
List<BookDistribution> findByBookIdAndStatus(String bookId, Short status);
}
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.querydsl.QuerydslPredicateExecutor;
import org.springframework.stereotype.Repository;
@Repository
public interface BookDistributionRepositoryJPA extends MongoRepository<BookDistribution, String>,
QuerydslPredicateExecutor<BookDistribution>, BookDistributionRepository {
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import java.util.List;
public class BookDistributionRepositoryJPAImpl {
@Autowired
private MongoTemplate mongoTemplate;
public Iterable<BookDistribution> findByBookIdAndStatus(String bookId, short status) {
Criteria criteria = Criteria.where("bookId").is(bookId);
if (status != null) {
criteria.and("status").is(status);
}
return mongoTemplate.find(new Query(criteria), BookDistribution.class);
}
}
1、方法名称相同:重写的方法名称必须与父类中的方法名称相同,包括方法的大小写和参数列表。
2、方法参数列表相同:重写的方法的参数列表(参数的数量、类型和顺序)必须与父类中的方法的参数列表相同。
3、方法返回类型兼容:重写的方法的返回类型必须与父类中的方法的返回类型兼容。这意味着子类的返回类型可以是父类方法返回类型的子类型(子类或接口类型)或者与其相同。如果是基本数据类型,返回类型必须相同。
4、访问修饰符不严格小于父类:重写的方法的访问修饰符不能比父类中的方法更严格。具体来说,如果父类方法是 public,子类方法可以是 public、protected 或 default(包内可见),但不能是 private。如果父类方法是 protected,子类方法可以是 protected 或 public,但不能是 default 或 private。
5、不能抛出更多异常:重写的方法不能抛出比父类方法更多或更宽的异常。子类方法可以不抛出异常,也可以抛出父类方法声明的异常或其子类异常,但不能抛出父类方法未声明的新异常。
6、不能使用 static 或 final 修饰符:重写的方法不能使用 static(静态方法)或 final(不能被重写的方法)修饰符。只有非 static 和非 final 的方法才能被重写。
7、必须是子类:方法重写只能在子类中进行,子类继承父类并重写父类方法。
对方法重写的要求还挺多,经过仔细对比,不难发现接口中的方法入参类型和重写的方法入参类型不一样。 前者是Short包装类型,而后者是基本数据类型short。
修改实现方法的入参类型,和接口中的保持一致,也为包装类型Short。
程序启动成功,断点调试也能够进入重写方法里。
不仅如此,反过来,接口中的方法命名也不要求符合Jpa命名规范了。
也就是说,接口中的方法是要自定义实现的话,其命名规则是随意的,不受Jpa命名规范的约束。
注解@EnableJpaRepositories是在spring-data-jpa框架中,它有一个参数配置,叫repositoryImplementationPostfix,其值默认便是“Impl”。

spring-boot-autoconfigure框架有对其进行默认开启。

查看JpaRepositoriesRegistrar.java,默认有开启注解EnableJpaRepositories。

通过本文也再次提醒我们,java是一个强类型的语言,作为方法重写这样的一个基础知识,对参数的类型可不能大意。