Skip to content

Commit d98aa59

Browse files
authored
imp: Factory to create JdbcTemplate (#2518)
1 parent b008284 commit d98aa59

File tree

5 files changed

+53
-16
lines changed

5 files changed

+53
-16
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2017-2023 original authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.micronaut.data.spring.jdbc;
17+
18+
import io.micronaut.context.annotation.EachBean;
19+
import io.micronaut.context.annotation.Factory;
20+
import io.micronaut.core.annotation.Internal;
21+
import io.micronaut.data.connection.jdbc.advice.DelegatingDataSource;
22+
import org.springframework.jdbc.core.JdbcTemplate;
23+
24+
import javax.sql.DataSource;
25+
26+
/**
27+
* Factory for creating {@link JdbcTemplate} instances for each bean of type {@link DataSource}.
28+
*/
29+
@Internal
30+
@Factory
31+
class JdbcTemplateFactory {
32+
/**
33+
*
34+
* @param dataSource Data Source
35+
* @return JdbcTemplate
36+
*/
37+
@EachBean(DataSource.class)
38+
JdbcTemplate createJdbcTemplate(DataSource dataSource) {
39+
return new JdbcTemplate(DelegatingDataSource.unwrapDataSource(dataSource));
40+
}
41+
}

doc-examples/jdbc-spring-template-example-groovy/src/main/groovy/example/AbstractBookRepository.groovy

+4-4
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ import javax.sql.DataSource
1818
@JdbcRepository(dialect = Dialect.H2)
1919
abstract class AbstractBookRepository implements CrudRepository<@Valid Book, @NotNull Long> {
2020

21-
private final JdbcTemplate jdbcTemplate; //<2>
21+
private final JdbcTemplate jdbcTemplate
2222

23-
AbstractBookRepository(DataSource dataSource) { // <1>
24-
this.jdbcTemplate = new JdbcTemplate(DelegatingDataSource.unwrapDataSource(dataSource)); //<2>
23+
AbstractBookRepository(JdbcTemplate jdbcTemplate) { // <1>
24+
this.jdbcTemplate = jdbcTemplate
2525
}
2626

2727
@Transactional
2828
List<Book> findByTitle(@NonNull @NotNull String title) {
29-
return jdbcTemplate.queryForList('SELECT * FROM Book AS book WHERE book.title = ?', title) // <3>
29+
return jdbcTemplate.queryForList('SELECT * FROM Book AS book WHERE book.title = ?', title) // <2>
3030
.collect(m -> new Book(m.id as Long, m.title as String, m.pages as Integer))
3131
}
3232
}

doc-examples/jdbc-spring-template-example-java/src/main/java/example/AbstractBookRepository.java

+4-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import io.micronaut.context.annotation.Requires;
44
import io.micronaut.core.annotation.NonNull;
5-
import io.micronaut.data.connection.jdbc.advice.DelegatingDataSource;
65
import io.micronaut.data.jdbc.annotation.JdbcRepository;
76
import io.micronaut.data.model.query.builder.sql.Dialect;
87
import io.micronaut.data.repository.CrudRepository;
@@ -19,15 +18,15 @@
1918
@JdbcRepository(dialect = Dialect.H2)
2019
public abstract class AbstractBookRepository implements CrudRepository<@Valid Book, @NotNull Long> {
2120

22-
private final JdbcTemplate jdbcTemplate; //<2>
21+
private final JdbcTemplate jdbcTemplate;
2322

24-
public AbstractBookRepository(DataSource dataSource) { // <1>
25-
this.jdbcTemplate = new JdbcTemplate(DelegatingDataSource.unwrapDataSource(dataSource)); //<2>
23+
public AbstractBookRepository(JdbcTemplate jdbcTemplate) { // <1>
24+
this.jdbcTemplate = jdbcTemplate;
2625
}
2726

2827
@Transactional
2928
public List<Book> findByTitle(@NonNull @NotNull String title) {
30-
return jdbcTemplate.queryForList("SELECT * FROM Book AS book WHERE book.title = ?", title) // <3>
29+
return jdbcTemplate.queryForList("SELECT * FROM Book AS book WHERE book.title = ?", title) // <2>
3130
.stream()
3231
.map(m -> new Book((Long) m.get("id"), (String) m.get("title"), (Integer) m.get("pages")))
3332
.toList();

doc-examples/jdbc-spring-template-example-kotlin/src/main/kotlin/example/AbstractBookRepository.kt

+2-4
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,11 @@ import org.springframework.jdbc.core.JdbcTemplate
1313
@Requires(property = "spec.name", value = "BookRepositoryTest") // tag::clazz[]
1414
// tag::clazz[]
1515
@JdbcRepository(dialect = Dialect.H2)
16-
abstract class AbstractBookRepository(dataSource: DataSource) : CrudRepository<@Valid Book, Long> { // <1>
17-
18-
private val jdbcTemplate: JdbcTemplate = JdbcTemplate(DelegatingDataSource.unwrapDataSource(dataSource)) //<2>
16+
abstract class AbstractBookRepository(val jdbcTemplate: JdbcTemplate) : CrudRepository<@Valid Book, Long> { //<1>
1917

2018
@Transactional
2119
open fun findByTitle(title: String) = jdbcTemplate
22-
.queryForList("SELECT * FROM Book AS book WHERE book.title = ?", title) // <3>
20+
.queryForList("SELECT * FROM Book AS book WHERE book.title = ?", title) // <2>
2321
.map { m -> Book(m["id"] as Long, m["title"] as String, m["pages"] as Int) }
2422
}
2523
// end::clazz[]

src/main/docs/guide/dbc/dbcRepositories.adoc

+2-3
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,8 @@ The following code illustrates an example that integrates a `JdbcTemplate` insta
4646

4747
snippet::example.AbstractBookRepository[project-base="doc-examples/jdbc-spring-template-example",source="main",tags="clazz"]
4848

49-
<1> Inject the `java.sql.DataSource` configured by the application.
50-
<2> Instantiate a `JdbcTemplate` object using the injected `DataSource`.
51-
<3> Now the `JdbcTemplate` API can be used to implement repository methods.
49+
<1> Inject the `org.springframework.jdbc.core.JdbcTemplate` configured by the application.
50+
<2> Now the `JdbcTemplate` API can be used to implement repository methods.
5251

5352
In addition, the transaction manager for Spring JDBC needs to be set in application configuration.
5453

0 commit comments

Comments
 (0)