Add jOOQ sample application

See gh-2804
This commit is contained in:
Phillip Webb
2015-06-22 20:15:09 -07:00
parent a08b09563b
commit ab1cc829c1
22 changed files with 2385 additions and 0 deletions

View File

@@ -0,0 +1,80 @@
/*
* Copyright 2012-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package sample.jooq;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.jooq.DSLContext;
import org.jooq.Query;
import org.jooq.Record;
import org.jooq.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;
import static sample.jooq.domain.tables.Author.AUTHOR;
import static sample.jooq.domain.tables.Book.BOOK;
@Component
public class JooqExamples implements CommandLineRunner {
private final DSLContext dsl;
private final JdbcTemplate jdbc;
@Autowired
public JooqExamples(DSLContext dsl, JdbcTemplate jdbc) {
this.dsl = dsl;
this.jdbc = jdbc;
}
@Override
public void run(String... args) throws Exception {
jooqFetch();
jooqSql();
}
private void jooqFetch() {
Result<Record> results = this.dsl.select().from(AUTHOR).fetch();
for (Record result : results) {
Integer id = result.getValue(AUTHOR.ID);
String firstName = result.getValue(AUTHOR.FIRST_NAME);
String lastName = result.getValue(AUTHOR.LAST_NAME);
System.out.println("jOOQ Fetch " + id + " " + firstName + " " + lastName);
}
}
private void jooqSql() {
Query query = this.dsl.select(BOOK.TITLE, AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME)
.from(BOOK).join(AUTHOR).on(BOOK.AUTHOR_ID.equal(AUTHOR.ID))
.where(BOOK.PUBLISHED_IN.equal(2015));
Object[] bind = query.getBindValues().toArray(new Object[] {});
List<String> list = this.jdbc.query(query.getSQL(), bind,
new RowMapper<String>() {
@Override
public String mapRow(ResultSet rs, int rowNum) throws SQLException {
return rs.getString(1) + " : " + rs.getString(2) + " "
+ rs.getString(3);
}
});
System.out.println("jOOQ SQL " + list);
}
}

View File

@@ -0,0 +1,29 @@
/*
* Copyright 2012-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package sample.jooq;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SampleJooqApplication {
public static void main(String[] args) {
SpringApplication.run(SampleJooqApplication.class, args);
}
}

View File

@@ -0,0 +1,12 @@
INSERT INTO language VALUES (1, 'EN', 'English');
INSERT INTO author VALUES (1, 'Greg', 'Turnquest', '1804-09-17', 1804, 1);
INSERT INTO author VALUES (2, 'Craig', 'Walls', '1804-09-18', 1804, 1);
INSERT INTO book VALUES (1, 1, 'Learning Spring Boot', 2015, 1);
INSERT INTO book VALUES (2, 2, 'Spring Boot in Action', 2015, 1);
INSERT INTO book_store VALUES ('Barnes & Noble');
INSERT INTO book_to_book_store VALUES ('Barnes & Noble', 1, 10);
INSERT INTO book_to_book_store VALUES ('Barnes & Noble', 2, 3);

View File

@@ -0,0 +1 @@
DROP ALL OBJECTS;

View File

@@ -0,0 +1,39 @@
CREATE TABLE language (
id NUMBER(7) NOT NULL PRIMARY KEY,
cd CHAR(2) NOT NULL,
description VARCHAR2(50)
);
CREATE TABLE author (
id NUMBER(7) NOT NULL PRIMARY KEY,
first_name VARCHAR2(50),
last_name VARCHAR2(50) NOT NULL,
date_of_birth DATE,
year_of_birth NUMBER(7),
distinguished NUMBER(1)
);
CREATE TABLE book (
id NUMBER(7) NOT NULL PRIMARY KEY,
author_id NUMBER(7) NOT NULL,
title VARCHAR2(400) NOT NULL,
published_in NUMBER(7) NOT NULL,
language_id NUMBER(7) NOT NULL,
CONSTRAINT fk_book_author FOREIGN KEY (author_id) REFERENCES author(id),
CONSTRAINT fk_book_language FOREIGN KEY (language_id) REFERENCES language(id)
);
CREATE TABLE book_store (
name VARCHAR2(400) NOT NULL UNIQUE
);
CREATE TABLE book_to_book_store (
name VARCHAR2(400) NOT NULL,
book_id INTEGER NOT NULL,
stock INTEGER,
PRIMARY KEY(name, book_id),
CONSTRAINT fk_b2bs_book_store FOREIGN KEY (name) REFERENCES book_store (name) ON DELETE CASCADE,
CONSTRAINT fk_b2bs_book FOREIGN KEY (book_id) REFERENCES book (id) ON DELETE CASCADE
);

View File

@@ -0,0 +1,46 @@
/*
* Copyright 2012-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package sample.jooq;
import org.junit.Rule;
import org.junit.Test;
import org.springframework.boot.test.OutputCapture;
import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertThat;
/**
* Integration tests for {@link SampleJooqApplication}.
*/
public class SampleJooqApplicationTests {
private static final String[] NO_ARGS = {};
@Rule
public OutputCapture out = new OutputCapture();
@Test
public void outputResults() throws Exception {
SampleJooqApplication.main(NO_ARGS);
assertThat(this.out.toString(), containsString("jOOQ Fetch 1 Greg Turnquest"));
assertThat(this.out.toString(), containsString("jOOQ Fetch 2 Craig Walls"));
assertThat(this.out.toString(), containsString("jOOQ SQL "
+ "[Learning Spring Boot : Greg Turnquest, "
+ "Spring Boot in Action : Craig Walls]"));
}
}