Skip to content

Commit 340993a

Browse files
committed
JOOQ Example
1 parent 645e0d6 commit 340993a

File tree

10 files changed

+746
-0
lines changed

10 files changed

+746
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.idea
2+
*.iml
3+
target
4+
*.DS_Store
5+
.mvn
6+
mvn*
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
CREATE DATABASE `library`;
2+
3+
USE `library`;
4+
5+
CREATE TABLE `author` (
6+
`id` int NOT NULL,
7+
`first_name` varchar(255) DEFAULT NULL,
8+
`last_name` varchar(255) DEFAULT NULL,
9+
PRIMARY KEY (`id`)
10+
);
11+
12+
INSERT INTO author(1, 'Hello', 'Koding');
13+
INSERT INTO author(2, 'jOOQ', 'Example');
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.hellokoding</groupId>
7+
<artifactId>hello-jooq</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<name>Hello jOOQ</name>
12+
<description>Hello jOOQ</description>
13+
14+
<properties>
15+
<maven.compiler.source>1.7</maven.compiler.source>
16+
<maven.compiler.target>1.7</maven.compiler.target>
17+
</properties>
18+
19+
<dependencies>
20+
<dependency>
21+
<groupId>mysql</groupId>
22+
<artifactId>mysql-connector-java</artifactId>
23+
<version>6.0.3</version>
24+
</dependency>
25+
<dependency>
26+
<groupId>org.jooq</groupId>
27+
<artifactId>jooq</artifactId>
28+
<version>3.8.3</version>
29+
</dependency>
30+
<dependency>
31+
<groupId>org.jooq</groupId>
32+
<artifactId>jooq-meta</artifactId>
33+
<version>3.8.3</version>
34+
</dependency>
35+
<dependency>
36+
<groupId>org.jooq</groupId>
37+
<artifactId>jooq-codegen</artifactId>
38+
<version>3.8.3</version>
39+
</dependency>
40+
</dependencies>
41+
42+
<build>
43+
<plugins>
44+
<plugin>
45+
<groupId>org.jooq</groupId>
46+
<artifactId>jooq-codegen-maven</artifactId>
47+
<version>3.8.3</version>
48+
49+
<!-- The plugin should hook into the generate goal -->
50+
<executions>
51+
<execution>
52+
<goals>
53+
<goal>generate</goal>
54+
</goals>
55+
</execution>
56+
</executions>
57+
58+
<dependencies/>
59+
60+
<configuration>
61+
<jdbc>
62+
<driver>${jdbc.driver}</driver>
63+
<url>${jdbc.url}</url>
64+
<user>${jdbc.user}</user>
65+
<password>${jdbc.password}</password>
66+
</jdbc>
67+
68+
<generator>
69+
<database>
70+
<name>org.jooq.util.mysql.MySQLDatabase</name>
71+
<includes>.*</includes>
72+
<excludes></excludes>
73+
<inputSchema>library</inputSchema>
74+
</database>
75+
<target>
76+
<packageName>com.hellokoding.jooq.model</packageName>
77+
<directory>src/main/java</directory>
78+
</target>
79+
</generator>
80+
</configuration>
81+
</plugin>
82+
83+
<plugin>
84+
<groupId>org.codehaus.mojo</groupId>
85+
<artifactId>exec-maven-plugin</artifactId>
86+
<version>1.5.0</version>
87+
<executions>
88+
<execution>
89+
<goals>
90+
<goal>exec</goal>
91+
</goals>
92+
</execution>
93+
</executions>
94+
<configuration>
95+
<systemProperties>
96+
<systemProperty>
97+
<key>jdbc.driver</key>
98+
<value>${jdbc.driver}</value>
99+
</systemProperty>
100+
<systemProperty>
101+
<key>jdbc.user</key>
102+
<value>${jdbc.user}</value>
103+
</systemProperty>
104+
<systemProperty>
105+
<key>jdbc.password</key>
106+
<value>${jdbc.password}</value>
107+
</systemProperty>
108+
<systemProperty>
109+
<key>jdbc.url</key>
110+
<value>${jdbc.url}</value>
111+
</systemProperty>
112+
</systemProperties>
113+
</configuration>
114+
</plugin>
115+
</plugins>
116+
</build>
117+
118+
<profiles>
119+
<profile>
120+
<id>default</id>
121+
<activation>
122+
<activeByDefault>true</activeByDefault>
123+
</activation>
124+
<properties>
125+
<jdbc.user>hellokoding</jdbc.user>
126+
<jdbc.password>hellokoding</jdbc.password>
127+
<jdbc.url>jdbc:mysql://localhost:3306/library?serverTimezone=UTC</jdbc.url>
128+
<jdbc.driver>com.mysql.cj.jdbc.Driver</jdbc.driver>
129+
</properties>
130+
</profile>
131+
</profiles>
132+
133+
134+
</project>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.hellokoding.jooq;
2+
3+
import org.jooq.DSLContext;
4+
import org.jooq.Record;
5+
import org.jooq.Result;
6+
import org.jooq.SQLDialect;
7+
import org.jooq.impl.DSL;
8+
9+
import java.sql.Connection;
10+
import java.sql.DriverManager;
11+
12+
import static com.hellokoding.jooq.model.Tables.*;
13+
14+
public class Application {
15+
public static void main(String[] args) throws Exception {
16+
String user = System.getProperty("jdbc.user");
17+
String password = System.getProperty("jdbc.password");
18+
String url = System.getProperty("jdbc.url");
19+
String driver = System.getProperty("jdbc.driver");
20+
21+
Class.forName(driver).newInstance();
22+
try (Connection connection = DriverManager.getConnection(url, user, password)) {
23+
DSLContext dslContext = DSL.using(connection, SQLDialect.MYSQL);
24+
Result<Record> result = dslContext.select().from(AUTHOR).fetch();
25+
26+
for (Record r : result) {
27+
Integer id = r.getValue(AUTHOR.ID);
28+
String firstName = r.getValue(AUTHOR.FIRST_NAME);
29+
String lastName = r.getValue(AUTHOR.LAST_NAME);
30+
31+
System.out.println("ID: " + id + " first name: " + firstName + " last name: " + lastName);
32+
}
33+
}
34+
catch (Exception e) {
35+
e.printStackTrace();
36+
}
37+
}
38+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* This class is generated by jOOQ
3+
*/
4+
package com.hellokoding.jooq.model;
5+
6+
7+
import java.util.ArrayList;
8+
import java.util.Arrays;
9+
import java.util.List;
10+
11+
import javax.annotation.Generated;
12+
13+
import org.jooq.Schema;
14+
import org.jooq.impl.CatalogImpl;
15+
16+
17+
/**
18+
* This class is generated by jOOQ.
19+
*/
20+
@Generated(
21+
value = {
22+
"http://www.jooq.org",
23+
"jOOQ version:3.8.3"
24+
},
25+
comments = "This class is generated by jOOQ"
26+
)
27+
@SuppressWarnings({ "all", "unchecked", "rawtypes" })
28+
public class DefaultCatalog extends CatalogImpl {
29+
30+
private static final long serialVersionUID = -1390638695;
31+
32+
/**
33+
* The reference instance of <code></code>
34+
*/
35+
public static final DefaultCatalog DEFAULT_CATALOG = new DefaultCatalog();
36+
37+
/**
38+
* The schema <code>library</code>.
39+
*/
40+
public final Library LIBRARY = com.hellokoding.jooq.model.Library.LIBRARY;
41+
42+
/**
43+
* No further instances allowed
44+
*/
45+
private DefaultCatalog() {
46+
super("");
47+
}
48+
49+
@Override
50+
public final List<Schema> getSchemas() {
51+
List result = new ArrayList();
52+
result.addAll(getSchemas0());
53+
return result;
54+
}
55+
56+
private final List<Schema> getSchemas0() {
57+
return Arrays.<Schema>asList(
58+
Library.LIBRARY);
59+
}
60+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* This class is generated by jOOQ
3+
*/
4+
package com.hellokoding.jooq.model;
5+
6+
7+
import com.hellokoding.jooq.model.tables.Author;
8+
import com.hellokoding.jooq.model.tables.records.AuthorRecord;
9+
10+
import javax.annotation.Generated;
11+
12+
import org.jooq.UniqueKey;
13+
import org.jooq.impl.AbstractKeys;
14+
15+
16+
/**
17+
* A class modelling foreign key relationships between tables of the <code>library</code>
18+
* schema
19+
*/
20+
@Generated(
21+
value = {
22+
"http://www.jooq.org",
23+
"jOOQ version:3.8.3"
24+
},
25+
comments = "This class is generated by jOOQ"
26+
)
27+
@SuppressWarnings({ "all", "unchecked", "rawtypes" })
28+
public class Keys {
29+
30+
// -------------------------------------------------------------------------
31+
// IDENTITY definitions
32+
// -------------------------------------------------------------------------
33+
34+
35+
// -------------------------------------------------------------------------
36+
// UNIQUE and PRIMARY KEY definitions
37+
// -------------------------------------------------------------------------
38+
39+
public static final UniqueKey<AuthorRecord> KEY_AUTHOR_PRIMARY = UniqueKeys0.KEY_AUTHOR_PRIMARY;
40+
41+
// -------------------------------------------------------------------------
42+
// FOREIGN KEY definitions
43+
// -------------------------------------------------------------------------
44+
45+
46+
// -------------------------------------------------------------------------
47+
// [#1459] distribute members to avoid static initialisers > 64kb
48+
// -------------------------------------------------------------------------
49+
50+
private static class UniqueKeys0 extends AbstractKeys {
51+
public static final UniqueKey<AuthorRecord> KEY_AUTHOR_PRIMARY = createUniqueKey(Author.AUTHOR, "KEY_author_PRIMARY", Author.AUTHOR.ID);
52+
}
53+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/**
2+
* This class is generated by jOOQ
3+
*/
4+
package com.hellokoding.jooq.model;
5+
6+
7+
import com.hellokoding.jooq.model.tables.Author;
8+
9+
import java.util.ArrayList;
10+
import java.util.Arrays;
11+
import java.util.List;
12+
13+
import javax.annotation.Generated;
14+
15+
import org.jooq.Catalog;
16+
import org.jooq.Table;
17+
import org.jooq.impl.SchemaImpl;
18+
19+
20+
/**
21+
* This class is generated by jOOQ.
22+
*/
23+
@Generated(
24+
value = {
25+
"http://www.jooq.org",
26+
"jOOQ version:3.8.3"
27+
},
28+
comments = "This class is generated by jOOQ"
29+
)
30+
@SuppressWarnings({ "all", "unchecked", "rawtypes" })
31+
public class Library extends SchemaImpl {
32+
33+
private static final long serialVersionUID = -273673399;
34+
35+
/**
36+
* The reference instance of <code>library</code>
37+
*/
38+
public static final Library LIBRARY = new Library();
39+
40+
/**
41+
* The table <code>library.author</code>.
42+
*/
43+
public final Author AUTHOR = com.hellokoding.jooq.model.tables.Author.AUTHOR;
44+
45+
/**
46+
* No further instances allowed
47+
*/
48+
private Library() {
49+
super("library", null);
50+
}
51+
52+
53+
/**
54+
* {@inheritDoc}
55+
*/
56+
@Override
57+
public Catalog getCatalog() {
58+
return DefaultCatalog.DEFAULT_CATALOG;
59+
}
60+
61+
@Override
62+
public final List<Table<?>> getTables() {
63+
List result = new ArrayList();
64+
result.addAll(getTables0());
65+
return result;
66+
}
67+
68+
private final List<Table<?>> getTables0() {
69+
return Arrays.<Table<?>>asList(
70+
Author.AUTHOR);
71+
}
72+
}

0 commit comments

Comments
 (0)