Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/sarakhild/querydslandprojections

This project simplify QueryDSl and Procjection concepts.
https://github.com/sarakhild/querydslandprojections

java projections querydsl relational-databases springboot springframework

Last synced: about 2 months ago
JSON representation

This project simplify QueryDSl and Procjection concepts.

Awesome Lists containing this project

README

        

# Project by Using QueryDSL And Projection Concepts

## Overview
I coded a project by using SpringBoot simplify QueryDSl concept that is a framework which enables the construction of type-safe SQL, instead of writing queries as inline strings or externalizing them into XML files they are constructed via a fluent API, besid Projection concept is defined as taking a vertical subset from the columns of a single table that retains the unique rows. This kind of SELECT statement returns some of the columns and all the rows in a table.



## Usages
- SpringBoot
- QueryDSL
- Procjection
- MySQL

## Architecture of the Project

### 1- src folders
- Controllers folder
- Models folder
- Projections
- Services folder
- Repositories folder

### 2-Maven pom.xml

```


org.springframework.boot
spring-boot-starter


org.springframework.boot
spring-boot-starter-web


org.springframework.boot
spring-boot-starter-test
test


mysql
mysql-connector-java
runtime
8.0.30


org.springframework.boot
spring-boot-starter-data-jpa


org.hibernate
hibernate-core
${hibernate.version}


com.querydsl
querydsl-apt
${querydsl.version}
jakarta


com.querydsl
querydsl-jpa
${querydsl.version}
jakarta


com.querydsl
querydsl-jpa-codegen
${querydsl.version}
jakarta

```

### 3-Application.properties.yml

```
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://localhost:3312/db
spring.datasource.username=db
spring.datasource.password=nFLhPPKOnkW1FA1e
spring.jpa.show-sql=true

```
## Let's Start :mechanical_arm:
### • Query Results of Employee

##### :pencil2: `This query compares employee name that is sending with employees name which stored in database and then get employee information.`

###### Code :computer:


```
@Override
public Employee findByName(String employeeName) {
var entityPath = QEmployee.employee;
var query = new JPAQuery<>(this.entityManager);
var result = query.select(entityPath).from(entityPath).where(entityPath.employeeName.eq(employeeName))
.fetchOne();
return result;
}
```


###### Result :star_struck:


Find By Name

---


##### :pencil2: `This query gets job names infrequently.`

###### Code :computer:


```
@Override
public List getUniqueJobName() {
var entityPath = QEmployee.employee;
var query = new JPAQuery<>(this.entityManager);
var result = query.select(entityPath.jopName).distinct().from(entityPath).fetch();
return result;

}
```


###### Result :star_struck:


Get Unique Job Name

---


##### :pencil2: `This query multiply current salary of employee with 15% and show employee names and new salary increasement.`

###### Code :computer:

```
@Override
public List getEmployeeNameWithIncreasedSalary() {
var entityPath = QEmployee.employee;
var query = new JPAQuery<>(this.entityManager);
var result = query
.select(Projections.bean(EmployeeProjection.class,
entityPath.employeeName.as("employeeName"),
entityPath.salary.multiply(Expressions.ONE.multiply(1.350))
.as("salary"))) // Increase 15%
.from(entityPath).fetch();
// --------------------------another way----------------------------
// var result = query.select(entityPath.employeeName,
// entityPath.salary).from(entityPath)
// .fetch().stream()
// .map(x -> new EmployeeProjection(x.get(entityPath.employeeName),
// x.get(entityPath.salary)))
// .collect(Collectors.toList());

return result;
}
```


###### Result :star_struck:


Get Employee Name With Increased Salary




Get Employee Name With Increased Salary

---


##### :pencil2: `This query gets employees who dosen't belong to this department number by comparing with other departments number which is sending and bringing them all, except employees who's under this department number.`

###### Code :computer:


