Skip to content

Commit 40b84aa

Browse files
committed
Merge pull request eugenp#395 from nguyennamthai/master
Code for jOOQ with Spring article
2 parents 4e16ccc + e4850ec commit 40b84aa

File tree

8 files changed

+445
-0
lines changed

8 files changed

+445
-0
lines changed

jooq-spring/.classpath

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" output="target/classes" path="src/main/java">
4+
<attributes>
5+
<attribute name="optional" value="true"/>
6+
<attribute name="maven.pomderived" value="true"/>
7+
</attributes>
8+
</classpathentry>
9+
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
10+
<attributes>
11+
<attribute name="maven.pomderived" value="true"/>
12+
</attributes>
13+
</classpathentry>
14+
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
15+
<attributes>
16+
<attribute name="optional" value="true"/>
17+
<attribute name="maven.pomderived" value="true"/>
18+
</attributes>
19+
</classpathentry>
20+
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
21+
<attributes>
22+
<attribute name="maven.pomderived" value="true"/>
23+
</attributes>
24+
</classpathentry>
25+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7">
26+
<attributes>
27+
<attribute name="maven.pomderived" value="true"/>
28+
</attributes>
29+
</classpathentry>
30+
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
31+
<attributes>
32+
<attribute name="maven.pomderived" value="true"/>
33+
</attributes>
34+
</classpathentry>
35+
<classpathentry kind="output" path="target/classes"/>
36+
</classpath>

jooq-spring/.project

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>jooq-spring</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
<buildCommand>
14+
<name>org.eclipse.m2e.core.maven2Builder</name>
15+
<arguments>
16+
</arguments>
17+
</buildCommand>
18+
</buildSpec>
19+
<natures>
20+
<nature>org.eclipse.jdt.core.javanature</nature>
21+
<nature>org.eclipse.m2e.core.maven2Nature</nature>
22+
</natures>
23+
</projectDescription>

