業務場景:大學生畢業后統計畢業去向。學生實體和畢業去向實體一對一關系。
實體:
@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
