(一)QueryDsl简介
(二)引入QueryDsl
2.1 依赖
<dependency> <groupId>com.querydsl</groupId> <artifactId>querydsl-jpa</artifactId> </dependency> <dependency> <groupId>com.querydsl</groupId> <artifactId>querydsl-apt</artifactId> <scope>provided</scope> </dependency>
2.2 plugin
<plugin> <groupId>com.mysema.maven</groupId> <artifactId>apt-maven-plugin</artifactId> <version>1.1.3</version> <executions> <execution> <goals> <goal>process</goal> </goals> <configuration> <outputDirectory>target/generated-sources/java</outputDirectory> <processor>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor> </configuration> </execution> </executions> </plugin>
JPAAnnotationProcessor 查找带有 javax.persistence.Entity 注解的领域类型并为其生成查询类型。简单来说,比如有个Student的entity,运行clean install 会在项目的target/generated-sources/java 目录下将生成QStudent,这个QStudent就是查询类型类。
(三)实体 Student类
@Table(name = "student") @Entity public class Student { @Id @GeneratedValue( strategy = GenerationType.IDENTITY ) private long id; private String name; private int age; private long schoolId; public long getId() { return id; } public void setId(long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public long getSchoolId() { return schoolId; } public void setSchoolId(Long schoolId) { this.schoolId = schoolId; } @Override public String toString() { return "Student{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + ", schoolId=" + schoolId + '}'; } }
运行mvn clean install后生成的QStudent
@Generated("com.querydsl.codegen.EntitySerializer") public class QStudent extends EntityPathBase<Student> { private static final long serialVersionUID = -5814978L; public static final QStudent student = new QStudent("student"); public final NumberPath<Integer> age = createNumber("age", Integer.class); public final NumberPath<Long> id = createNumber("id", Long.class); public final StringPath name = createString("name"); public final NumberPath<Long> schoolId = createNumber("schoolId", Long.class); public QStudent(String variable) { super(Student.class, forVariable(variable)); } public QStudent(Path<? extends Student> path) { super(path.getType(), path.getMetadata()); } public QStudent(PathMetadata metadata) { super(Student.class, metadata); } }
(四)查询
4.0 QueryDslTest测试类
@RunWith(SpringRunner.class) @SpringBootTest public class QueryDslTest { @Autowired @PersistenceContext private EntityManager entityManager; private JPAQueryFactory queryFactory; @Before public void init() { queryFactory = new JPAQueryFactory(entityManager); } }
4.1 简单查询
@Test public void selectStudent(){ QStudent qStudent = QStudent.student; List<Student> students = queryFactory.selectFrom(qStudent).fetch(); System.out.println(students); }
4.2 单表条件查询
@Test
public void selectStudentWithWhere() {
QStudent qStudent = QStudent.student;
//单表单条件查询
List<Student> students = queryFactory.selectFrom(qStudent).where(qStudent.age.goe(2)).fetch();
//单表多条件and查询
List<Student> studentList = queryFactory.selectFrom(qStudent).where(qStudent.age.goe(2).and(qStudent.name.eq("vincent"))).fetch();
//单表多条件or查询
List<Student> list = queryFactory.selectFrom(qStudent).where(qStudent.age.goe(20).or(qStudent.name.eq("vincent"))).fetch();
//单表排序分页查询
List<Student> list1 = queryFactory.selectFrom(qStudent).offset(1).limit(3).orderBy(qStudent.id.desc()).fetch();
System.out.println(students);
System.out.println(studentList);
System.out.println(list);
System.out.println(list1);
}
4.3 多表查询
@Test public void selectStudent1() { QStudent qStudent = QStudent.student; QSchool qSchool = QSchool.school; List<Student> students = queryFactory.select(qStudent).from(qSchool, qStudent).where(qSchool.id.eq(qStudent.schoolId)).fetch(); System.out.println(students); }
以上查询的结果只是student的表的值,比如我现在关心学校名称,那么这样子就不能得到,用下面的做法可以得到你想要的值
@Test public void selectStudent2() { QStudent qStudent = QStudent.student; QSchool qSchool = QSchool.school; List<Tuple> students = queryFactory.select(qStudent.age, qStudent.name, qSchool.name).from(qSchool, qStudent).where(qSchool.id.eq(qStudent.schoolId)).fetch(); List<Map<String, Object>> maps = students.stream().map(tuple -> { Map<String, Object> map = new HashMap<>(); map.put("studentName", tuple.get(qStudent.name)); map.put("age", tuple.get(qStudent.age)); map.put("schoolName", tuple.get(qSchool.name)); return map; }).collect(Collectors.toList()); System.out.println(maps); }
4.4 leftjoin查询