jooq-spring/pom.xml

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.baeldung</groupId>
5+
<artifactId>jooq-spring</artifactId>
6+
<version>0.0.1-SNAPSHOT</version>
7+
8+
<properties>
9+
<org.jooq.version>3.7.3</org.jooq.version>
10+
<com.h2database.version>1.4.191</com.h2database.version>
11+
<com.zaxxer.HikariCP.version>2.4.4</com.zaxxer.HikariCP.version>
12+
<org.springframework.version>4.2.5.RELEASE</org.springframework.version>
13+
<org.slf4j.version>1.7.18</org.slf4j.version>
14+
<ch.qos.logback.version>1.1.3</ch.qos.logback.version>
15+
<junit.version>4.12</junit.version>
16+
</properties>
17+
18+
<dependencies>
19+
<!-- jOOQ -->
20+
<dependency>
21+
<groupId>org.jooq</groupId>
22+
<artifactId>jooq</artifactId>
23+
<version>${org.jooq.version}</version>
24+
</dependency>
25+
26+
<!-- Database Access -->
27+
<dependency>
28+
<groupId>com.h2database</groupId>
29+
<artifactId>h2</artifactId>
30+
<version>${com.h2database.version}</version>
31+
</dependency>
32+
<dependency>
33+
<groupId>com.zaxxer</groupId>
34+
<artifactId>HikariCP</artifactId>
35+
<version>${com.zaxxer.HikariCP.version}</version>
36+
</dependency>
37+
38+
<!-- Spring -->
39+
<dependency>
40+
<groupId>org.springframework</groupId>
41+
<artifactId>spring-context</artifactId>
42+
<version>${org.springframework.version}</version>
43+
</dependency>
44+
<dependency>
45+
<groupId>org.springframework</groupId>
46+
<artifactId>spring-jdbc</artifactId>
47+
<version>${org.springframework.version}</version>
48+
</dependency>
49+
50+
<!-- Logging -->
51+
<dependency>
52+
<groupId>org.slf4j</groupId>
53+
<artifactId>slf4j-api</artifactId>
54+
<version>${org.slf4j.version}</version>
55+
<scope>runtime</scope>
56+
</dependency>
57+
<dependency>
58+
<groupId>ch.qos.logback</groupId>
59+
<artifactId>logback-classic</artifactId>
60+
<version>${ch.qos.logback.version}</version>
61+
<scope>runtime</scope>
62+
</dependency>
63+
64+
<!-- Testing -->
65+
<dependency>
66+
<groupId>junit</groupId>
67+
<artifactId>junit</artifactId>
68+
<version>${junit.version}</version>
69+
<scope>test</scope>
70+
</dependency>
71+
<dependency>
72+
<groupId>org.springframework</groupId>
73+
<artifactId>spring-test</artifactId>
74+
<version>${org.springframework.version}</version>
75+
<scope>test</scope>
76+
</dependency>
77+
</dependencies>
78+
79+
<build>
80+
<plugins>
81+
<plugin>
82+
<groupId>org.codehaus.mojo</groupId>
83+
<artifactId>properties-maven-plugin</artifactId>
84+
<version>1.0.0</version>
85+
<executions>
86+
<execution>
87+
<phase>initialize</phase>
88+
<goals>
89+
<goal>read-project-properties</goal>
90+
</goals>
91+
<configuration>
92+
<files>
93+
<file>src/main/resources/intro_config.properties</file>
94+
</files>
95+
</configuration>
96+
</execution>
97+
</executions>
98+
</plugin>
99+
100+
<plugin>
101+
<groupId>org.codehaus.mojo</groupId>
102+
<artifactId>sql-maven-plugin</artifactId>
103+
<version>1.5</version>
104+
<executions>
105+
<execution>
106+
<phase>initialize</phase>
107+
<goals>
108+
<goal>execute</goal>
109+
</goals>
110+
<configuration>
111+
<driver>${db.driver}</driver>
112+
<url>${db.url}</url>
113+
<username>${db.username}</username>
114+
<password>${db.password}</password>
115+
<srcFiles>
116+
<srcFile>src/main/resources/intro_schema.sql</srcFile>
117+
</srcFiles>
118+
</configuration>
119+
</execution>
120+
</executions>
121+
<dependencies>
122+
<dependency>
123+
<groupId>com.h2database</groupId>
124+
<artifactId>h2</artifactId>
125+
<version>${com.h2database.version}</version>
126+
</dependency>
127+
</dependencies>
128+
</plugin>
129+
130+
<plugin>
131+
<groupId>org.jooq</groupId>
132+
<artifactId>jooq-codegen-maven</artifactId>
133+
<version>${org.jooq.version}</version>
134+
<executions>
135+
<execution>
136+
<phase>generate-sources</phase>
137+
<goals>
138+
<goal>generate</goal>
139+
</goals>
140+
<configuration>
141+
<jdbc>
142+
<driver>${db.driver}</driver>
143+
<url>${db.url}</url>
144+
<user>${db.username}</user>
145+
<password>${db.password}</password>
146+
</jdbc>
147+
<generator>
148+
<target>
149+
<packageName>com.baeldung.jooq.introduction.db</packageName>
150+
<directory>src/test/java</directory>
151+
</target>
152+
</generator>
153+
</configuration>
154+
</execution>
155+
</executions>
156+
</plugin>
157+
</plugins>
158+
</build>
159+
</project>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#Database Configuration
2+
db.driver=org.h2.Driver
3+
db.url=jdbc:h2:~/jooq
4+
db.username=sa
5+
db.password=
6+
7+
#SQL Dialect
8+
jooq.sql.dialect=H2
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
DROP TABLE IF EXISTS author_book, author, book;
2+
3+
CREATE TABLE author (
4+
id INT NOT NULL PRIMARY KEY,
5+
first_name VARCHAR(50),
6+
last_name VARCHAR(50) NOT NULL
7+
);
8+
9+
CREATE TABLE book (
10+
id INT NOT NULL PRIMARY KEY,
11+
title VARCHAR(100) NOT NULL
12+
);
13+
14+
CREATE TABLE author_book (
15+
author_id INT NOT NULL,
16+
book_id INT NOT NULL,
17+
18+
PRIMARY KEY (author_id, book_id),
19+
CONSTRAINT fk_ab_author FOREIGN KEY (author_id) REFERENCES author (id)
20+
ON UPDATE CASCADE ON DELETE CASCADE,
21+
CONSTRAINT fk_ab_book FOREIGN KEY (book_id) REFERENCES book (id)
22+
);
23+
24+
INSERT INTO author VALUES
25+
(1, 'Kathy', 'Sierra'),
26+
(2, 'Bert', 'Bates'),
27+
(3, 'Bryan', 'Basham');
28+
29+
INSERT INTO book VALUES
30+
(1, 'Head First Java'),
31+
(2, 'Head First Servlets and JSP'),
32+
(3, 'OCA/OCP Java SE 7 Programmer');
33+
34+
INSERT INTO author_book VALUES (1, 1), (1, 3), (2, 1);
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.jooq.introduction;
2+
3+
import org.jooq.ExecuteContext;
4+
import org.jooq.SQLDialect;
5+
import org.jooq.impl.DefaultExecuteListener;
6+
7+
import org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator;
8+
import org.springframework.jdbc.support.SQLExceptionTranslator;
9+
10+
public class ExceptionTranslator extends DefaultExecuteListener {
11+
private static final long serialVersionUID = 649359748808106775L;
12+
13+
@Override
14+
public void exception(ExecuteContext context) {
15+
SQLDialect dialect = context.configuration().dialect();
16+
SQLExceptionTranslator translator = new SQLErrorCodeSQLExceptionTranslator(dialect.name());
17+
context.exception(translator.translate("Access database using jOOQ", context.sql(), context.sqlException()));
18+
}
19+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package com.baeldung.jooq.introduction;
2+
3+
import javax.sql.DataSource;
4+
import com.zaxxer.hikari.HikariDataSource;
5+
6+
import org.jooq.SQLDialect;
7+
import org.jooq.impl.DataSourceConnectionProvider;
8+
import org.jooq.impl.DefaultConfiguration;
9+
import org.jooq.impl.DefaultDSLContext;
10+
import org.jooq.impl.DefaultExecuteListenerProvider;
11+
12+
import org.springframework.beans.factory.annotation.Autowired;
13+
import org.springframework.context.annotation.Bean;
14+
import org.springframework.context.annotation.ComponentScan;
15+
import org.springframework.context.annotation.Configuration;
16+
import org.springframework.context.annotation.PropertySource;
17+
import org.springframework.core.env.Environment;
18+
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
19+
import org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy;
20+
import org.springframework.transaction.annotation.EnableTransactionManagement;
21+
22+
@Configuration
23+
@ComponentScan({ "com.baeldung.jooq.introduction.db.public_.tables" })
24+
@EnableTransactionManagement
25+
@PropertySource("classpath:intro_config.properties")
26+
public class PersistenceContext {
27+
@Autowired
28+
private Environment environment;
29+
30+
@Bean
31+
public DataSource dataSource() {
32+
HikariDataSource dataSource = new HikariDataSource();
33+
34+
dataSource.setDriverClassName(environment.getRequiredProperty("db.driver"));
35+
dataSource.setJdbcUrl(environment.getRequiredProperty("db.url"));
36+
dataSource.setUsername(environment.getRequiredProperty("db.username"));
37+
dataSource.setPassword(environment.getRequiredProperty("db.password"));
38+
39+
return dataSource;
40+
}
41+
42+
@Bean
43+
public TransactionAwareDataSourceProxy transactionAwareDataSource() {
44+
return new TransactionAwareDataSourceProxy(dataSource());
45+
}
46+
47+
@Bean
48+
public DataSourceTransactionManager transactionManager() {
49+
return new DataSourceTransactionManager(dataSource());
50+
}
51+
52+
@Bean
53+
public DataSourceConnectionProvider connectionProvider() {
54+
return new DataSourceConnectionProvider(transactionAwareDataSource());
55+
}
56+
57+
@Bean
58+
public ExceptionTranslator exceptionTransformer() {
59+
return new ExceptionTranslator();
60+
}
61+
62+
@Bean
63+
public DefaultDSLContext dsl() {
64+
return new DefaultDSLContext(configuration());
65+
}
66+
67+
@Bean
68+
public DefaultConfiguration configuration() {
69+
DefaultConfiguration jooqConfiguration = new DefaultConfiguration();
70+
jooqConfiguration.set(connectionProvider());
71+
jooqConfiguration.set(new DefaultExecuteListenerProvider(exceptionTransformer()));
72+
73+
String sqlDialectName = environment.getRequiredProperty("jooq.sql.dialect");
74+
SQLDialect dialect = SQLDialect.valueOf(sqlDialectName);
75+
jooqConfiguration.set(dialect);
76+
77+
return jooqConfiguration;
78+
}
79+
}

0 commit comments

Comments
 (0)