SpringBoot 환경 에서 QueryDSL - JPA 사용
12157 단어 JPAQueryFactoryquerydsl자바
4.0.0
com.xiaohang.springio
spring-data-jpa
1.0-SNAPSHOT
jar
org.springframework.boot
spring-boot-parent
2.1.0.RELEASE
UTF-8
1.7
1.7
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
org.springframework.boot
spring-boot-starter-data-jpa
org.springframework.boot
spring-boot-starter-data-rest
com.alibaba
druid-spring-boot-starter
1.1.10
mysql
mysql-connector-java
5.1.46
org.projectlombok
lombok
1.16.18
com.querydsl
querydsl-jpa
com.querydsl
querydsl-apt
provided
org.springframework.boot
spring-boot-maven-plugin
org.springframework
springloaded
1.2.5.RELEASE
com.mysema.maven
apt-maven-plugin
1.1.3
process
target/generated-sources/java
com.querydsl.apt.jpa.JPAAnnotationProcessor
2. application. yml 프로필
#
spring.datasource.type: com.alibaba.druid.pool.DruidDataSource
spring.datasource.username: root
spring.datasource.password: root
spring.datasource.url: jdbc:mysql://192.168.56.1:3306/test01?useUnicode=true&characterEncoding=UTF-8&useSSL=false
spring.datasource.driver-class-name: com.mysql.jdbc.Driver
#
spring.jpa.properties.hibernate.dialect: org.hibernate.dialect.MySQL57Dialect
spring.jpa.show-sql: true
spring.jpa.hibernate.ddl-auto: update
#
spring.jpa.database-platform: mysql
# JPA
spring.jpa.database: mysql
#
spring.jpa.open-in-view: false
3. 실체 류
Customer.java
package com.xiaohang.springio.jpastudy.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.*;
@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity(name = "t_customer")
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "customer_name")
private String name;
@Column(name = "customer_part")
private String part;
}
Employees.java
package com.xiaohang.springio.jpastudy.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.*;
@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity(name = "t_employee")
public class Employees {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "employee_name")
private String name;
@Column(name = "employee_part")
private String part;
}
4、Repository
package com.xiaohang.springio.jpastudy.repository;
import com.xiaohang.springio.jpastudy.entity.Employees;
import org.springframework.data.jpa.repository.JpaRepository;
public interface EmployeesRepository extends JpaRepository {
}
package com.xiaohang.springio.jpastudy.repository;
import com.xiaohang.springio.jpastudy.entity.Customer;
import org.springframework.data.jpa.repository.JpaRepository;
public interface CustomerRepository extends JpaRepository {
}
5. JPA Query Factory 류 의 사용 중점
package com.xiaohang.springio.jpastudy.service;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.QueryResults;
import com.querydsl.core.types.Projections;
import com.querydsl.jpa.JPAExpressions;
import com.querydsl.jpa.impl.JPAQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;
import com.xiaohang.springio.jpastudy.entity.Customer;
import com.xiaohang.springio.jpastudy.entity.Employees;
import com.xiaohang.springio.jpastudy.entity.QCustomer;
import com.xiaohang.springio.jpastudy.entity.QEmployees;
import com.xiaohang.springio.jpastudy.repository.CustomerRepository;
import com.xiaohang.springio.jpastudy.repository.EmployeesRepository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Bean;
import org.springframework.test.context.junit4.SpringRunner;
import javax.persistence.EntityManager;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest
public class BookServiceTest {
// JPAQueryFactory
@Bean
@Autowired
public JPAQueryFactory jpaQuery(EntityManager entityManager) {
return new JPAQueryFactory(entityManager);
}
@Autowired
private JPAQueryFactory jpaQueryFactory;
@Autowired
private EmployeesRepository employeesRepository;
@Autowired
private CustomerRepository customerRepository;
@org.junit.Test
public void jpaQuery() {
Employees employees = new Employees();
employees.setName(" ");
employees.setPart(" ");
employeesRepository.saveAndFlush(employees);
}
@org.junit.Test
public void updateBook() {
Customer customer = new Customer();
customer.setName(" ");
customer.setPart(" ");
customerRepository.saveAndFlush(customer);
}
/**
*
*/
@org.junit.Test
public void findAllAuthors() {
QCustomer qc = QCustomer.customer;
QEmployees qe = QEmployees.employees;
//
List employeesList = jpaQueryFactory.select(Projections.constructor(Employees.class, qe.id, qe.name, qe.part)).from(qe).innerJoin(qc).on(qc.id.eq(qe.id)).fetch();
employeesList.forEach(e -> {
System.out.println(e.getName());
});
//
List employeesList1 = jpaQueryFactory.select(Projections.constructor(Employees.class, qe.id, qe.name, qe.part)).from(qe).leftJoin(qc).on(qc.id.eq(qe.id)).fetch();
employeesList1.forEach(employees -> {
System.out.println(employees.getName());
});
//
List employeesList2 = jpaQueryFactory.select(Projections.constructor(Employees.class, qe.id, qe.name, qe.part)).from(qe).rightJoin(qc).on(qc.id.eq(qe.id)).fetch();
employeesList2.forEach(employees -> {
System.out.println(employees.getName());
});
//
List employeesList3 = jpaQueryFactory.select(Projections.constructor(Employees.class, qe.id, qe.name, qe.part)).from(qe).leftJoin(qc).on(qc.id.eq(qe.id)).where(qc.id.isNull()).fetch();
employeesList3.forEach(employees -> {
System.out.println(employees.getName());
});
}
/**
*
*/
@org.junit.Test
public void findAll() {
// select customer0_.id as id1_1_, customer0_.customer_name as customer2_1_, customer0_.customer_part as customer3_1_ from t_customer customer0_ where (customer0_.customer_name like ? escape '!' or customer0_.customer_name=?) and (customer0_.id between ? and ?)
QCustomer qc = QCustomer.customer;
BooleanBuilder builder = new BooleanBuilder();
builder.and(qc.name.like('%' + " " + '%'));
builder.or(qc.name.eq(" "));
builder.and(qc.id.between(3, 5));
List customerList = jpaQueryFactory.selectFrom(qc).where(builder).fetch();
customerList.forEach(customer -> {
System.out.println(customer.getName());
});
}
/**
*
*/
@org.junit.Test
public void findAllDto() {
QCustomer qc = QCustomer.customer;
QEmployees qe = QEmployees.employees;
List customerList = jpaQueryFactory.selectFrom(qc).where(qc.id.eq(JPAExpressions.select(qe.id).from(qe).where(qe.name.eq(" ")))).fetch();
customerList.forEach(customer -> {
System.out.println(customer.getName());
});
}
/**
*
*/
@Test
public void testDesc() {
QCustomer qc = QCustomer.customer;
List customerList = jpaQueryFactory.selectFrom(qc).orderBy(qc.id.desc()).fetch();
customerList.forEach(customer -> {
System.out.println(customer.getName());
});
}
/**
*
*/
@Test
public void testPage() {
QCustomer qc = QCustomer.customer;
QEmployees qe = QEmployees.employees;
//
JPAQuery employeesJPAQuery = jpaQueryFactory.selectFrom(qe);
// offset
List employeesList = employeesJPAQuery.offset(3).limit(2).fetch();
employeesList.forEach(employees -> {
System.out.println(employees);
});
//
Long total = employeesJPAQuery.fetchCount();
QueryResults customerQueryResults = jpaQueryFactory.selectFrom(qc).offset(0).limit(3).fetchResults();
List results = customerQueryResults.getResults();
results.forEach(customer -> {
System.out.println(results);
});
}
}
6. 추천 읽 기 및 참고
https://www.jianshu.com/p/69dcb1b85bbb