今天在学习Jpa过程中,看到自己的代码:
public interface UserRepository extends JpaRepository<User, Long>{}
突然发现,ID的对象类型是Long,然后就想了一下,Long这个对象类型怎么来的。
经过查看Spring data Jpa 参考文档,发现有一下说明:
红色方框字段说明:将实体类型和实体类的id类型作为类型参数。
红色方框字段说明:接口必须继承类型参数化为实体类型和实体类中的Id类型的Repository
因为本例中public interface UserRepository extends JpaRepository<T, ID>,
根据上述描述:T 需要类型化为实体类(Entity)User,ID需要实体类User中Id(我定义的Id类型是Long)的类型
故此,UserRepository接口需要写成:public interface UserRepository extends JpaRepository<User, Long>