본문 바로가기
Spring

Spring 4.2.4 + Querydsl 3.7.1

by 수앙 2016. 2. 22.

# Spring 4.2.4, Querydsl 3.7.1, Hibernate 5.1.0 기준으로 작성

Full 예제는 아니므로 자신의 개발환경에 맞추어 코딩

최소한의 설정과 예제로 작성됨


# Querydsl 3버전은 패키지 경로가 com.mysema.query로 시작하나 Querydsl 4버전에는 com.query으로 시작함

4버전 써도 무방하고 이 글 쓰는 시점의 최신 버전은 4.0.7


# 메이븐

- dependency

<!-- Logback -->

<dependency>

<groupId>ch.qos.logback</groupId>

<artifactId>logback-classic</artifactId>

<version>1.1.3</version>

</dependency>


<!-- Hibernate -->

<dependency>

<groupId>org.hibernate</groupId>

<artifactId>hibernate-entitymanager</artifactId>

<version>5.1.0.Final</version>

</dependency>


<!-- Querydsl -->

<dependency>

<groupId>com.mysema.querydsl</groupId>

<artifactId>querydsl-apt</artifactId>

<version>${querydsl.version}</version>

<scope>provided</scope>

</dependency>

<dependency>

<groupId>com.mysema.querydsl</groupId>

<artifactId>querydsl-jpa</artifactId>

<version>${querydsl.version}</version>

</dependency>

- 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>src/generated</outputDirectory> <!-- 이 위치는 자신의 환경에 맞춤 -->

<processor>com.mysema.query.apt.jpa.JPAAnnotationProcessor</processor>

</configuration>

</execution>

</executions>

</plugin>


# Spring xxx-context.xml

<bean id="dataSource" class="org.apache.tomcat.dbcp.dbcp2.BasicDataSource" destroy-method="close">

<property name="driverClassName" value="${driverClassName}" />

<property name="url" value="${url}" />

<property name="username" value="${username}" />

<property name="password" value="${password}" />

</bean>


<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">

<property name="packagesToScan" value="${packagesToScan}" /> <!-- 스캔 패키지명 -->

<property name="dataSource" ref="dataSource" />

<property name="jpaVendorAdapter">

<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">

<property name="generateDdl" value="true" />

<property name="showSql" value="true" />

</bean>

</property>

</bean>


<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">

<property name="dataSource" ref="dataSource" />

<property name="entityManagerFactory" ref="entityManagerFactory" />

</bean>

<tx:annotation-driven transaction-manager="transactionManager" />


# User domain

대략 아래처럼 domain 클래스는 Table에 대한 Column을 선언하게 되는데

클래스 파일을 저장해서 컴파일이 되면 메이븐 플러그인에 정의했던

outputDirectory 태그의 경로에 QUser 클래스가 자동으로 생성된다.

import java.io.Serializable;


import javax.persistence.Entity;

import javax.persistence.Id;


@Entity

public class User implements Serializable {


private static final long serialVersionUID = xxxL;


@Id

private String host;


public String getHost() {

return host;

}


public void setHost(String host) {

this.host = host;

}


}


# Dao

import java.util.List;


import javax.persistence.EntityManager;

import javax.persistence.PersistenceContext;


import org.springframework.stereotype.Repository;


import com.mysema.query.jpa.impl.JPAQuery;


@Repository

public class UserDao {


@PersistenceContext

private EntityManager entityManager;

public List<User> getUserList() {

JPAQuery query = new JPAQuery(entityManager);

QUser user = QUser.user;

return query.from(user).list(user);

}

}


# Service와 Controller는 보통 스프링 MVC 개발 방식으로 코딩하면 됨

'Spring' 카테고리의 다른 글

Spring 4.3.2 자바 annotation 기반 설정  (0) 2016.09.21
Spring batch 예제  (0) 2016.04.20
Spring WebSocket  (3) 2015.09.13
RestTemplate 사용법  (0) 2015.02.20
Spring quartz JMX 사용방법  (0) 2013.02.15

댓글