JPA 多表分页查询

业务场景:大学生毕业后统计毕业去向。学生实体和毕业去向实体一对一关系。

实体:

@Entity
@Data
@Table(name = "t_s_student")
public class TSStudent implements Serializable {

    /**主键*/
    @Id
    @GeneratedValue(generator = "idGenerator")
    @GenericGenerator(name = "idGenerator",strategy = "uuid")
    @Column(length = 32)
    private String id;

    /**姓名*/
    @Column(name = "name")
    private String name;

    /**学号*/
    @Column(name = "student_id",length = 20)
    private String studentId;

    /**电话*/
    @Column(name = "phone",length = 20)
    private String phone;

    /**地址*/
    @Column(name = "address",length = 200)
    private String address;

    /**籍贯*/
    @Column(name = "birthplace",length = 200)
    private String birthplace;
    /**生日*/

    @Column(name = "birthday")
    private String birthday;

    /**毕业时间*/
    @Column(name = "graduation_time")
    private String graduationTime;

    /**班级*/
    @Column(name = "clazz",length = 50)
    private String clazz;

    /**
     * 性别
     */
    @Column(name = "sex",length = 5)
    private String sex;

    /**
     * 年级
     */
    @Column(name = "grade",length = 20)
    private String grade;
    /**
     * 成绩
     */
    @Column(name = "achievement",length = 20)
    private String achievement;

    /**
     * 毕业去向
     */
    @Column(name = "graduation",length = 100)
    private String graduation;

    /**
     * 所在地区
     */
    @Column(name = "area",length = 200)
    private String area;

    /**
     * QQ
     */
    @Column(name = "qq",length = 20)
    private String qq;

    /**
     * 行业
     */
    @Column(name = "industry",length = 20)
    private String industry;


}

  毕业去向实体:

@Entity
@Table(name ="t_s_graduation")
@Data
public class TSGraduation implements Serializable {

    @Id
    @GeneratedValue(generator = "idGenerator")
    @GenericGenerator(name = "idGenerator",strategy = "uuid")
    @Column(length = 32)
    private String id;

    /**
     * 城市
     */
    @Column(name = "city",length = 50)
    private String city;

    /**
     * 公司名
     */
    @Column(name = "company",length = 100)
    private String company;

    /**
     * 公司地址
     */
    @Column(name = "company_address",length = 200)
    private String companyAddress;

    /**
     * 公司电话
     */
    @Column(name = "company_phone",length = 20)
    private String companyPhone;

    /**
     * 从事行业
     */
    @Column(name = "industry",length = 20)
    private String industry;

    /**
     * 所属地区
     */
    @Column(name = "area",length = 100)
    private String area;

    /**
     * 入职时间
     */
    @Column(name = "entry_time",length = 20)
    private String entryTime;

    /**
     * 薪资
     */
    @Column(name = "salary")
    private Integer salary;

    /**
     * 是否有五险一金
     */
    @Column(name = "has_insurance_and_housing_fund",length = 5)
    private String hasInsuranceAndHousingFund;

    /**
     * 关联学生ID
     */
    @OneToOne(cascade = CascadeType.ALL)//student 是关系的维护端,当删除学生的时候,会级联删除去向信息
    @JoinColumn(name = "student_id",referencedColumnName = "id")//去向中的student_id字段参考学生表中的ID字段
    private TSStudent student;
}

  分页服务接口实现:

@Override
    public Page<TSGraduation> search(TSGraduation graduation, AbstractPageRequest pageable) {
        return graduationRepository.findAll(new Specification<TSGraduation>() {
            @Override
            public Predicate toPredicate(Root<TSGraduation> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {

                List<Predicate> list = new ArrayList<>();
                
                //获取学生属性,查询条件
                Join<Object, TSStudent> student = root.join("student", JoinType.LEFT);

                if (graduation.getStudent() != null && !StringUtils.isEmpty(graduation.getStudent().getClazz())){
                    list.add(criteriaBuilder.like(student.get("clazz").as(String.class),"%" + graduation.getStudent().getClazz() + "%"));
                }
                if (graduation.getStudent() != null && !StringUtils.isEmpty(graduation.getStudent().getName())){
                    list.add(criteriaBuilder.like(student.get("name").as(String.class),"%" + graduation.getStudent().getName() + "%"));
                }

                if (graduation.getStudent() != null && !StringUtils.isEmpty(graduation.getStudent().getSex())){
                    list.add(criteriaBuilder.equal(student.get("sex").as(String.class),graduation.getStudent().getSex()));
                }

                if (graduation.getStudent() != null && !StringUtils.isEmpty(graduation.getStudent().getGraduation())){
                    list.add(criteriaBuilder.equal(student.get("graduation").as(String.class), graduation.getStudent().getGraduation() ));
                }

                if (!StringUtils.isEmpty(graduation.getCompany())){
                    list.add(criteriaBuilder.like(root.get("company"),"%" + graduation.getCompany()+ "%"));
                }

                if (!StringUtils.isEmpty(graduation.getIndustry())){
                    list.add(criteriaBuilder.like(root.get("industry"),"%" + graduation.getIndustry()+ "%"));
                }

                if (!StringUtils.isEmpty(graduation.getArea())){
                    list.add(criteriaBuilder.like(root.get("area"),"%" + graduation.getArea()+ "%"));
                }

                if (!StringUtils.isEmpty(graduation.getSalary())){
                    list.add(criteriaBuilder.equal(root.get("salary"),graduation.getSalary()));
                }

                if (!StringUtils.isEmpty(graduation.getHasInsuranceAndHousingFund())){
                    list.add(criteriaBuilder.equal(root.get("hasInsuranceAndHousingFund"),graduation.getHasInsuranceAndHousingFund()));
                }

                return criteriaBuilder.and(list.toArray(new Predicate[list.size()]));
            }
        },pageable);
    }

  参考:https://blog.csdn.net/qq_36289377/article/details/80761103

posted @ 2019-05-07 23:10  xfma  阅读(4660)  评论(0编辑  收藏  举报