관리 메뉴

IT.FARMER

JOOQ DSL(Domain Specific Language) 생성방법 본문

JAVA/JOOQ

JOOQ DSL(Domain Specific Language) 생성방법

아이티.파머 2020. 10. 22. 16:33
반응형

2020/10/22 - [JAVA] - QueryDSL 과 JOOQ 비교

2020/10/22 - [JAVA/JOOQ] - JOOQ DSL(Domain Specific Language) 생성방법

2020/10/22 - [JAVA/JOOQ] - Spring Boot & Jooq

Jooq를 domain 형태로 쉽게 SQL을 작성할수 있다. IDE를 사용한다면 더욱 쉽게 사용 가능 하다. 타입 세이프하고, 컴파일 시에 오류를 알려준다.

https://www.jooq.org/doc/3.13/manual/sql-building/sql-statements/dsl-and-non-dsl/

JOOQ는 DB에 있는 테이블 정보를 가져와 도메인 객체로 만들어준다. QueryDsl 이나 JPA를 사용할때 처럼 도메인과 DB간에 매핑을 시켜주면서 하는 작업이 아닌 역으로 DB 테이블 스키마를 도메인으로 만들어 주는것이다.

1. Jooq DSL Code Generated 방법

Maven 을 사용할 경우 플러그인 후 code를 generated 하여 사용하도록 한다.

...
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jooq</artifactId>
</dependency>
...
<build>
	<plugins>
		<plugin>
		      <!-- https://www.jooq.org/doc/latest/manual/code-generation/codegen-configuration/ -->
		      <groupId>org.jooq</groupId>
		      <artifactId>jooq-codegen-maven</artifactId>
		      <version>3.13.2</version>
		      <executions>
		          <execution>
		              <id>jooq-codegen</id>
		              <phase>generate-sources</phase>
		              <goals>
		                  <goal>generate</goal>
		              </goals>
		          </execution>
		      </executions>
		
		      <dependencies>
		          <dependency>
		              <groupId>com.skan.farm.common</groupId>
		              <artifactId>mas-farm-common-domain</artifactId>
		              <version>1.0-SNAPSHOT</version>
		          </dependency>
		      </dependencies>
		
		      <configuration>
		          <!-- JDBC connection parameters -->
		          <jdbc>
		              <driver>com.mysql.jdbc.Driver</driver>
		              <url>jdbc:mysql://localhost:3306/naive-it-farm?useUnicode=true&amp;characterEncoding=utf8&amp;useSSL=false&amp;serverTimezone=Asia/Seoul</url>
		              <user>admin</user>
		              <password>1234</password>
		          </jdbc>
		          <!-- Generator parameters -->
		          <generator>
		              <!-- The default code generator. You can override this one, to generate your own code style
		                     Defaults to org.jooq.codegen.JavaGenerator -->
		              <name>org.jooq.codegen.JavaGenerator</name>
		
		              <!-- The naming strategy used for class and field names.
		                   You may override this with your custom naming strategy. Some examples follow
		                   Defaults to org.jooq.codegen.DefaultGeneratorStrategy -->
		              <strategy>
		                  <name>com.skan.farm.common.config.CustomGeneratorStrategy</name>
		              </strategy>
		
		
		              <database>
		                  <name>org.jooq.meta.mysql.MySQLDatabase</name>
		                  <inputSchema>naive-it-farm</inputSchema>
		                  <includes>.*</includes>
		                  <excludes>
		                      UNUSED_TABLE # This table (unqualified name) should not be generated
		                      | PREFIX_.* # Objects with a given prefix should not be generated
		                      | SECRET_SCHEMA\.SECRET_TABLE # This table (qualified name) should not be generated
		                      | SECRET_ROUTINE # This routine (unqualified name) ...
		                  </excludes>
		              </database>
		              <target>
		                  <packageName>com.skan.farm.domain</packageName>
		                  <directory>target/generated-sources/jooq</directory>
		                  <encoding>UTF-8</encoding>
		                  <clean>true</clean>
		              </target>
		          </generator>
		      </configuration>
		  </plugin>
	</plugins>
</build>

mvn jooq-codegen:generate 으로 target/generated-sources/jooq 폴더에 DSL이 생성되는것을 확인 할수 있다.

참고 : Code Generation Maven

Running the code generator with Maven

 

Running the code generator with Maven

There are, however, some additional, Maven-specific flags that can be specified with the jooq-codegen-maven plugin only:

www.jooq.org

2. CustomGeneratorStrategy

일반 도메인 객체와 Jooq generater 에서 나온 객체가 같은 이름으로 겹칠수 있다. 이때 DSL 도메인 생성시 org.jooq.codegen.DefaultGeneratorStrategy 을 상속받아 파일 이름 앞에 prefix 를 붙여 구분하기 쉽게 할 수 있다.

주의사항 , Maven 사용시 같은 프로젝트 내에서 CustomGeneratorStrategy를 만들어 pom.xml 의 <strategy/> 에 추가 하게 되면 class not found 가 되어 버린다. 프로젝트를 분리하여, <defendences/> 안에 프로젝트를 넣어 주면 정상적으로 사용 가능 하다.

pom.xml

<dependencies>
    <!-- https://mvnrepository.com/artifact/org.jooq/jooq-codegen -->
    <dependency>
        <groupId>org.jooq</groupId>
        <artifactId>jooq</artifactId>
    </dependency>
    <dependency>
        <groupId>org.jooq</groupId>
        <artifactId>jooq-meta</artifactId>
    </dependency>
    <dependency>
        <groupId>org.jooq</groupId>
        <artifactId>jooq-codegen</artifactId>
    </dependency>
</dependencies>

CustomStrategy

package com.skan.farm.common.config;


import org.jooq.codegen.DefaultGeneratorStrategy;
import org.jooq.meta.Definition;


/**
 * <pre>
 * Description : Jooq code generator 시에 기본 Model 이랑 구분하기 위한 커스텀 클레스
 *
 *
 *
 * </pre>
 *
 * @author skan
 * @version Copyright (C) 2020 by CJENM|Mezzomedia. All right reserved.
 * @since 2020-06-18
 */
public class CustomGeneratorStrategy extends DefaultGeneratorStrategy {

    @Override
    public String getJavaClassName(Definition definition, Mode mode) {
        return "J"+ super.getJavaClassName(definition,mode);
    }
}

 

반응형

'JAVA > JOOQ' 카테고리의 다른 글

QueryDSL 과 JOOQ 비교  (8) 2020.10.22
Spring Boot & Jooq  (0) 2020.10.22