https://beanbroker.github.io/2019/07/13/Java/java_querydsl_gradle4-1/
5๋ฒ์ ผ๊ณผ 4๋ฒ์ ผ์ ์ฐจ์ด๋ฅผ ์ถํ ๋ธ๋ก๊ทธ์ ๊ณต์ ํ ์์ !
ํ์ฌ์์ gradle 4๋ฒ์ ผ์ ์ฌ์ฉํจ์ ์ธ์ง!
build.gradle์์ ๊ฐ์ฅ ์ค์ํ๊ฒ ๋ด์ผํ๋ ๋ถ๋ถ์ // querydsl ์ ์ฉ ์ด๋ผ ์ ํ์๋ ๋ถ๋ถ
build.gradle (๋ฉ์ด๋ธ์ผ ๊ฒฝ์ฐ ๋ค๋ฅธ๋ฐ๊ณณ์์ ์ฐพ์์....ํ์๊ธธ..)
build.gradle
buildscript {
ext {
springBootVersion = '2.1.6.RELEASE'
querydslPluginVersion = '1.0.10'
}
repositories {
mavenCentral()
maven { url "https://plugins.gradle.org/m2/" } // plugin ์ ์ฅ์
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath("gradle.plugin.com.ewerk.gradle.plugins:querydsl-plugin:${querydslPluginVersion}")
}
}
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
group = 'com.beanbroker'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
}
dependencies {
compile("com.querydsl:querydsl-jpa") // querydsl
compile("com.querydsl:querydsl-apt") // querydsl
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile('org.springframework.boot:spring-boot-starter-web')
// runtimeOnly('com.h2database:h2')
runtimeOnly 'mysql:mysql-connector-java'
compile('org.projectlombok:lombok')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
// querydsl ์ ์ฉ
apply plugin: "com.ewerk.gradle.plugins.querydsl"
def querydslSrcDir = 'src/main/generated'
querydsl {
library = "com.querydsl:querydsl-apt"
jpa = true
querydslSourcesDir = querydslSrcDir
}
sourceSets {
main {
java {
srcDirs = ['src/main/java', querydslSrcDir]
}
}
}
}
def querydslSrcDir = 'src/main/generated'
querydsl {
library = "com.querydsl:querydsl-apt"
jpa = true
querydslSourcesDir = querydslSrcDir
}
compileQuerydsl{
options.annotationProcessorPath = configurations.querydsl
}
configurations {
querydsl.extendsFrom compileClasspath
}
sourceSets {
main {
java {
srcDirs = ['src/main/java', querydslSrcDir]
}
}
}
application.yml
spring:
datasource:
url: jdbc:mysql://localhost:3306/study
username: root
password: password
user.sql
CREATE TABLE `users` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`age` int(11) NOT NULL,
`name` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`user_id` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
UserEntity.class
@Data
@Entity(name = "users")
public class UserEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private String userId;
private String name;
private int age;
@Builder
public UserEntity(String userId, String name, int age){
this.userId = userId;
this.name = name;
this.age = age;
}
}
๋ฐ๋์ ์ฝ๊ณ ์ ๋ ํฌ๊ตฌ์ฑ์ด ์๋์ ๊ฐ์ด ์งํ๋๋์ง!
์๋์ ์ค๋ช ์ ์ฝ์ด์ผํ๋์ด์ ! ( you must )
you must first define a fragment interface and an implementation for the custom functionality, as shown in the following example:
์ฐธ๊ณ ํ๋ฉด ์ข์ ์ฌํญ https://spring.io/blog/2011/04/26/advanced-spring-data-jpa-specifications-and-querydsl
UserRepository.java
import com.beanbroker.sample.api.user.entity.UserEntity;
import org.springframework.data.jpa.repository.JpaRepository;
public interface UserRepository extends
JpaRepository<UserEntity, Long>,
// QuerydslPredicateExecutor<UserEntity>,
UserRepositoryCustom {
}
UserRepositoryCustom.java
import com.beanbroker.sample.api.user.entity.UserEntity;
public interface UserRepositoryCustom {
UserEntity getByUserId(String userId);
}
UserRepositoryImpl.java
import com.beanbroker.sample.api.user.entity.QUserEntity;
import com.beanbroker.sample.api.user.entity.UserEntity;
import org.springframework.data.jpa.repository.support.QuerydslRepositorySupport;
public class UserRepositoryImpl extends QuerydslRepositorySupport
implements UserRepositoryCustom {
private static final QUserEntity table = QUserEntity.userEntity;
/**
* Creates a new {@link QuerydslRepositorySupport} instance for the given domain type.
*
* @param domainClass must not be {@literal null}.
*/
public UserRepositoryImpl() {
super(UserEntity.class);
}
@Override
public UserEntity getByUserId(String userId) {
return from(table)
.where(table.userId.eq(userId))
.fetchOne();
}
}
์๋ฌ ์ฒ๋ฆฌ๋ฐ ์ค๋ณต์ฒ๋ฆฌ ์๋์ด์์ ๊ทธ๋ฅ ์ฐ๋ ๋ฐฉ๋ฒ๋ง ์๊ณ ์์์
UserService.java
import com.beanbroker.sample.api.user.domain.UserInfo;
import com.beanbroker.sample.api.user.entity.UserEntity;
import com.beanbroker.sample.api.user.repository.UserRepository;
import javassist.NotFoundException;
import org.springframework.stereotype.Service;
import java.util.Optional;
@Service
public class UserService {
private final UserRepository userRepository;
public UserService(UserRepository userRepository) {
this.userRepository = userRepository;
}
public void createUser(){
userRepository.save(
new UserEntity("beanbroker", "pkj", 32)
);
}
public UserEntity getUserId(String userId) throws NotFoundException {
Optional<UserEntity> userEntity = Optional.ofNullable(userRepository.getByUserId(userId));
if(!userEntity.isPresent()){
throw new NotFoundException("Not Found");
}
UserInfo userInfo = new UserInfo();
userInfo.setUserAge(userEntity.get().getAge());
userInfo.setUserName(userEntity.get().getName());
return userEntity.get();
}
}
UserInfo.java
entity์ client์๊ฒ ๊ฐ response๊ฐ ์๋ฐ๋ก๋ฐ๋ก ๋๊ฐ์ผํ ๊ฐ๋ผ๋ ๊ณ ๋ฏผ์ ๊ผญํ์...
public class UserInfo {
private String userName;
private int userAge;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public int getUserAge() {
return userAge;
}
public void setUserAge(int userAge) {
this.userAge = userAge;
}
}
UserController.java
@RestController
public class UserController {
private final UserService userService;
public UserController(UserService userService) {
this.userService = userService;
}
@GetMapping("/user")
public UserEntity testUser() throws NotFoundException {
userService.createUser();
return userService.getUserId("beanbroker");
}
}
์ฝ์์ด ์์ผ๋ฏ๋ก ๋น ๋ฅด๊ฒ ํํ ๋ค์์ฅ์์ github๊ณผ ๋ค๋ฅธ ๋ถ๋ถ๋ค ๋์์ธํ๊ฒ
2019๋ 7์ 17์ผ ๊นํ ์ถ๊ฐ
git source ๋งํฌ
git source ๋งํฌ
kotlin_version ๋งํฌ
PREDICATOR๊ฐ ์ํ์ํ ์ง๋ ๋์ ์ฟผ๋ฆฌ๋ฅผ ๋งค๋ฒ๋งค๋ฒ function์ผ๋ก ์ถ๊ฐํ์ฌ ์ธ๋ฐ์๋ ๋ฉ์๋ ์ถ๊ฐ๋ฅผ ๋ฐฉ์ง
์ค์๋ถ๋ถ ๋งค์ฐ ์ค์
๊ดํ ์ดํํฐ๋ธ์๋ฐ์์ ๋น๋๋ฅผ ์จ๋ผ๋ผ๊ณ ํ๋๊ฒ์ด ์๋!
UserPredicator.java
package com.beanbroker.sample.api.user.service;
import com.beanbroker.sample.api.user.entity.QUserEntity;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.types.Predicate;
import org.springframework.util.StringUtils;
public class UserPredicator {
private static final QUserEntity table = QUserEntity.userEntity;
private BooleanBuilder builder = new BooleanBuilder();
public UserPredicator userId(String userId){
if(userId != null){
builder.and(table.userId.eq(userId));
}
return this;
}
public UserPredicator name(String name){
if(name != null){
builder.and(table.name.eq(name));
}
return this;
}
public UserPredicator age(int age){
if(age > 0){
builder.and(table.age.eq(age));
}
return this;
}
public Predicate values(){
return builder.getValue();
}
}
์ฐธ์ฌ์ฃ ์?
UserRepositoryCustom.java
import com.beanbroker.sample.api.user.entity.UserEntity;
import com.querydsl.core.types.Predicate;
public interface UserRepositoryCustom {
UserEntity getByUserId(String userId);
UserEntity getUserInfoWithPredicator(Predicate predicate);
}
UserRepositoryImpl.java
public class UserRepositoryImpl extends QuerydslRepositorySupport
implements UserRepositoryCustom {
private static final QUserEntity table = QUserEntity.userEntity;
/**
* Creates a new {@link QuerydslRepositorySupport} instance for the given domain type.
*
* @param domainClass must not be {@literal null}.
*/
public UserRepositoryImpl() {
super(UserEntity.class);
}
@Override
public UserEntity getByUserId(String userId) {
return from(table)
.where(table.userId.eq(userId))
.fetchOne();
}
@Override
public UserEntity getUserInfoWithPredicator(Predicate userPredicator) {
return from(table)
.where(userPredicator)
.fetchOne();
}
}
์๋๊ฐ ํต์ฌ์ฝ๋
public UserEntity getUserInfoWithPredicator(
String userId,
String name,
int age
){
return userRepository.getUserInfoWithPredicator(
setUserQuery(userId, name, age)
);
}
private Predicate setUserQuery( String userId,
String name,
int age){
return new UserPredicator()
.userId(userId)
.name(name)
.age(age)
.values();
}
UserEntity test1 = userService.getUserInfoWithPredicator(
"beanbroker",
null,
0
);
System.out.println(test1.toString());
UserEntity test2 = userService.getUserInfoWithPredicator(
"beanbroker",
"pkj",
0
);
System.out.println(test2.toString());
UserEntity test3 = userService.getUserInfoWithPredicator(
null,
null,
32
);
System.out.println(test3.toString());
์ ์์ฆ์ ์ ์ ๋ ๊ฒ์จ์ผํ๋์ง ์ค๋ช ์ ๋๊ฒ ๊ท์ฐฎ๋ค.... ํ์ฉ๋ฐฉ๋ฒ์ ๋ค์ํ๋! ์์ฌ์ฉํฉ์๋น