Spring JDBC SQL 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.
-
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 of1: N
result data toAggregate
object graph byLEFT OUTER JOIN
lookup -
JdbcRepository
provides insert / update syntax -
Support for setting
Reactive (Flux / Mono)
type as the return type ofCustomRepository
method
- Gradle
buildscript {
repositories {
mavenCentral()
mavenLocal()
maven {
url "https://repo.spring.io/milestone/"
}
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:2.2.6.RELEASE")
}
}
dependencies {
implementation("com.navercorp.spring:spring-boot-starter-data-jdbc-plus-sql:2.0.0.RC2")
implementation("org.springframework.data:spring-data-jdbc:2.0.0.RC2")
implementation("org.springframework.data:spring-data-relational:2.0.0.RC2")
implementation("org.springframework.data:spring-data-commons:2.3.0.RC2")
}
- Maven
<repositories>
<repository>
<id>repository.spring.milestone</id>
<name>Spring Milestone Repository</name>
<url>http://repo.spring.io/milestone</url>
</repository>
</repositories>
<dependency>
<groupId>com.navercorp.spring</groupId>
<artifactId>spring-boot-starter-data-jdbc-plus-sql</artifactId>
<version>2.0.0.RC2</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jdbc</artifactId>
<version>2.0.0.RC2</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational</artifactId>
<version>2.0.0.RC2</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
<version>2.3.0.RC2</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
- It is recommended to use NamedParameter for the parameter to be bound to SQL.
- SQL Injection vulnerability, Prepared Statement Cache, etc. are affected.
- 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
"""
}
}
- naver hackday-conventions-java
- https://github.com/naver/hackday-conventions-java)
- checkstyle: ./rule/naver-checkstyle-rules.xml
- intellij-formatter: ./rule/naver-intellij-formatter.xml (https://naver.github.io/hackday-conventions-java/#editor-config)
$ ./gradlew clean build
Copyright 2020-present 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.