按照上一篇文章 我们知道 我们定义的方法 都要根据它的规范进行定义 不然就没法实用
这篇我们讲@Query 查询注解 我们就可以不需要遵循它的方法规则去编写 咱们讲@Query定义到方法上即可 下面咱们通过例子来讲
本案例对应的方法------------------> getEmployeeByAge 这里我们可以定义自己的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(); }
测试类
对应的方法 ---------->testgetEmployeeByAge
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()); } }
输出结果
咱们看下数据库表最大的年龄是不是这条记录
再来一个 还是在 EmployeeRepository 加方法
//获取name包含 哪些 并且年龄等于多少岁 @Query("select o from Employee o where o.name like %?1% and o.age = ?2") public List<Employee> querayParams(String name,Integer age);
然后编写测试类
@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()); } }
看下输出结果
very good 就那么简单