使用SpringDataJpa进行高级查询

使用方法名称来指定高级查询的条件

SpringDataJpa支持使用方法名称来指定高级查询的条件
	方法名称必须遵守以下格式要求:
		① 方法名称必须以findBy或者findAllBy开头
		② findBy或者findAllBy后面紧跟domain实体类的字段名称(首字母大写)
		③ 字段名称后面紧跟运算符单词
			Like   NotLike :用于名称的模糊查询
			In      NotIn  :判断是否在某个集合中
			Between NotBetween :在什么范围内
			GreaterThan  GreaterThanEqual :大于、大于等于
			LessThan        LessThanEqual :小于、小于等于
			Euqal   NotEqual :是否相等
			IsNull  IsNotNull  :是否为空
		④ 方法的参数列表要求如下:
			Like  方法的参数必须是一个String类型   而且要添加%
			In 对参数类型没有要求,个数也没有要求
			Between 参数类型必须是整数/浮点数/日期类型,而且必须是两个参数,两个参数的类型必须统一
			GreaterThan  GreaterThanEqual  LessThan  LessThanEqual 参数类型必须是整数/浮点数/日期类型,而且必须是1个参数
		⑤ 如果字段名称后面紧跟运算符单词是Euqal,则Euqal单词可以省略
		⑥ IsNull  IsNotNull 则方法不传参数
		⑦ 多条件查询,则可以使用And或者Or来连接

例如:用户名模糊查询 用户名中包含admin1的数据

//根据用户名模糊查询
List<Employee> findAllByUsernameLike(String username);

使用@Query注解来指定查询语句

可以使用@Query注解来指定JPQL或Sql查询语句
nativeQuery=false表示JPQL查询
nativeQuery=true表示原生SQL查询
默认是false

@Query("select e from Employee e where username like ?1")
List<Employee> getAllByUsername(String username);

@Query(nativeQuery=true,value="select * from Employee where username like ?1")
List<Employee> getAllByUsernameSql(String username);

使用JpaSpecificationExecutor接口

JpaSpecificationExecutor接口是SpringDataJpa提供的专门用来做高级查询的,封装高级查询的条件组合,通常情况下采用匿名内部类方式,直接创建Specification对象

public void testFindByManyCondition(){
    Specification<Employee> specification = new Specification<Employee>() {
        @Override
        public Predicate toPredicate(Root<Employee> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
            Path username=root.get("username");
            Predicate predicate1=criteriaBuilder.like(username,"%admin1%");
            Path age=root.get("age");
            Predicate predicate2=criteriaBuilder.ge(age, 25);
            CriteriaQuery<?> query=criteriaQuery.where(predicate1, predicate2);
            return query.getRestriction();
        }
    };
    List<Employee> list=employeeRepository.findAll(specification);
    list.forEach(System.out::println);

}

Specification接口中重写方法传入的三个参数:
root 看成是一个实体类,可以通过它获取字段名称
可以通过它的 Path get(String attributeName); 这个方法拿到我们要操作的字段
criteriaQuery 将多个条件组合起来,多个条件之间用and或者or来连接
criteriaBuilder 拼接一个条件的语句 例如 username = ?

使用jpa-spec插件

这是插件是对JpaSpecificationExecutor接口的扩展,只是对Specification对象的创建方式进行了封装,采用了一个工具类调用静态方法来实现,对查询条件的拼接进行了简化。
导包:

<dependency>
  	<groupId>com.github.wenhao</groupId>
  	<artifactId>jpa-spec</artifactId>
  	<version>3.1.1</version>
  	<!-- 把所有依赖都过滤 -->
  	<exclusions>
    	<exclusion>
     	 	<groupId>*</groupId>
      		<artifactId>*</artifactId>
   	 	</exclusion>
  	</exclusions>
</dependency>

使用Specifications静态类进行条件拼接
Specifications.and()传入泛型并使用and关键字进行条件拼接
最后要调用build()方法返回Specification对象

@Test
public void testFindByManyCondition(){
    Specification<Employee> specification=Specifications.<Employee>and()
            .like("username", "%admin1%")
            .ge("age",25)
            .build();
    List<Employee> list=employeeRepository.findAll(specification);
    list.forEach(System.out::println);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值