[JAVA] I tried using JOOQ with Gradle

I tried using JOOQ with Gradle

Note: This article is a memorandum when using JOOQ in Tomcat (JavaEE) of IntelliJ Community (free version). The environment construction of tomcat using Gradle in IntelliJ is explained in the following link. Prepare the execution environment of Tomcat in IntelliJ Community It seems that ON DUPLICATE KEY UPDATE cannot be used with iciql (code generation also failed), so I migrated.

In addition, the environment is assumed to be Windows 10.

Add jooq to external library

Add the following to the dependencies of build.gradle.

dependencies {
    //Add database connector
    //(In the case of mysql, if you specify a version of 6 or higher, you will get a TimeZone related error.)
    compile group: 'mysql', name: 'mysql-connector-java', version: '5.1.47'

    //Added jooq related jar
    compile group: 'org.jooq', name: 'jooq', version: '3.11.2'
    compile group: 'org.jooq', name: 'jooq-parent', version: '3.11.2'/*, ext: 'pom'*/
    compile group: 'org.jooq', name: 'jooq-meta', version: '3.11.2'
    compile group: 'org.jooq', name: 'jooq-meta-extensions', version: '3.11.2'
    compile group: 'org.jooq', name: 'jooq-codegen', version: '3.11.2'
    compile group: 'org.jooq', name: 'jooq-codegen-maven', version: '3.11.2'
}

It is necessary to add the database connector together, but if you are using MySQL, if you specify version 6.0 or higher, the following error will occur at runtime.

The server time zone value '???? (?W????)' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specific time zone value if you want to utilize time zone support.



## Include a plugin that is useful for generating code with JOOQ
 In order to install [JOOQ plugins](https://plugins.gradle.org/plugin/nu.studer.jooq), add plugins of build.gradle.

```groovy
plugins {
    id 'nu.studer.jooq' version '3.0.2'
}

Set up the JOOQ plugin

You can configure the JOOQ plugin by declaring jooq {} in build.gradle and writing it inside.


dependencies {
    jooqRuntime group: 'mysql', name: 'mysql-connector-java', version: '5.1.47'
}

jooq {
    //Specify the version of jooq
    version = '3.11.2'
    edition = 'OSS'
    tables(sourceSets.main) {
        jdbc {
            //Database driver fully qualified class name
            driver = 'com.mysql.jdbc.Driver'
            //URL to connect to SQL
            url = 'jdbc:mysql://localhost:3306/Hoge?serverTimezone=JST'
            //Username to log in to the database
            user = 'user'
            //Password to log in to the database
            password = 'password'
        }
        generator {
            name = 'org.jooq.codegen.DefaultGenerator'
            database {
                //Class name representing the target database(See below)
                name = 'org.jooq.meta.mysql.MySQLDatabase'
                inputSchema = ''
                includes = '.*'
                excludes = ''
            }
            target {
                //Package name of the generated code
                packageName = 'jooq'
                //Directory to place the generated code
                directory = 'src/main/java'
            }
        }
    }
}

If you want to use other than MySQL in the database, specify the name as shown in the table below. The jooq class name used to connect to the database.

Database name
ase org.jooq.util.ase.ASEDatabase
cubrid org.jooq.util.cubrid.CUBRIDDatabase
db2 org.jooq.util.db2.DB2Database
derby org.jooq.util.derby.DerbyDatabase
h2 org.jooq.util.h2.H2Database
hsqldb org.jooq.util.hsqldb.HSQLDBDatabase
ingres org.jooq.util.ingres.IngresDatabase
mysql org.jooq.util.mysql.MySQLDatabase
oracle org.jooq.util.oracle.OracleDatabase
postgres org.jooq.util.postgres.PostgresDatabase
sqlite org.jooq.util.sqlite.SQLiteDatabase
sqlserver org.jooq.util.sqlserver.SQLServerDatabase
sybe org.jooq.util.sybase.SybaseDatabase

Jooq Code generation | Java Development

JOOQ code generation

Since there is a task called jooq → generateTablesJooqSchemaSource in the Gradle window, you can automatically generate the corresponding class from the database by executing it.

Let's Programming with JOOQ!

Summary of how to use jOOQ

Recommended Posts

I tried using JOOQ with Gradle
I tried using Realm with Swift UI
I tried using Scalar DL with Docker
I tried using OnlineConverter with SpringBoot + JODConverter
I tried using OpenCV with Java + Tomcat
I tried using Gson
I tried using TestNG
I tried using Galasa
I made blackjack with Ruby (I tried using minitest)
I tried Getting Started with Gradle on Heroku
I tried DI with Ruby
I tried using azure cloud-init
I tried using Apache Wicket
I tried using Java REPL
I tried UPSERT with PostgreSQL.
I tried BIND with Docker
I tried using Spring + Mybatis + DbUnit
I tried morphological analysis with MeCab
I can't install lombok with Gradle.
I tried to interact with Java
I tried UDP communication with Java
I tried using Java8 Stream API
I tried using JWT in Java
I tried GraphQL with Spring Boot
[Android] I tried using Coordinator Layout.
I tried Flyway with Spring Boot
I tried using Pari gp container
I tried using WebAssembly Stadio (2018/4/17 version)
I tried to get started with Swagger using Spring Boot
I tried customizing slim with Scaffold
I tried using the CameraX library with Android Java Fragment
I tried using Java memo LocalDate
I tried using GoogleHttpClient of Java
I tried connecting to MySQL using JDBC Template with Spring MVC
I tried using Elasticsearch API in Java
I tried using Java's diagnostic tool Arthas
I tried using UICollectionViewListCell added from Xcode12.
I tried to get started with WebAssembly
I tried time-saving management learning with Studyplus.
It's new, but I tried using Groonga
I tried playing with BottomNavigationView a little ①
I tried Lazy Initialization with Spring Boot 2.2.0
I tried to implement ModanShogi with Kinx
I created and set my own Dialect with Thymeleaf and tried using it
I tried Spring.
I tried tomcat
I tried youtubeDataApi.
I tried refactoring ①
I tried FizzBuzz.
I tried JHipster 5.1
I tried using Docker for the first time
I tried using Junit on Mac VScode Maven
[For beginners] I tried using DBUnit in Eclipse
I tried barcode scanning using Rails + React + QuaggaJS
I tried to verify AdoptOpenJDK 11 (11.0.2) with Docker image
I tried to make Basic authentication with Java
I tried to manage struts configuration with Coggle
[For beginners] I tried using JUnit 5 in Eclipse
I tried to manage login information with JMX
I tried writing CRUD with Rails + Vue + devise_token_auth
[Android] I quit SQLite and tried using Realm