JPA的动态条件查询之Hql
程序员文章站
2022-04-21 15:40:45
...
JPA的动态条件查询之Hql
1.Hql的动态查询语句(重点部分)
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
//1.Hql的动态查询语句
@Query(value = "SELECT" +
" trr.companyName as companyName, " +
" trr.status as status, " +
" trr.rejectReason as rejectReason," +
" trr.businessLicense as businessLicense, " +
" tru.recruiterName as recruiterName, " +
" trr.createTime as createTime, " +
" trr.id as id ," +
" trr.status as status ," +
" user.userName as userName " +
" FROM " +
" RecruiterUser AS tru " +
" LEFT JOIN RecruiterRecord AS trr ON tru.id = trr.recruiterUserId " +
" LEFT JOIN User AS user ON user.id = tru.userId " +
" where (?1 is null or trr.companyName like %?1%) and (?3 is null or tru.recruiterName like %?3%) and (?2 is null or tru.recruiterPhone like %?2%) ")
Page<RecruiterRecordResult> findRecord(String companyName, String recruiterPhone, String recruiterName, Pageable request);
2.接收的实体类
//接收的实体类
public interface RecruiterRecordResult {
String getCompanyName();
String getBusinessLicense();
String getRecruiterName();
Date getCreateTime();
Integer getId();
Integer getStatus();
String getUserName();
}
3.测试调用的代码
//调用的代码
String companyName = recruiterVo.getCompanyName();
String recruiterPhone = recruiterVo.getRecruiterPhone();
String recruiterName = recruiterVo.getRecruiterName();
PageRequest request = PageRequest.of(page, size, Sort.by("updateTime"));
Page<RecruiterRecordResult> resultSimplePage = recruiterRecordRepository.findRecord(companyName,recruiterPhone,recruiterName, request);
return resultSimplePage;
下一篇: 初步学习BFS的心得体会
推荐阅读
-
mysql进阶讲解之数据表中带OR的多条件查询
-
Excel里两种方法实现动态查询与查询符合条件的多个结果
-
Spring data jpa的使用与详解(复杂动态查询及分页,排序)
-
基于jpa的specification实现动态查询
-
spring data jpa的动态查询封装
-
Hibernate的Criteria 之条件查询
-
在基于Spring架构的通用查询系统中用DWR实现查询条件的动态显示
-
在 Grails 中利用闭包实现查询条件的动态构造
-
asp.net实现的MVC跨数据库多表联合动态条件查询功能示例
-
序列化表单为json对象,datagrid带额外参提交一次查询 后台用Spring data JPA 实现带条件的分页查询 多表关联查询