initial commit for jooq with spring
This commit is contained in:
parent
da9f3c6f21
commit
e4850ec275
|
@ -0,0 +1,36 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="src" output="target/classes" path="src/main/java">
|
||||
<attributes>
|
||||
<attribute name="optional" value="true"/>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
|
||||
<attributes>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
|
||||
<attributes>
|
||||
<attribute name="optional" value="true"/>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
|
||||
<attributes>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7">
|
||||
<attributes>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
|
||||
<attributes>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="output" path="target/classes"/>
|
||||
</classpath>
|
|
@ -0,0 +1,23 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>jooq-spring</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.m2e.core.maven2Builder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
<nature>org.eclipse.m2e.core.maven2Nature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
|
@ -0,0 +1,159 @@
|
|||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>jooq-spring</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
|
||||
<properties>
|
||||
<org.jooq.version>3.7.3</org.jooq.version>
|
||||
<com.h2database.version>1.4.191</com.h2database.version>
|
||||
<com.zaxxer.HikariCP.version>2.4.4</com.zaxxer.HikariCP.version>
|
||||
<org.springframework.version>4.2.5.RELEASE</org.springframework.version>
|
||||
<org.slf4j.version>1.7.18</org.slf4j.version>
|
||||
<ch.qos.logback.version>1.1.3</ch.qos.logback.version>
|
||||
<junit.version>4.12</junit.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<!-- jOOQ -->
|
||||
<dependency>
|
||||
<groupId>org.jooq</groupId>
|
||||
<artifactId>jooq</artifactId>
|
||||
<version>${org.jooq.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Database Access -->
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<version>${com.h2database.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.zaxxer</groupId>
|
||||
<artifactId>HikariCP</artifactId>
|
||||
<version>${com.zaxxer.HikariCP.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Spring -->
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
<version>${org.springframework.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-jdbc</artifactId>
|
||||
<version>${org.springframework.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Logging -->
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
<version>${org.slf4j.version}</version>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>ch.qos.logback</groupId>
|
||||
<artifactId>logback-classic</artifactId>
|
||||
<version>${ch.qos.logback.version}</version>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- Testing -->
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>${junit.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-test</artifactId>
|
||||
<version>${org.springframework.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.codehaus.mojo</groupId>
|
||||
<artifactId>properties-maven-plugin</artifactId>
|
||||
<version>1.0.0</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>initialize</phase>
|
||||
<goals>
|
||||
<goal>read-project-properties</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<files>
|
||||
<file>src/main/resources/intro_config.properties</file>
|
||||
</files>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.codehaus.mojo</groupId>
|
||||
<artifactId>sql-maven-plugin</artifactId>
|
||||
<version>1.5</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>initialize</phase>
|
||||
<goals>
|
||||
<goal>execute</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<driver>${db.driver}</driver>
|
||||
<url>${db.url}</url>
|
||||
<username>${db.username}</username>
|
||||
<password>${db.password}</password>
|
||||
<srcFiles>
|
||||
<srcFile>src/main/resources/intro_schema.sql</srcFile>
|
||||
</srcFiles>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<version>${com.h2database.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.jooq</groupId>
|
||||
<artifactId>jooq-codegen-maven</artifactId>
|
||||
<version>${org.jooq.version}</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>generate-sources</phase>
|
||||
<goals>
|
||||
<goal>generate</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<jdbc>
|
||||
<driver>${db.driver}</driver>
|
||||
<url>${db.url}</url>
|
||||
<user>${db.username}</user>
|
||||
<password>${db.password}</password>
|
||||
</jdbc>
|
||||
<generator>
|
||||
<target>
|
||||
<packageName>com.baeldung.jooq.introduction.db</packageName>
|
||||
<directory>src/test/java</directory>
|
||||
</target>
|
||||
</generator>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
|
@ -0,0 +1,8 @@
|
|||
#Database Configuration
|
||||
db.driver=org.h2.Driver
|
||||
db.url=jdbc:h2:~/jooq
|
||||
db.username=sa
|
||||
db.password=
|
||||
|
||||
#SQL Dialect
|
||||
jooq.sql.dialect=H2
|
|
@ -0,0 +1,34 @@
|
|||
DROP TABLE IF EXISTS author_book, author, book;
|
||||
|
||||
CREATE TABLE author (
|
||||
id INT NOT NULL PRIMARY KEY,
|
||||
first_name VARCHAR(50),
|
||||
last_name VARCHAR(50) NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE book (
|
||||
id INT NOT NULL PRIMARY KEY,
|
||||
title VARCHAR(100) NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE author_book (
|
||||
author_id INT NOT NULL,
|
||||
book_id INT NOT NULL,
|
||||
|
||||
PRIMARY KEY (author_id, book_id),
|
||||
CONSTRAINT fk_ab_author FOREIGN KEY (author_id) REFERENCES author (id)
|
||||
ON UPDATE CASCADE ON DELETE CASCADE,
|
||||
CONSTRAINT fk_ab_book FOREIGN KEY (book_id) REFERENCES book (id)
|
||||
);
|
||||
|
||||
INSERT INTO author VALUES
|
||||
(1, 'Kathy', 'Sierra'),
|
||||
(2, 'Bert', 'Bates'),
|
||||
(3, 'Bryan', 'Basham');
|
||||
|
||||
INSERT INTO book VALUES
|
||||
(1, 'Head First Java'),
|
||||
(2, 'Head First Servlets and JSP'),
|
||||
(3, 'OCA/OCP Java SE 7 Programmer');
|
||||
|
||||
INSERT INTO author_book VALUES (1, 1), (1, 3), (2, 1);
|
|
@ -0,0 +1,19 @@
|
|||
package com.baeldung.jooq.introduction;
|
||||
|
||||
import org.jooq.ExecuteContext;
|
||||
import org.jooq.SQLDialect;
|
||||
import org.jooq.impl.DefaultExecuteListener;
|
||||
|
||||
import org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator;
|
||||
import org.springframework.jdbc.support.SQLExceptionTranslator;
|
||||
|
||||
public class ExceptionTranslator extends DefaultExecuteListener {
|
||||
private static final long serialVersionUID = 649359748808106775L;
|
||||
|
||||
@Override
|
||||
public void exception(ExecuteContext context) {
|
||||
SQLDialect dialect = context.configuration().dialect();
|
||||
SQLExceptionTranslator translator = new SQLErrorCodeSQLExceptionTranslator(dialect.name());
|
||||
context.exception(translator.translate("Access database using jOOQ", context.sql(), context.sqlException()));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,79 @@
|
|||
package com.baeldung.jooq.introduction;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
|
||||
import org.jooq.SQLDialect;
|
||||
import org.jooq.impl.DataSourceConnectionProvider;
|
||||
import org.jooq.impl.DefaultConfiguration;
|
||||
import org.jooq.impl.DefaultDSLContext;
|
||||
import org.jooq.impl.DefaultExecuteListenerProvider;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
|
||||
import org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan({ "com.baeldung.jooq.introduction.db.public_.tables" })
|
||||
@EnableTransactionManagement
|
||||
@PropertySource("classpath:intro_config.properties")
|
||||
public class PersistenceContext {
|
||||
@Autowired
|
||||
private Environment environment;
|
||||
|
||||
@Bean
|
||||
public DataSource dataSource() {
|
||||
HikariDataSource dataSource = new HikariDataSource();
|
||||
|
||||
dataSource.setDriverClassName(environment.getRequiredProperty("db.driver"));
|
||||
dataSource.setJdbcUrl(environment.getRequiredProperty("db.url"));
|
||||
dataSource.setUsername(environment.getRequiredProperty("db.username"));
|
||||
dataSource.setPassword(environment.getRequiredProperty("db.password"));
|
||||
|
||||
return dataSource;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public TransactionAwareDataSourceProxy transactionAwareDataSource() {
|
||||
return new TransactionAwareDataSourceProxy(dataSource());
|
||||
}
|
||||
|
||||
@Bean
|
||||
public DataSourceTransactionManager transactionManager() {
|
||||
return new DataSourceTransactionManager(dataSource());
|
||||
}
|
||||
|
||||
@Bean
|
||||
public DataSourceConnectionProvider connectionProvider() {
|
||||
return new DataSourceConnectionProvider(transactionAwareDataSource());
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ExceptionTranslator exceptionTransformer() {
|
||||
return new ExceptionTranslator();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public DefaultDSLContext dsl() {
|
||||
return new DefaultDSLContext(configuration());
|
||||
}
|
||||
|
||||
@Bean
|
||||
public DefaultConfiguration configuration() {
|
||||
DefaultConfiguration jooqConfiguration = new DefaultConfiguration();
|
||||
jooqConfiguration.set(connectionProvider());
|
||||
jooqConfiguration.set(new DefaultExecuteListenerProvider(exceptionTransformer()));
|
||||
|
||||
String sqlDialectName = environment.getRequiredProperty("jooq.sql.dialect");
|
||||
SQLDialect dialect = SQLDialect.valueOf(sqlDialectName);
|
||||
jooqConfiguration.set(dialect);
|
||||
|
||||
return jooqConfiguration;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,87 @@
|
|||
package com.baeldung.jooq.introduction;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.jooq.DSLContext;
|
||||
import org.jooq.Record3;
|
||||
import org.jooq.Result;
|
||||
import org.jooq.impl.DSL;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import com.baeldung.jooq.introduction.db.public_.tables.Author;
|
||||
import com.baeldung.jooq.introduction.db.public_.tables.AuthorBook;
|
||||
import com.baeldung.jooq.introduction.db.public_.tables.Book;
|
||||
|
||||
@ContextConfiguration(classes = PersistenceContext.class)
|
||||
@Transactional(transactionManager = "transactionManager")
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
public class QueryTest {
|
||||
@Autowired
|
||||
private DSLContext create;
|
||||
|
||||
Author author = Author.AUTHOR;
|
||||
Book book = Book.BOOK;
|
||||
AuthorBook authorBook = AuthorBook.AUTHOR_BOOK;
|
||||
|
||||
@Test
|
||||
public void givenValidData_whenInserting_thenSucceed() {
|
||||
create.insertInto(author).set(author.ID, 4).set(author.FIRST_NAME, "Herbert").set(author.LAST_NAME, "Schildt").execute();
|
||||
create.insertInto(book).set(book.ID, 4).set(book.TITLE, "A Beginner's Guide").execute();
|
||||
create.insertInto(authorBook).set(authorBook.AUTHOR_ID, 4).set(authorBook.BOOK_ID, 4).execute();
|
||||
Result<Record3<Integer, String, Integer>> result = create.select(author.ID, author.LAST_NAME, DSL.count()).from(author).join(authorBook).on(author.ID.equal(authorBook.AUTHOR_ID)).join(book).on(authorBook.BOOK_ID.equal(book.ID))
|
||||
.groupBy(author.LAST_NAME).fetch();
|
||||
|
||||
assertEquals(3, result.size());
|
||||
assertEquals("Sierra", result.getValue(0, author.LAST_NAME));
|
||||
assertEquals(Integer.valueOf(2), result.getValue(0, DSL.count()));
|
||||
assertEquals("Schildt", result.getValue(2, author.LAST_NAME));
|
||||
assertEquals(Integer.valueOf(1), result.getValue(2, DSL.count()));
|
||||
}
|
||||
|
||||
@Test(expected = DataAccessException.class)
|
||||
public void givenInvalidData_whenInserting_thenFail() {
|
||||
create.insertInto(authorBook).set(authorBook.AUTHOR_ID, 4).set(authorBook.BOOK_ID, 5).execute();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenValidData_whenUpdating_thenSucceed() {
|
||||
create.update(author).set(author.LAST_NAME, "Baeldung").where(author.ID.equal(3)).execute();
|
||||
create.update(book).set(book.TITLE, "Building your REST API with Spring").where(book.ID.equal(3)).execute();
|
||||
create.insertInto(authorBook).set(authorBook.AUTHOR_ID, 3).set(authorBook.BOOK_ID, 3).execute();
|
||||
Result<Record3<Integer, String, String>> result = create.select(author.ID, author.LAST_NAME, book.TITLE).from(author).join(authorBook).on(author.ID.equal(authorBook.AUTHOR_ID)).join(book).on(authorBook.BOOK_ID.equal(book.ID)).where(author.ID.equal(3))
|
||||
.fetch();
|
||||
|
||||
assertEquals(1, result.size());
|
||||
assertEquals(Integer.valueOf(3), result.getValue(0, author.ID));
|
||||
assertEquals("Baeldung", result.getValue(0, author.LAST_NAME));
|
||||
assertEquals("Building your REST API with Spring", result.getValue(0, book.TITLE));
|
||||
}
|
||||
|
||||
@Test(expected = DataAccessException.class)
|
||||
public void givenInvalidData_whenUpdating_thenFail() {
|
||||
create.update(authorBook).set(authorBook.AUTHOR_ID, 4).set(authorBook.BOOK_ID, 5).execute();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenValidData_whenDeleting_thenSucceed() {
|
||||
create.delete(author).where(author.ID.lt(3)).execute();
|
||||
Result<Record3<Integer, String, String>> result = create.select(author.ID, author.FIRST_NAME, author.LAST_NAME).from(author).fetch();
|
||||
|
||||
assertEquals(1, result.size());
|
||||
assertEquals("Bryan", result.getValue(0, author.FIRST_NAME));
|
||||
assertEquals("Basham", result.getValue(0, author.LAST_NAME));
|
||||
}
|
||||
|
||||
@Test(expected = DataAccessException.class)
|
||||
public void givenInvalidData_whenDeleting_thenFail() {
|
||||
create.delete(book).where(book.ID.equal(1)).execute();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue