jOOQ Academy ...
This commit is contained in:
parent
88353f19d5
commit
f89b14542a
@ -1,10 +1,7 @@
|
||||
Thanks for downloading jOOQ.
|
||||
Please visit http://www.jooq.org for more information.
|
||||
|
||||
This example was inspired by Petri Kainulainen's excellent blog post:
|
||||
http://www.petrikainulainen.net/programming/jooq/using-jooq-with-spring-configuration/
|
||||
|
||||
To install and run this example, please check out the complete jOOQ repository first, and use Maven to install the latest SNAPSHOT version of jOOQ:
|
||||
This example will guide you through the jOOQ API to get started and productive quickly, with jOOQ.
|
||||
|
||||
```
|
||||
$ pwd
|
||||
@ -13,7 +10,7 @@ $ ls
|
||||
jOOQ jOOQ-meta jOOQ-codegen ...
|
||||
$ mvn clean install
|
||||
...
|
||||
$ cd jOOQ-examples/jOOQ-spring-example
|
||||
$ cd jOOQ-examples/jOOQ-academy
|
||||
...
|
||||
$ mvn clean install
|
||||
```
|
||||
|
||||
@ -51,12 +51,6 @@
|
||||
<type>jar</type>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-test</artifactId>
|
||||
<version>${org.springframework.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
@ -142,6 +136,9 @@
|
||||
<url>${db.url}</url>
|
||||
<username>${db.username}</username>
|
||||
<password>${db.password}</password>
|
||||
|
||||
<!-- See http://stackoverflow.com/a/13082837/521799 -->
|
||||
<delimiterType>row</delimiterType>
|
||||
|
||||
<autocommit>true</autocommit>
|
||||
<srcFiles>
|
||||
@ -155,7 +152,7 @@
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<version>1.3.168</version>
|
||||
<version>${h2.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</plugin>
|
||||
@ -202,6 +199,14 @@
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<version>${h2.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
@ -0,0 +1,79 @@
|
||||
/**
|
||||
* Copyright (c) 2009-2013, Data Geekery GmbH (http://www.datageekery.com)
|
||||
* All rights reserved.
|
||||
*
|
||||
* This work is dual-licensed
|
||||
* - under the Apache Software License 2.0 (the "ASL")
|
||||
* - under the jOOQ License and Maintenance Agreement (the "jOOQ License")
|
||||
* =============================================================================
|
||||
* You may choose which license applies to you:
|
||||
*
|
||||
* - If you're using this work with Open Source databases, you may choose
|
||||
* either ASL or jOOQ License.
|
||||
* - If you're using this work with at least one commercial database, you must
|
||||
* choose jOOQ License
|
||||
*
|
||||
* For more information, please visit http://www.jooq.org/licenses
|
||||
*
|
||||
* Apache Software License 2.0:
|
||||
* -----------------------------------------------------------------------------
|
||||
* 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.
|
||||
*
|
||||
* jOOQ License and Maintenance Agreement:
|
||||
* -----------------------------------------------------------------------------
|
||||
* Data Geekery grants the Customer the non-exclusive, timely limited and
|
||||
* non-transferable license to install and use the Software under the terms of
|
||||
* the jOOQ License and Maintenance Agreement.
|
||||
*
|
||||
* This library is distributed with a LIMITED WARRANTY. See the jOOQ License
|
||||
* and Maintenance Agreement for more details: http://www.jooq.org/licensing
|
||||
*/
|
||||
package org.jooq.academy.section1;
|
||||
|
||||
import static org.jooq.example.db.h2.Tables.AUTHOR;
|
||||
import static org.jooq.impl.DSL.select;
|
||||
|
||||
import org.jooq.academy.tools.Tools;
|
||||
import org.jooq.impl.DSL;
|
||||
|
||||
public class WorkWithTheDSL {
|
||||
|
||||
public static void main(String[] args) {
|
||||
example1_createAndPrintASimpleQuery();
|
||||
example2_executeASimpleQuery();
|
||||
}
|
||||
|
||||
static void example1_createAndPrintASimpleQuery() {
|
||||
|
||||
// This creates a simple query without executing it
|
||||
// By default, a Query's toString() method will print the SQL string to the console
|
||||
Tools.run("Create a simple query without executing it", () ->
|
||||
select(AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME)
|
||||
.from(AUTHOR)
|
||||
.orderBy(AUTHOR.ID)
|
||||
);
|
||||
}
|
||||
|
||||
static void example2_executeASimpleQuery() {
|
||||
|
||||
// All we need to
|
||||
Tools.run("Selecting FIRST_NAME and LAST_NAME from the AUTHOR table", () ->
|
||||
DSL.using(Tools.connection())
|
||||
.select(AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME)
|
||||
.from(AUTHOR)
|
||||
.orderBy(AUTHOR.ID)
|
||||
.fetch()
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,90 @@
|
||||
/**
|
||||
* Copyright (c) 2009-2013, Data Geekery GmbH (http://www.datageekery.com)
|
||||
* All rights reserved.
|
||||
*
|
||||
* This work is dual-licensed
|
||||
* - under the Apache Software License 2.0 (the "ASL")
|
||||
* - under the jOOQ License and Maintenance Agreement (the "jOOQ License")
|
||||
* =============================================================================
|
||||
* You may choose which license applies to you:
|
||||
*
|
||||
* - If you're using this work with Open Source databases, you may choose
|
||||
* either ASL or jOOQ License.
|
||||
* - If you're using this work with at least one commercial database, you must
|
||||
* choose jOOQ License
|
||||
*
|
||||
* For more information, please visit http://www.jooq.org/licenses
|
||||
*
|
||||
* Apache Software License 2.0:
|
||||
* -----------------------------------------------------------------------------
|
||||
* 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.
|
||||
*
|
||||
* jOOQ License and Maintenance Agreement:
|
||||
* -----------------------------------------------------------------------------
|
||||
* Data Geekery grants the Customer the non-exclusive, timely limited and
|
||||
* non-transferable license to install and use the Software under the terms of
|
||||
* the jOOQ License and Maintenance Agreement.
|
||||
*
|
||||
* This library is distributed with a LIMITED WARRANTY. See the jOOQ License
|
||||
* and Maintenance Agreement for more details: http://www.jooq.org/licensing
|
||||
*/
|
||||
package org.jooq.academy.tools;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.util.Properties;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
|
||||
/**
|
||||
* @author Lukas Eder
|
||||
*/
|
||||
public class Tools {
|
||||
|
||||
/**
|
||||
* Get a fresh connection from H2.
|
||||
*/
|
||||
public static Connection connection() {
|
||||
try {
|
||||
Properties properties = new Properties();
|
||||
properties.load(Tools.class.getResourceAsStream("/config.properties"));
|
||||
|
||||
Class.forName(properties.getProperty("db.driver"));
|
||||
|
||||
return DriverManager.getConnection(
|
||||
properties.getProperty("db.url"),
|
||||
properties.getProperty("db.username"),
|
||||
properties.getProperty("db.password"));
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute a runnable with a title for pretty-printing
|
||||
*/
|
||||
public static void run(String title, Supplier<?> supplier) {
|
||||
String dashes = "-----------------------------------------------------------------------------------------------";
|
||||
|
||||
System.out.println();
|
||||
System.out.println(title);
|
||||
System.out.println(dashes);
|
||||
System.out.println();
|
||||
|
||||
System.out.println(supplier.get());
|
||||
System.out.println();
|
||||
System.out.println(dashes);
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,46 +1,46 @@
|
||||
DROP TABLE IF EXISTS book_to_book_store;
|
||||
DROP TABLE IF EXISTS book_store;
|
||||
DROP TABLE IF EXISTS book;
|
||||
DROP TABLE IF EXISTS author;
|
||||
DROP TABLE IF EXISTS book_to_book_store
|
||||
;
|
||||
DROP TABLE IF EXISTS book_store
|
||||
;
|
||||
DROP TABLE IF EXISTS book
|
||||
;
|
||||
DROP TABLE IF EXISTS author
|
||||
;
|
||||
|
||||
DROP SEQUENCE IF EXISTS s_author_id;
|
||||
CREATE SEQUENCE s_author_id START WITH 1;
|
||||
DROP SEQUENCE IF EXISTS s_author_id
|
||||
;
|
||||
CREATE SEQUENCE s_author_id START WITH 1
|
||||
;
|
||||
|
||||
CREATE TABLE author (
|
||||
id INT NOT NULL,
|
||||
first_name VARCHAR(50),
|
||||
last_name VARCHAR(50) NOT NULL,
|
||||
date_of_birth DATE,
|
||||
year_of_birth INT,
|
||||
address VARCHAR(50),
|
||||
|
||||
CONSTRAINT pk_t_author PRIMARY KEY (ID)
|
||||
);
|
||||
)
|
||||
;
|
||||
|
||||
CREATE TABLE book (
|
||||
id INT NOT NULL,
|
||||
author_id INT NOT NULL,
|
||||
co_author_id INT,
|
||||
details_id INT,
|
||||
title VARCHAR(400) NOT NULL,
|
||||
published_in INT,
|
||||
language_id INT,
|
||||
content_text CLOB,
|
||||
content_pdf BLOB,
|
||||
|
||||
rec_version INT,
|
||||
rec_timestamp TIMESTAMP,
|
||||
|
||||
CONSTRAINT pk_t_book PRIMARY KEY (id),
|
||||
CONSTRAINT fk_t_book_author_id FOREIGN KEY (author_id) REFERENCES author(id),
|
||||
CONSTRAINT fk_t_book_co_author_id FOREIGN KEY (co_author_id) REFERENCES author(id)
|
||||
);
|
||||
)
|
||||
;
|
||||
|
||||
CREATE TABLE book_store (
|
||||
name VARCHAR(400) NOT NULL,
|
||||
|
||||
CONSTRAINT uk_t_book_store_name PRIMARY KEY(name)
|
||||
);
|
||||
)
|
||||
;
|
||||
|
||||
CREATE TABLE book_to_book_store (
|
||||
book_store_name VARCHAR(400) NOT NULL,
|
||||
@ -54,25 +54,51 @@ CREATE TABLE book_to_book_store (
|
||||
CONSTRAINT fk_b2bs_b_id FOREIGN KEY (book_id)
|
||||
REFERENCES book (id)
|
||||
ON DELETE CASCADE
|
||||
);
|
||||
)
|
||||
;
|
||||
|
||||
INSERT INTO author VALUES (next value for s_author_id, 'George', 'Orwell', '1903-06-25', 1903, null);
|
||||
INSERT INTO author VALUES (next value for s_author_id, 'Paulo', 'Coelho', '1947-08-24', 1947, null);
|
||||
INSERT INTO author VALUES (next value for s_author_id, 'George', 'Orwell', '1903-06-25')
|
||||
;
|
||||
INSERT INTO author VALUES (next value for s_author_id, 'Paulo', 'Coelho', '1947-08-24')
|
||||
;
|
||||
|
||||
INSERT INTO book VALUES (1, 1, null, null, '1984', 1948, 1, 'To know and not to know, to be conscious of complete truthfulness while telling carefully constructed lies, to hold simultaneously two opinions which cancelled out, knowing them to be contradictory and believing in both of them, to use logic against logic, to repudiate morality while laying claim to it, to believe that democracy was impossible and that the Party was the guardian of democracy, to forget, whatever it was necessary to forget, then to draw it back into memory again at the moment when it was needed, and then promptly to forget it again, and above all, to apply the same process to the process itself -- that was the ultimate subtlety; consciously to induce unconsciousness, and then, once again, to become unconscious of the act of hypnosis you had just performed. Even to understand the word ''doublethink'' involved the use of doublethink..', null, 1, '2010-01-01 00:00:00');
|
||||
INSERT INTO book VALUES (2, 1, null, null, 'Animal Farm', 1945, 1, null, null, null, '2010-01-01 00:00:00');
|
||||
INSERT INTO book VALUES (3, 2, null, null, 'O Alquimista', 1988, 4, null, null, 1, null);
|
||||
INSERT INTO book VALUES (4, 2, null, null, 'Brida', 1990, 2, null, null, null, null);
|
||||
INSERT INTO book VALUES (1, 1, '1984' , 1948, null)
|
||||
;
|
||||
INSERT INTO book VALUES (2, 1, 'Animal Farm' , 1945, null)
|
||||
;
|
||||
INSERT INTO book VALUES (3, 2, 'O Alquimista', 1988, null)
|
||||
;
|
||||
INSERT INTO book VALUES (4, 2, 'Brida' , 1990, null)
|
||||
;
|
||||
|
||||
INSERT INTO book_store (name) VALUES
|
||||
('Orell Füssli'),
|
||||
('Ex Libris'),
|
||||
('Buchhandlung im Volkshaus');
|
||||
('Amazon'),
|
||||
('Barnes and Noble'),
|
||||
('Payot')
|
||||
;
|
||||
|
||||
INSERT INTO book_to_book_store VALUES
|
||||
('Orell Füssli', 1, 10),
|
||||
('Orell Füssli', 2, 10),
|
||||
('Orell Füssli', 3, 10),
|
||||
('Ex Libris', 1, 1),
|
||||
('Ex Libris', 3, 2),
|
||||
('Buchhandlung im Volkshaus', 3, 1);
|
||||
('Amazon', 1, 10),
|
||||
('Amazon', 2, 10),
|
||||
('Amazon', 3, 10),
|
||||
('Barnes and Noble', 1, 1),
|
||||
('Barnes and Noble', 3, 2),
|
||||
('Payot', 3, 1)
|
||||
;
|
||||
|
||||
DROP ALIAS IF EXISTS count_books
|
||||
;
|
||||
|
||||
CREATE OR REPLACE ALIAS count_books AS $$
|
||||
int countBooks(Connection c, int authorID) throws SQLException {
|
||||
try (PreparedStatement s = c.prepareStatement("SELECT COUNT(*) FROM book WHERE author_id = ?")) {
|
||||
s.setInt(1, authorID);
|
||||
|
||||
try (ResultSet rs = s.executeQuery()) {
|
||||
rs.next();
|
||||
return rs.getInt(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
$$
|
||||
;
|
||||
@ -30,7 +30,7 @@
|
||||
</logger>
|
||||
|
||||
<root>
|
||||
<priority value="debug"/>
|
||||
<priority value="info"/>
|
||||
<appender-ref ref="console"/>
|
||||
</root>
|
||||
</log4j:configuration>
|
||||
|
||||
Loading…
Reference in New Issue
Block a user