按照上一篇文章 咱們知道 咱們定義的方法 都要根據它的規範進行定義 否則就無法實用java
這篇咱們講@Query 查詢註解 咱們就能夠不須要遵循它的方法規則去編寫 我們講@Query定義到方法上便可 下面我們經過例子來說spring
本案例對應的方法------------------> getEmployeeByAge 這裏咱們能夠定義本身的sql sql
package org.springdata.repository; import org.springdata.domain.Employee; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.Repository; import org.springframework.data.repository.RepositoryDefinition; import java.util.List; /*** * */ @RepositoryDefinition(domainClass = Employee.class, idClass = Integer.class) public interface EmployeeRepository /*extends Repository<Employee,Integer>*/ { /** * 根據名字找員工 * desc 你們能夠發現 我只聲明瞭一個方法 並無寫任何的實現類 哦了 就這樣 我們寫個實現類 * @param name * @return */ public Employee findByName(String name); // name 根據模糊查詢 而且 年齡<多少歲的員工 public List<Employee> findByNameIsStartingWithAndAgeLessThan(String name, Integer gae); // names in ('','','') 年齡小於多少 public List<Employee> findByNameInOrAgeLessThan(List<String> names,Integer age); //獲取年齡最大的信息 注意 from Employee 的是類名 不是表名 @Query(" select o from Employee o where o.age=(select max(age) from Employee t1)") public Employee getEmployeeByAge(); }
測試類 數據庫
對應的方法 ---------->testgetEmployeeByAgedom
package org.springdata; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.springdata.domain.Employee; import org.springdata.repository.EmployeeRepository; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.util.ArrayList; import java.util.List; /** * 測試類 */ public class SpringDataTest { private ApplicationContext ctx = null; private EmployeeRepository employeeRepository = null; @Before public void setup(){ ctx = new ClassPathXmlApplicationContext("beans_news.xml"); employeeRepository = ctx.getBean(EmployeeRepository.class); System.out.println("setup"); } @After public void tearDown(){ ctx = null; System.out.println("tearDown"); } @Test public void testEntityManagerFactory(){ } @Test public void testFindByName(){ System.out.println(employeeRepository); Employee employee = employeeRepository.findByName("zhangsan"); System.out.println("id:" + employee.getId() + " , name:" + employee.getName() + " ,age:" + employee.getAge()); } @Test public void testfindByNameIsStartingWithAndAgeLessThan(){ System.out.println(employeeRepository); List<Employee> employees = employeeRepository.findByNameIsStartingWithAndAgeLessThan("wang",50); for (Employee employee: employees) { System.out.println("id:" + employee.getId() + " , name:" + employee.getName() + " ,age:" + employee.getAge()); } } @Test public void testfindByNameInOrAgeLessThan(){ List<String> names = new ArrayList<String>(); names.add("wangwu1"); names.add("wangwu2"); names.add("wangwu3"); System.out.println(employeeRepository); List<Employee> employees = employeeRepository.findByNameInOrAgeLessThan(names,50); for (Employee employee: employees) { System.out.println("id:" + employee.getId() + " , name:" + employee.getName() + " ,age:" + employee.getAge()); } } @Test public void testgetEmployeeByAge(){ Employee employee = employeeRepository.getEmployeeByAge(); System.out.println("id:" + employee.getId() + " , name:" + employee.getName() + " ,age:" + employee.getAge()); } }
輸出結果測試
我們看下數據庫表最大的年齡是否是這條記錄spa
再來一個 仍是在 EmployeeRepository 加方法code
//獲取name包含 哪些 而且年齡等於多少歲 @Query("select o from Employee o where o.name like %?1% and o.age = ?2") public List<Employee> querayParams(String name,Integer age);
而後編寫測試類xml
@Test public void tesquerayParams(){ List<Employee> employees = employeeRepository.querayParams("wangwu",12); for (Employee employee: employees) { System.out.println("id:" + employee.getId() + " , name:" + employee.getName() + " ,age:" + employee.getAge()); } }
看下輸出結果blog
very good 就那麼簡單