Giter Site home page Giter Site logo

spring-jdbc-plus's Introduction

GitHub release GitHub license

Spring JDBC Plus build Gitter chat

Spring JDBC Plus provides Spring Data JDBC based extension. It provides necessary features when writing more complex SQL than the functions supported by CrudRepository. If you need to use Spring Data JDBC's Persistence features and SQL execution function in combination, Spring JDBC Plus may be an appropriate choice.

Features

  • Support for executing custom SQL SELECT statements

  • Provide BeanParameterSource, MapParameterSource, EntityParameterSource

  • Provide parameter source converters such as Java8Time,Enum, etc.

  • Entity mapping support for complex table join SELECT results

  • AggregateResultSet supports mapping of 1: N result data to Aggregate object graph by LEFT OUTER JOIN lookup

  • JdbcRepository provides insert / update syntax

  • Support for setting Reactive (Flux / Mono) type as the return type of CustomRepository method

  • User Guide

Getting Started (Spring Boot Starter Data JDBC Plus SQL)

  • Gradle
buildscript {
    repositories {
        mavenCentral()
        mavenLocal()
        maven {
            url "https://repo.spring.io/milestone/"
        }
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:2.4.2")
    }
}

dependencies {
    implementation("org.springframework.boot:spring-boot-starter-data-jdbc")
    implementation("com.navercorp.spring:spring-boot-starter-data-jdbc-plus-sql:2.1.6")
}
  • Maven
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.4.2</version>
    <relativePath/>
</parent>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>

<dependency>
    <groupId>com.navercorp.spring</groupId>
    <artifactId>spring-boot-starter-data-jdbc-plus-sql</artifactId>
    <version>2.1.6</version>
</dependency>
  • Java Codes
@Table("n_order")
@Data
public class Order {
    @Id
    @Column("order_no")
    private Long orderNo;

    @Column("price")
    private long price;

    @Column("purchaser_no")
    private String purchaserNo;
}

public interface OrderRepository extends CrudRepository<Order, Long>, OrderRepositoryCustom {
}

public interface OrderRepositoryCustom {
    List<Order> findByPurchaserNo(String purchaserNo);
}

public class OrderRepositoryImpl extends JdbcRepositorySupport<Order> implements OrderRepositoryCustom {
	private final OrderSql sqls;

	public OrderRepositoryImpl(EntityJdbcProvider entityJdbcProvider) {
		super(Order.class, entityJdbcProvider);
		this.sql = sqls(OrderSql::new);
	}

	@Override
	public List<Order> findByPurchaserNo(String purchaserNo) {
		String sql = this.sql.selectByPurchaserNo();
		return find(sql, mapParameterSource()
			.addValue("purchaserNo", purchaserNo));
	}
}
  • Groovy codes for SQL
class OrderSql extends SqlGeneratorSupport {

    String selectByPurchaserNo() {
        """
        SELECT ${sql.columns(Order)}
        FROM n_order
        WHERE purchaser_no = :purchaserNo
        """
    }
}

Cautions when writing SQL

  • Must use named parameters to pass parameters to SQL.
  • If parameter values are concatenated directly to String, it produces bad effects.
    • May cause SQL injection vulnerability.
    • Reduce efficiency of caches in PreparedStatement and NamedParameterJdbcTemplate

Be careful when use string interpolation in Groovy and Kotlin.

  • Bad ๐Ÿ‘Ž
class OrderSql extends SqlGeneratorSupport {

    String selectByPurchaserNo(String purchaserNo) {
        """
        SELECT ${sql.columns(Order)}
        FROM n_order
        WHERE purchaser_no = '${purchaserNo}'
        """
    }
}
  • Good ๐Ÿ‘
class OrderSql extends SqlGeneratorSupport {

    String selectByPurchaserNo() {
        """
        SELECT ${sql.columns(Order)}
        FROM n_order
        WHERE purchaser_no = :purchaserNo
        """
    }
}

Examples

Getting Help

Coding Convention

Building from Source

$  ./gradlew clean build

License

   Copyright 2020-2021 NAVER Corp.

   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.

spring-jdbc-plus's People

Contributors

iam20 avatar naveross avatar daemin-hwang avatar junoyoon avatar benelog avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.