spring-data的Repository接口的使用

下面是具体的使用和说明

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的一些实现类了子接口

下面是方法书写的规则

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值