기억의 습작

기 생성된 DB 테이블을 자동으로 Entity 객체를 생성해 주면서 Queryfactory 객체까지 추가로 생성 본문

Software/Spring

기 생성된 DB 테이블을 자동으로 Entity 객체를 생성해 주면서 Queryfactory 객체까지 추가로 생성

뿌사리다 2019. 9. 1. 14:45

환경

 - Maven

 

 

프로젝트 git 풀 소스

 

 

ppusarida / EntityAutoCreation

기 생성된 DB에서 자동으로 Entity 객체를 생성해주면서 Queryfactory 객체까지 추가로 생성

ppusari.iptime.org:10280

 

소스추가

 

POM.xml  파일에 아래 추가1~추가3 을 적용합니다.

DB 접속 주소와 계정이 연결할 수 없으면 아래 <execution> 에 빨강줄이 생기면서 오류가 발생하는점 참고 바랍니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?xml version="1.0" encoding="UTF-8"?>
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.7.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.ppusari</groupId>
    <artifactId>eac</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>EntityAutoCreation</name>
    <description>Entity auto creation  project for Spring Boot </description>
 
    <properties>
        <java.version>1.8</java.version>
        
        <!-- 추가1 -->
        <querydsl.version>4.2.1</querydsl.version>
<!-- 추가1 -->
 
    </properties>
 
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
 
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        
        <!-- 추가2 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>com.querydsl</groupId>
            <artifactId>querydsl-jpa</artifactId>
        </dependency>                
        <dependency>
            <groupId>com.querydsl</groupId>
            <artifactId>querydsl-sql-spring</artifactId>
            <version>${querydsl.version}</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>6.0.17.Final</version>
        </dependency>
<!-- 추가2 -->
        
    </dependencies>
 
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            
            <!-- 추가3 -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>com.querydsl</groupId>
                <artifactId>querydsl-maven-plugin</artifactId>
                <version>${querydsl.version}</version>
                 <executions>
                         
                     <!-- DB 접속 주소와 계정이 연결할 수 없으면 아래 <execution> 에 빨강줄이 생기면서 오류가 발생한다. --> 
                    <execution>
                        <goals>
                            <goal>export</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <jdbcDriver>com.mysql.jdbc.Driver</jdbcDriver>
                    <jdbcUrl>jdbc:mysql://ppusari.iptime.org/test</jdbcUrl>
                    <jdbcUser>ppusari</jdbcUser>
                    <jdbcPassword>your-password</jdbcPassword>
                    <packageName>jpa.model</packageName>
                    <targetFolder>target/generated-sources/java</targetFolder>
                    <namePrefix>Q</namePrefix>                    
                    <exportBeans>true</exportBeans>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>5.1.38</version>
                        <scope>compile</scope>
                    </dependency>
                </dependencies>
            </plugin>   
            <!-- 추가3 -->
 
         
        </plugins>
    </build>
 
</project>
 
 

 

코드 자동생성하기

 프로젝트 우클릭 -> Run As ->  Maven Generate-sources 하면 아래 스샷처럼 Entity와 queryDsl을 활용할 수 있는 query-factory 클래스가 자동으로 생성된다.

 

사용방법

생성된 소스를 src/main/java 이하 패키지로 카피하여 사용하시면 됩니다.

728x90
반응형
LIST