> For the complete documentation index, see [llms.txt](https://wannaqueen.gitbook.io/spring5/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://wannaqueen.gitbook.io/spring5/spring-boot/undefined-1/31.-sql-by-ys/31.6-jooq.md).

# 31.6  jOOQ 사용하기

Java Object Oriented Querying ( [jOOQ](https://www.jooq.org/) )은 데이터베이스에서 Java 코드를 생성하고 유창한 API를 통해 유형 안전 SQL 쿼리를 작성할 수있게 해주는 [Data ](https://www.datageekery.com/)[Geekery](https://www.jooq.org/) 에서 널리 사용되는 제품입니다 . 상용 및 오픈 소스 에디션 모두 Spring Boot와 함께 사용할 수 있습니다.

#### 31.6.1 코드 생성

jOOQ 타입 안전 질의를 사용하려면 데이터베이스 스키마에서 Java 클래스를 생성해야한다. jOOQ 사용자 설명서의 지침을 따를 수 있습니다. `jooq-codegen-maven`플러그인을 사용하고 `spring-boot-starter-parent` "parent POM"도 사용하는 경우 안전하게 플러그인의  태그를 생략 할 수 있습니다. Spring 부트 정의 버전 변수 (예 : h`h2.version`)를 사용하여 플러그인의 데이터베이스 종속성을 선언 할 수도 있습니다. 다음 목록은 예제를 보여줍니다.

```markup
<plugin>
	<groupId>org.jooq</groupId>
	<artifactId>jooq-codegen-maven</artifactId>
	<executions>
		...
	</executions>
	<dependencies>
		<dependency>
			<groupId>com.h2database</groupId>
			<artifactId>h2</artifactId>
			<version>${h2.version}</version>
		</dependency>
	</dependencies>
	<configuration>
		<jdbc>
			<driver>org.h2.Driver</driver>
			<url>jdbc:h2:~/yourdatabase</url>
		</jdbc>
		<generator>
			...
		</generator>
	</configuration>
</plugin>
```

#### 31.6.2 DSLContext 사용

jOOQ가 제공하는 유창한 API는 `org.jooq.DSLContext` 인터페이스를 통해 시작됩니다. Spring Boot는 DSLContext를 Spring Bean으로 자동 구성하고이를 응용 프로그램 `DataSource`에 연결합니다. `DSLContext`를 사용하려면 다음 예제와 같이 `@Autowire`를 사용할 수 있습니다.

```java
@Component
public class JooqExample implements CommandLineRunner {

	private final DSLContext create;

	@Autowired
	public JooqExample(DSLContext dslContext) {
		this.create = dslContext;
	}

}
```

| ![\[팁\]](https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/images/tip.png) |
| --------------------------------------------------------------------------------------------- |
| jOOQ 매뉴얼은 `create`라는 변수를 사용하여 `DSLContext`를 유지하는 경향이 있습니다.                                    |

그런 다음에 `DSLContext`를 사용하여 다음 예제와 같이 쿼리를 구성 할 수 있습니다 .

```java
public List<GregorianCalendar> authorsBornAfter1980() {
	return this.create.selectFrom(AUTHOR)
		.where(AUTHOR.DATE_OF_BIRTH.greaterThan(new GregorianCalendar(1980, 0, 1)))
		.fetch(AUTHOR.DATE_OF_BIRTH);
}
```

#### 31.6.3 jOOQ SQL Dialect

`spring.jooq.sql-dialect` 속성이 구성되어 있지 않으면 Spring Boot가 데이터 소스에 사용할 SQL 언어를 결정합니다. 스프링 부트가 방언을 탐지하지 못하면 `DEFAULT`를 사용합니다.

| ![\[노트\]](https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/images/note.png) |
| ----------------------------------------------------------------------------------------------- |
| 스프링 부트는 오픈 소스 버전의 jOOQ가 지원하는 dialects 만 자동 구성 할 수 있습니다.                                         |

#### 31.6.4 jOOQ 커스터마이징

jooQ `Configuration`이 생성 될 때 사용되는 `@Bean`정의를 정의하여 더 많은 고급 사용자 정의를 구현할 수 있습니다. 다음 jOOQ 유형에 대해 bean을 정의 할 수 있습니다.

* `ConnectionProvider`
* `ExecutorProvider`
* `TransactionProvider`
* `RecordMapperProvider`
* `RecordUnmapperProvider`
* `RecordListenerProvider`
* `ExecuteListenerProvider`
* `VisitListenerProvider`
* `TransactionListenerProvider`

또한 jOOQ 설정을 완벽하게 제어하려면`org.jooq.Configuration @Bean`을 직접 생성 할 수 있습니다.

### <br>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://wannaqueen.gitbook.io/spring5/spring-boot/undefined-1/31.-sql-by-ys/31.6-jooq.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
