Available on Gradle public repository : https://plugins.gradle.org/plugin/org.jet.sql.codegen
Gradle plugin to reduce boiler-plate code for running JDBC queries in java and improve type safety while running PreparedStatements in java.
This plugin requires simple YAML files and a gradle plugin to generate wrapper java classes that abstract away the boiler-plate code.
- Step 1
// Add the gradle plugin.
plugins {
id 'org.jet.sql.codegen' version '1.1.2'
}
sqlWrapperConfig {
sources = fileTree(dir: 'src/main/resources/sql') // source directory for YAML sql configuration
generatedFileDirectory = file('src/gen/java') // output directory for generated classes.
userName = 'testUser' // user name required to connect to the database
password = 'testPassword' // password required to connect to the database
type = 'postgresql' or 'mysql' // type of the database to connect. Currently only supports postgres and mysql
host = 'testHost' // hostname where the database server is deployed.
}
- Step 2 : Add the YAML files to the source directory.
Sample Yaml file
packageName: org.jet.test
className: EmployeeQueries
queries:
- name: 'get_employee_by_id'
sql: 'SELECT ID, NAME FROM EMPLOYEE WHERE ID = arg_id'
- Step 3 : Generate required java classes by running the gradle plugin task
./gradlew generateSqlWrapper
- Step 4 : Use the generated classes
Once the plugin runs , it will auto generate all the required wrapper classes. Which in this example will be a class named
EmployeeQueries
under package
org.jet.test
you can then use it in your code to replace boiler plate code
package org.jet.example;
import org.jet.test.EmployeeQueries;
import java.sql.Connection;
import java.sql.SQLException;
public class EmployeeDao
{
public void getEmployeeById(int id, Connection connection) throws SQLException
{
EmployeeQueries.Runners.GetEmployeeByIdQueryRunner()
.connection(connection)
.ID(id) // set the id before executing.
.executeAsStream() // can also use .execute() or .executeAndReturnIterator()
.forEach(e -> {
System.out.println("Employee Id " + e.id() + ", Employee Name" + e.name());
});
}
}