// where name = ?1 employeeRepository.findByName("test1")
// where name = ?1 and age = ?2 employeeRepository.findByNameAndAge("test1", 20);
// where age between ?1 and ?2 【包含头尾】 List<Employee> byAgeBetween = employeeRepository.findByAgeBetween(10, 16);
// where age < ?1 List<Employee> list = employeeRepository.findByAgeLessThan(10);
// where age > ?1 List<Employee> list = employeeRepository.findByAgeGreaterThan(90);
// where name is null 【不包含为空字符串的数据】 List<Employee> list = employeeRepository.findByNameIsNull();
// where name like "test9%" 以test9为开头的name List<Employee> list = employeeRepository.findByNameLike("test9%"); 或者 List<Employee> employees = employeeRepository.findByNameStartingWith("test")
// where name like "test_" 以test开头,且后面只模糊匹配一位 List<Employee> list2 = employeeRepository.findByNameLike("test_");
// where name like "%6" 模糊匹配以6结尾的 List<Employee> employees = employeeRepository.findByNameEndingWith(6)
// where name in (?1, ?2) List<String> names = Arrays.asList("test1", "test2"); List<Employee> employees = employeeRepository.findByNameIn(names);
// where age <> ?1 List<Employee> employees = employeeRepository.findByAgeNot(99);
// where name = ?1 order by age desc List<Employee> findByNameOrderByAgeDesc("test");
使用自定义 Sql 以及 原生 Sql 查询
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/** EmployeeRepository.java 添加方法 */
// 根据姓名与年龄查找,[通过占位符获取参数值] @Query("select o from Employee o where name = ?1 and age = ?2") List<Employee> queryEmployeeByParams(String name, Integer age);
// 根据姓名与年龄查找,[通过命名参数获取参数值],必须使用 @Param 注解 @Query("select o from Employee o where name = :name and age = :age") List<Employee> queryEmployeeByParams2(@Param("name") String name, @Param("age") Integer age);
// 原生SQL,与上面不同的是,上面使用的是对象名称以及对象属性名称,Native SQL使用数据库表名以及字段名 @Query(nativeQuery = true, value = "select * from employee where name = :name and age = :age") List<Employee> queryEmployeeByParams3(@Param("name") String name, @Param("age") Integer age);