SpringData关键字查询练习

我们在上一节知道SpringData关键字有很多,我就拿几个例子练练手

1.需求我们查询lastName like sun and id < ?的查询

package com.fxr.springdata;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;


public interface PersonRepsotory extends JpaRepository<Person,Integer>{

	//根据lastName来获取对应的Person
	Person getByLastName(String lastName);
	
	//我们来试着写一个查询laskName like 'sun' and id < ?的方法
	List<Person> getByLastNameStartingWithAndIdLessThan(String lastName,Integer id);
	
}

2.需求我们查询WHERE lastName LIKE %? AND id < ?

package com.fxr.springdata;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;


public interface PersonRepsotory extends JpaRepository<Person,Integer>{

	//根据lastName来获取对应的Person
	Person getByLastName(String lastName);
	
	//我们来试着写一个查询laskName like 'sun' and id < ?的方法
	List<Person> getByLastNameStartingWithAndIdLessThan(String lastName,Integer id);
	
	
	//WHERE lastName LIKE %? AND id > ?
	List<Person> getByLastNameEndingWithAndIdGreaterThan(String lastName,Integer id);
	
}

  好了,对着我们上一节那个关键字的表,你可以写出符合你的查询的逻辑语句,如果我们在项目中遇见业务特别复杂的查询的话,我们需要自己写SQL,这个怎么实现,我们会在以后的章节中讲到,嘿嘿·········

原文地址:https://www.cnblogs.com/airycode/p/6535475.html