```
@Override
public List getEmployeeNotBelongToDepartment(int departmentNo) {
var entityPath = QEmployee.employee;
var query = new JPAQuery<>(this.entityManager);
var result = query.select(entityPath).from(entityPath)
.where(entityPath.department.departmentNo.notIn(departmentNo)).fetch();
return result;
}
```


###### Result :star_struck:


Get Employee Not Belong To Department




Get Employee Not Belong To Department

---


##### :pencil2: `This query brings employees information whoe's been hired before year that's sending.`

###### Code :computer:


```
@Override
public List getEmployeeWhoJoinedBefore(int year) {

var entityPath = QEmployee.employee;
var query = new JPAQuery<>(this.entityManager);
var result = query.select(entityPath).from(entityPath)
.where(entityPath.hireDate.year().lt(year)).fetch();
return result;

}
```


##### Result :star_struck:


Get Employee Who's Joined Before




Employee Who's Joined Before

---


##### :pencil2: `This query fetches employee number, employee name, department number and department name where department number on employee equal department number on department.`

###### Code :computer:


```
@Override
public List getEmployeeWithDepartment() {
var employeeEntityPath = QEmployee.employee;
var departmentEntityPath = QDepartment.department;
var query = new JPAQuery<>(this.entityManager);

var result = query
.select(Projections.bean(EmployeeProjection.class,
employeeEntityPath.employeeNo.as("employeeNo"),
employeeEntityPath.employeeName.as("employeeName"),
departmentEntityPath.departmentNo.as("departmentNo"),
departmentEntityPath.departmentName.as("departmentName")))
.innerJoin(departmentEntityPath)
.on(employeeEntityPath.department.departmentNo.eq(departmentEntityPath.departmentNo));

return result.fetch();
}

```


###### Result :star_struck:


Get Employee With Department




Get Employee With Department




Get Employee With Department

---


##### :pencil2: `This query calculates the employee average salary and then fetches employee name and job name which it's greater than this salary average.`

###### Code :computer:


```
@Override
public List getEmployeeWhoSalaryIsGreaterThanTheAverageSalaryOfAllEmployees() {
var entityPath = QEmployee.employee;
var query = new JPAQuery<>(this.entityManager);
var subQuery = query.select(entityPath.salary.avg()).from(entityPath).fetchOne();
var result = query
.select(Projections.bean(EmployeeProjection.class,
entityPath.employeeName.as("employeeName"),
entityPath.jopName.as("jopName"), entityPath.salary.as("salary")))
.from(entityPath).where(entityPath.salary.gt(subQuery)).fetch();

return result;
}

```


###### Result :star_struck:


Get Employee Who's Salary Is Greater Than The Average Salary Of All Employees




Get Employee Who's Salary Is Greater Than The Average Salary Of All Employees

---


##### :pencil2: `This query counts every employees under each manager, where compare employee's manager id with employee number then arrange identical employee number and name without repetition, then show employee number, name, job name, and how many employees under this manager.`

###### Code :computer:


```
@Override
public List countAllEmployeesUnderEachManager() {
var entityPathManager = QEmployee.employee;
var entityPathEmployee = QEmployee.employee;
var query = new JPAQuery<>(this.entityManager);
var result = query
.select(Projections.bean(EmployeeProjection.class,
entityPathManager.employeeNo.as("employeeNo"),
entityPathManager.employeeName.as("employeeName"),
entityPathManager.jopName.as("jopName"),
entityPathManager.managerId.as("managerId"),
entityPathEmployee.count().as("count")))
.from(entityPathEmployee)
.join(entityPathManager)
.on(entityPathEmployee.managerId.eq(entityPathManager.employeeNo))
.groupBy(entityPathManager.employeeName, entityPathManager.employeeNo, entityPathManager.jopName)
.fetch();
return result;
}

```


###### Result :star_struck:


Count All Employees Under Each Manager




Count All Employees Under Each Manager

---


