What is QueryDSL?
Querydsl is an extensive Java framework, which allows for the generation of type-safe queries in a syntax similar to SQL
Why QueryDSL?
- Code Completion
- Type Safety
- No Strings Involved
- Adapts to Refactoring
Example QueryDSLs
query.from(employee).where(employee.name.eq("auhuman"));
query.from(employee).where(employee.age.gt(35)).and(employee.gender.eq(“Male”);
Maven Dependencies
<dependency>
<groupId>com.mysema.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
<version>3.7.4</version>
</dependency>
<dependency>
<groupId>com.mysema.querydsl</groupId>
<artifactId>querydsl-jpa</artifactId>
<version>3.7.4</version>
</dependency>
Maven Build 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.mysema.query.apt.jpa.JPAAnnotationProcessor</processor>
</configuration>
</execution>
</executions>
</plugin>
Entity
@Entity
public class Employee {
@Id
@GeneratedValue
private Long id;
private String firstName, lastName, description;
private Long count;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Long getCount() {
return count;
}
public void setCount(Long count) {
this.count = count;
}
}
public class Employee {
@Id
@GeneratedValue
private Long id;
private String firstName, lastName, description;
private Long count;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Long getCount() {
return count;
}
public void setCount(Long count) {
this.count = count;
}
}
Sample Code
@RequestMapping(value = "/", method = RequestMethod.GET)
@ResponseBodyList<Company> queryDSL(@RequestParam(value = "name", required = false) String name) {
JPAQuery query = new JPAQuery(entityManager);
QCompany company = QCompany.company;
if (name == null) {
return query.from(company).list(company);
} else {
return query.from(company).where(company.name.eq(name)).list(company);
}
}
@RequestMapping(value = "/queryDSLGroupByCount", method = RequestMethod.GET)
@ResponseBody
List<Employee> queryDSLGroupByCount() {
JPAQuery jpaQuery = new JPAQuery(entityManager);
QEmployee employee = QEmployee.employee;
return jpaQuery.from(employee).groupBy(employee.lastName).list(Projections.constructor(
Employee.class,
employee.lastName,
employee.count()
));
}
@RequestMapping(value = "/queryDSLGroupByCount", method = RequestMethod.GET)
@ResponseBody
List<Employee> queryDSLGroupByCount() {
JPAQuery jpaQuery = new JPAQuery(entityManager);
QEmployee employee = QEmployee.employee;
return jpaQuery.from(employee).groupBy(employee.lastName).list(Projections.constructor(
Employee.class,
employee.lastName,
employee.count()
));
}
No comments:
Post a Comment