欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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;