SpringBootJPA使⽤Specification多表查询LEFTJOIN 1.Student
package com.ity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.*;
/**
* @author ⼩⽯潭记
* @date 2020/12/12 18:32
* @Description: ${todo}
*/
sql left join 多表连接@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "t_student")
public class Student {
@Id
private Long id;
private String name;
private String mobile;
private Integer age;
@ManyToOne
private Company company;
}
2.Company
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
/
**
* @author ⼩⽯潭记
* @date 2020/12/12 18:36
* @Description: ${todo}
*/
@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "t_company")
public class Company {
@Id
@GeneratedValue
private Long id;
private String name;
private String code;
private String address;
}
3.StudentRepository
package com.pository;
import com.ity.Student;
import org.springframework.pository.JpaSpecificationExecutor;
import org.pository.PagingAndSortingRepository;
import org.springframework.stereotype.Component;
/**
* @author ⼩⽯潭记
* @date 2020/12/12 14:21
* @Description: ${todo}
*/
@Component
public interface StudentRepository extends PagingAndSortingRepository<Student, Integer>, JpaSpecificationExecutor<Student> { }
4.StudentService
import com.ity.PageStudentRequest;
import com.ity.Student;
import com.pository.StudentRepository;
import com.frank.jpaSpecification.specification.StudentSpecification;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
/**
* @author ⼩⽯潭记
* @date 2020/12/12 14:22
* @Description: ${todo}
*/
@Service
public class StudentService {
@Autowired
private StudentRepository studentRepository;
@Autowired
private StudentSpecification studentSpecification;
public Page<Student> getStudentList(PageStudentRequest studentRequest) {
PageRequest pageRequest = PageRequest.PageNumber(), PageSize());        return studentRepository.StudentSpecification(studentRequest), pageRequest);
}
}
5.StudentSpecification
package com.frank.jpaSpecification.specification;
import com.ity.*;
import org.apachemons.lang.StringUtils;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import iteria.Join;
import iteria.JoinType;
import iteria.Predicate;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
/**
* @author ⼩⽯潭记
* @date 2020/12/12 14:27
* @Description: ${todo}
*/
@Component
public class StudentSpecification {
/
**
* root 就是mobile实例  ("name") name是属性名不是数据库字段名
* @param paramRequest
* @return
* */
public Specification<Student> getStudentSpecification(PageStudentRequest paramRequest) {
return (root, criteriaQuery, criteriaBuilder) -> {
// localhost:8080/student?companyName=北京&pageNumber=0&pageSize=5&name=明
/*
// 普通的or
Predicate namePre = criteriaBuilder.("name"), "%" + Name() + "%");
Predicate companyPre = criteriaBuilder.("company").get("name"), "%" + CompanyName() + "%");            (namePre, companyPre);
*/
// 使⽤左连接查询
Join<Student, Company> companyJoin = root.join("company", JoinType.LEFT);
Predicate namePre = criteriaBuilder.("name"), "%" + Name() + "%");
Predicate companyPre = criteriaBuilder.("name"), "%" + CompanyName() + "%");
(namePre, companyPre);
};
}
}
6.PageStudentRequest 请求体
package com.ity;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;
import java.time.LocalDate;
import java.util.List;
/**
* @author ⼩⽯潭记
* @date 2020/12/12 14:26
* @Description: ${todo}
*/
@Data
public class PageStudentRequest {
private Integer pageSize = 10;
private Integer pageNumber = 1;
private String name;
private String companyName;
}
7.StudentController
package com.ller;
import com.ity.PageStudentRequest;
import com.ity.Student;
import com.frank.jpaSpecification.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author ⼩⽯潭记
* @date 2020/12/12 19:02
* @Description: ${todo}
*/
@RestController
@RequestMapping("/student")
public class StudentController {
@Autowired
private StudentService studentService;
@GetMapping
public Page<Student> index(PageStudentRequest pageStudentRequest) {        StudentList(pageStudentRequest);
}
}
8.测试

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。