下面是具体的使用和说明
import org.springframework.data.repository.Repository;
import com.springdata.study.entitys.Person;
//1.实际上Repository是一个口接口,没有提供任何方法,是一个标记接口
//2.实现了Repository接口就会被spring IOC容器识别为Repository Bean
// 会被纳入IOC容器中
//3.Repository接口也可以同@RepositoryDefinition 注解代替,效果是一样的
//4.接口中的泛型:第一个是那个实体类的Repository,第二个是实体类的主键的类型
//@RepositoryDefinition(domainClass=Person.class,idClass=Integer.class)
/**
* 在Repository接口中申明方法
* 1.申明方法需要符合一定的规范
* 2.查询方法需要以 find | read | get开头
* 3.涉及查询条件时 需要用条件关键字连接
* 4.属性首字母大写
* 5.支持级联属性
* 6.AddressId若当前实体类中有属性,则优先使用该属性,若想要使用级联属性,需要用下划线隔开Address_Id
*/
public interface PersonRepositoiry extends Repository<Person, Integer>{
//select p from Person where p.name = ?
Person getByName(String name);
List<Person> findByNameStartingWithAndIdLessThan(String name, Integer id);
//where name like %? and id < ?
List<Person> findByNameEndingWithAndIdLessThan(String name, Integer id);
//where email in ? age < ?
List<Person> readByEmailInOrAgeLessThan(List<String> emails, int age);
//级联属性查询
//where address.id > ?
List<Person> findByAddress_IdGreaterThan(Integer is);
}
下面是Repository的一些实现类了子接口
下面是方法书写的规则