반응형
Notice
Recent Posts
Recent Comments
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- QueryDSL
- Docker Compose
- docker-compose
- Netty
- spring cloud
- RabbitMQ
- docker
- KAFKA
- Selenium
- Spring Open Feign
- cassandra
- Redis Sentinel
- docker compose mysql
- spring boot redis
- netflix oss
- vue.js
- coalesce
- WebLogic
- spring boot
- weblogic 10
- Hibernate
- MySQL
- redis
- argo cd
- JPA
- ChannelPipeline
- mybatis
- grafana
- jmeter
- Airflow
Archives
- Today
- Total
IT.FARMER
Spring Boot & Jooq 본문
반응형
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란 ?
Jooq는 데이터베이스에서 자바코드를 빌드하고 안전한 Sql 쿼리를 작성할수 있는 API이다 .
라이브러리 설정
Spring boot 를 사용중이라면 spring boot starter에서 jooq만 추가해주면 된다.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jooq</artifactId>
</dependency>
환경설정
Java Configration으로 설정 하는 방법도 있지만 Spring Boot에서는 정말 간편하게 사용가능하다.
application.yml 에 jooq:sql-dialect 내역만 활성화 시켜준다.
spring:
profiles: local
devtools:
restart:
enabled: false
datasource:
#url: jdbc:h2:mem:naive-it-farm
url: jdbc:h2:file:~/database/naive-it-farm;AUTO_SERVER=TRUE
driver-class-name: org.h2.Driver
username: admin
password: 1234
jooq:
sql-dialect: h2
h2:
console:
enabled: true
path: /h2-console
logging:
file:
name: logs/it.farm.apiserver.local.log
level:
root: info
com.skan: debug
org.springframework : info
web: info
코드 샘플
DSLContext 를 주입받아 표준 sql 을생성, 이를 이용하여 jdbcTemplate 로 실행 할수 있으며, DSLContext로 가져와 도메인 객체로 변경 할수도 있다.
@Repository
@Slf4j
public class TestJooqRepository {
private final DSLContext dslContext;
private final JdbcTemplate jdbcTemplate;
public TestJooqRepository(DSLContext dslContext, JdbcTemplate jdbcTemplate) {
this.dslContext = dslContext;
this.jdbcTemplate = jdbcTemplate;
}
/**
* jooq로 표쥰 sql 생성후 jdbctemplate 을 사용하여 데이터 불러오기
*
* @return
*/
public List<Map<String, Object>> getList() {
String sql = dslContext.select(DSL.field("user_id"), DSL.field("user_name"))
.from(DSL.table("users")).getSQL();
log.debug("string sql = {}", sql);
return jdbcTemplate.queryForList(sql);
}
/**
* jooq로 데이터를 가져와 domain 객체로 바꿔주기
* @return
*/
public List<Users> getListObject() {
return dslContext.select(DSL.field("user_id"), DSL.field("user_name"))
.from(DSL.table("users")).fetchInto(Users.class);
}
}
반응형
'JAVA > JOOQ' 카테고리의 다른 글
QueryDSL 과 JOOQ 비교 (8) | 2020.10.22 |
---|---|
JOOQ DSL(Domain Specific Language) 생성방법 (0) | 2020.10.22 |