A library that extends the existing JDBC API so that data objects can be used as input (to set parameters) and output (from ResultSet's rows).

Overview

SqlObjectMapper

This is a library that extends the existing JDBC API so that data objects can be used as input (to set parameters) and output (from ResultSet's rows).

Two kinds of data object is currently supported:

  • Java's Bean
  • Kotlin's data class (all parameters of primary constructors must also be properties)

Documentations

Quick start

1. Download the package

Download the package from maven central:

<dependency>
  <groupId>com.qualifiedcactus</groupId>
  <artifactId>sqlObjectMapper</artifactId>
  <version>1.1.0</version>
</dependency>

2. Set default class mapping provider

If you use Java, pick sqlObjectMapper.annotationProcessing.bean.BeanMappingProvider.

ClassMappingProvider.setDefaultClassMappingProvider(new BeanMappingProvider())

If you use Kotlin, pick sqlObjectMapper.annotationProcessing.dataClass.DataClassMappingProvider.

ClassMappingProvider.defaultClassMappingProvider = DataClassMappingProvider()

Note: There should be only 1 instance of each type of ClassMappingProvider (2 mentioned above) because the cache is stored in each instance.

3. Define your data object

Kotlin Data Class:

data class QueryInput(
    val param1: String,
    val param2: String
)

data class Entity1(
    val column1: Int,
    val column2: String,
    val column3: String,
)

Or Java Bean (with lombok):

@Data
public class QueryInput {
    private String param1;
    private String param2;
}

@Data
public class Entity1 {
    private Integer column1;
    private String column2;
    private String column3;
}

4. Execute Query with NpPreparedStatement or NpCallableStatement

Use convenient class QueryExecutor to set query parameters using a DTO and put the result of a query into a list of DTOs.

In kotlin:

val entityList: List<Entity1> = QueryExecutor().queryForList(
    connection, 
    """
    SELECT column_1, column_2, column_3 
    FROM entity_1 
    WHERE column_2 LIKE :param_1 AND column_3 LIKE :param_2
    """,
    QueryInput("abc%", "%def"),
    Entity1::class.java
)

In Java:

public class Main {
    public static void main(String[] args) {
        List<Entity1> entityList = new QueryExecutor().queryForList(
            connection,
            """
            SELECT column_1, column_2, column_3
            FROM entity_1
            WHERE column_2 LIKE :param_1 AND column_3 LIKE :param_2
            """,
            new QueryInput("abc%", "%def"),
            Entity1.class
        );
    }
}

For more examples, see these unit tests:

Motivation

In web development, your interactions with data object tends to be in the following order:

  1. Receive data object from your json parser
  2. Validate it
  3. Parse the data object into your sql parameters and execute a query
  4. Parse the result of a query in to a data object
  5. Send that data object as a response body

In step 3 and 4, you have the options of either do it by hand or automate it using Hibernate, or many other ORM libraries. However, Hibernate sometimes feel too restrictive because too much is abstracted away. That is why I created this library that simply add a data-object-parsing feature to Java's JDBC API.

You might also like...
This is a skeleton project for Zircon users that can be used to get started with Zircon.
This is a skeleton project for Zircon users that can be used to get started with Zircon.

Zircon Kotlin Skeleton Project This is a skeleton project for Zircon users that can be used to get started with Zircon. Getting started This project w

Tool for exporting Old School RuneScape environments so they can be used in 3D modeling programs like Blender.

OSRS Environment Exporter Tool for exporting Old School RuneScape environments so that they can be used in 3D modeling programs like Blender. Download

Use Android Data Binding wih Live Data to glue View Model and Android

Gruop-C Spliff Summary Use Android Data Binding wih Live Data to glue View Model and Android. Asynchronous communications implemented with KotlinX Cor

Demo Spting REST Service on Kotlin. Works with PostgreSQL via Spring Data. Data initialization provided by liquibase

Spring Boot REST API with Kotlin Spring Boot REST API service. Spring Data with PostgreSQL. Data initialization with Liquibase. Swagger UI Reference D

Detailing about the data provided (Data Visualization Application)

Detailing about the data provided (Data Visualization Application): • In the application, the data provided in the CSV is used for the Scatter plot cr

Service exposes sensitive administration APIs to initialize and set lower level of Slurpanize infrastructure

slurpanize-baker Project This project uses Quarkus, the Supersonic Subatomic Java Framework. If you want to learn more about Quarkus, please visit its

A Gradle plugin providing various utility methods and common code required to set up multi-version Minecraft mods.

Essential Gradle Toolkit A Gradle plugin providing various utility methods and common code required to set up multi-version Minecraft mods via archite

An Android app that scans images or human faces in real time and detects whether the mask is worn or not, with the ability to set an audible alert
An Android app that scans images or human faces in real time and detects whether the mask is worn or not, with the ability to set an audible alert

Swift Mask Real time face mask detection Brief overview Swift Mask scans images or human faces in real time and detects whether the mask is worn or no

Basic application that uses Retrofit, Moshi and Coil libraries to parse data from web API

DogAlbum_Api_CodeThrough Basic application that uses Retrofit, Moshi and Coil libraries to parse data from web API This folder contains the completed

Owner
Qualified Cactus
Qualified Cactus
Automatically generates UI demos which allow users to call any function with any parameters

Automatically generates UI demos which allow users to call any function (including composable ones) with any parameters. Useful for building demo screens in playground apps of various design systems.

Anton Popov 3 Jul 28, 2022
Kotlin Symbol Processor library to create Mutable and Immutable variants of objects.

implier Kotlin Symbol Processor plugin to create Mutable and Immutable variants of objects. Examples @ImmutableImpl @MutableImpl public interface Samp

Vadim Yaroschuk 14 Nov 8, 2022
🔥The Android Startup library provides a straightforward, performant way to initialize components at the application startup. Both library developers and app developers can use Android Startup to streamline startup sequences and explicitly set the order of initialization.

??The Android Startup library provides a straightforward, performant way to initialize components at the application startup. Both library developers and app developers can use Android Startup to streamline startup sequences and explicitly set the order of initialization.

Rouse 1.3k Dec 30, 2022
This prototype app provides a list of events to be held under an organization (school, college, club, etc.) and the users can manually set event reminders at their scheduled time so that they do not miss an event.

E-CELL NITS Sample App This prototype app provides a list of events to be held under E-Cell NIT Silchar (for example, Srijan 2.0) and the users can ma

Ritam Nath 1 Nov 7, 2021
FizzBuzzKotlin - A function fizzBuzz to generate a list of string based on the input number

FizzBuzzKotlin write a function fizzBuzz to generate a list of string based on t

gson 0 Feb 12, 2022
Team management service is a production ready and fully tested service that can be used as a template for a microservices development.

team-mgmt-service Description Team management service is a production ready and fully tested service that can be used as a template for a microservice

Albert Llousas Ortiz 18 Oct 10, 2022
Tweaks - A customizable debug screen to view and edit flags that can be used for development

A customizable debug screen to view and edit flags that can be used for developm

TelefĂłnica 10 Jun 28, 2022
An open source app which can be used to do basic surveys

SurveyApp This is an open source app which can be used to do basic surveys. It supports multiple question types. For demo please check the releases pa

Dhiraj Uchil 0 Dec 9, 2021