##### :pencil2: `This query fetches employee number based on manager name that sending and then comparing this employee number which cames with managerId of employees and then show employees information under this manager.`

###### Code :computer:


```
@Override
public List getEmployeesWhoseUnderManagerName(String managerName) {
var entityPathManager = QEmployee.employee;
var entityPathEmployee = QEmployee.employee;
var findIdManagerQuery = new JPAQuery<>(this.entityManager);
var findEmployeeQuery = new JPAQuery<>(this.entityManager);

var subQuery = findIdManagerQuery.select(entityPathManager.employeeNo).from(entityPathManager)
.where(entityPathManager.employeeName.like("%" + managerName + "%")).fetchOne();

var result = findEmployeeQuery.select(entityPathEmployee).from(entityPathEmployee)
.where(entityPathEmployee.managerId.eq(subQuery));

return result.fetch();
}

```


###### Result :star_struck:


Get Employees Whose Under Manager Name




---
---
---


### • Query Results of Department

##### :pencil2: `This query compares department name that is sending with department name which stored in database and then get department information.`

###### Code :computer:


```
@Override
public Department findByName(String departmentName) {

var entityPath = QDepartment.department;
var query = new JPAQuery<>(this.entityManager);
var result = query.select(entityPath).from(entityPath).where(entityPath.departmentName.eq(departmentName))
.fetchOne();

return result;

}
```


###### Result :star_struck:


Find By Name


---


##### :pencil2: `This query takes department number that is sending then filtering it with all department number that's stored in database and show department number, department name except this department number.`

###### Code :computer:


```
@Override
public List findAllDepartmentsThatIsNotIncludeThisDepartmentNo(int departmentNo) {
var entityPath = QDepartment.department;
var query = new JPAQuery<>(this.entityManager);
var result = query
.select(Projections.bean(DepartmentProjection.class, entityPath.departmentNo.as("departmentNo"),
entityPath.departmentName.as("departmentName")))
.where(entityPath.departmentNo.notIn(departmentNo)).from(entityPath).fetch();

return result;

}
```


###### Result :star_struck:


Find All Departments That Is Not Include This DepartmentNo


---


##### :pencil2: `This query brings departments which have no employee hired yet, where to get department number of employee in employee table and filter the result if the values that aren't mentioned as part of the department number of department table then get this department infomation.`

###### Code :computer:


```
@Override
public List findAllDepartmentsThatEmployeesNotIncluded() {
var departmentEntityPath = QDepartment.department;
var employeeEntityPath = QEmployee.employee;
var query = new JPAQuery<>(this.entityManager);
var subQuery = query.select(employeeEntityPath.department.departmentNo).from(employeeEntityPath).fetch();
var result = query.select(departmentEntityPath).from(departmentEntityPath)
.where(departmentEntityPath.departmentNo.notIn(subQuery)).fetch();

return result;
}
```


###### Result :star_struck:


Find All Departments That Employees Not Included


---



##### :pencil2: `This query gets departments which have at lest two employees, that compares department number that's in department table with department number of employee in employee table then arrange identical departments number, name without repetition, then count how many employee in this department which will be greater than or equal two.`

###### Code :computer:


```
@Override
public List findDepartmentsHaveAtLeastTwoEmployees() {
var entityPath = QDepartment.department;
var employeeEntityPath = QEmployee.employee;
var query = new JPAQuery<>(this.entityManager);
var result = query
.select(Projections.bean(DepartmentProjection.class, entityPath.departmentNo.as("departmentNo"),
entityPath.departmentName.as("departmentName")))
.from(entityPath, employeeEntityPath)
.where(entityPath.departmentNo.eq(employeeEntityPath.department.departmentNo))
.groupBy(entityPath.departmentName,entityPath.departmentNo)
.having(entityPath.count().goe(2))
.fetch();

return result;
}

```


###### Result :star_struck:


Find Departments Have At Least Two Employees


---

### Good Luck