11 changed files with 300 additions and 99 deletions
@ -0,0 +1,20 @@
@@ -0,0 +1,20 @@
|
||||
/* |
||||
* Copyright 2002-2024 the original author or authors. |
||||
* |
||||
* 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 |
||||
* |
||||
* https://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. |
||||
*/ |
||||
|
||||
package org.springframework.docs.dataaccess.jdbc.jdbcJdbcTemplateidioms; |
||||
|
||||
public interface CorporateEventDao { |
||||
} |
||||
@ -0,0 +1,20 @@
@@ -0,0 +1,20 @@
|
||||
/* |
||||
* Copyright 2002-2024 the original author or authors. |
||||
* |
||||
* 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 |
||||
* |
||||
* https://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. |
||||
*/ |
||||
|
||||
package org.springframework.docs.dataaccess.jdbc.jdbcJdbcTemplateidioms; |
||||
|
||||
public interface CorporateEventRepository { |
||||
} |
||||
@ -0,0 +1,32 @@
@@ -0,0 +1,32 @@
|
||||
/* |
||||
* Copyright 2002-2024 the original author or authors. |
||||
* |
||||
* 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 |
||||
* |
||||
* https://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. |
||||
*/ |
||||
|
||||
package org.springframework.docs.dataaccess.jdbc.jdbcJdbcTemplateidioms; |
||||
|
||||
import javax.sql.DataSource; |
||||
|
||||
import org.springframework.jdbc.core.JdbcTemplate; |
||||
|
||||
public class JdbcCorporateEventDao implements CorporateEventDao { |
||||
|
||||
private JdbcTemplate jdbcTemplate; |
||||
|
||||
public void setDataSource(DataSource dataSource) { |
||||
this.jdbcTemplate = new JdbcTemplate(dataSource); |
||||
} |
||||
|
||||
// JDBC-backed implementations of the methods on the CorporateEventDao follow...
|
||||
} |
||||
@ -0,0 +1,30 @@
@@ -0,0 +1,30 @@
|
||||
package org.springframework.docs.dataaccess.jdbc.jdbcJdbcTemplateidioms; |
||||
|
||||
import javax.sql.DataSource; |
||||
|
||||
import org.apache.commons.dbcp2.BasicDataSource; |
||||
|
||||
import org.springframework.context.annotation.Bean; |
||||
import org.springframework.context.annotation.Configuration; |
||||
|
||||
@Configuration |
||||
public class JdbcCorporateEventDaoConfiguration { |
||||
|
||||
// tag::snippet[]
|
||||
@Bean |
||||
JdbcCorporateEventDao corporateEventDao(DataSource dataSource) { |
||||
return new JdbcCorporateEventDao(); |
||||
} |
||||
|
||||
@Bean(destroyMethod = "close") |
||||
BasicDataSource dataSource() { |
||||
BasicDataSource dataSource = new BasicDataSource(); |
||||
dataSource.setDriverClassName("org.hsqldb.jdbcDriver"); |
||||
dataSource.setUrl("jdbc:hsqldb:hsql://localhost:"); |
||||
dataSource.setUsername("sa"); |
||||
dataSource.setPassword(""); |
||||
return dataSource; |
||||
} |
||||
// end::snippet[]
|
||||
|
||||
} |
||||
@ -0,0 +1,37 @@
@@ -0,0 +1,37 @@
|
||||
/* |
||||
* Copyright 2002-2024 the original author or authors. |
||||
* |
||||
* 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 |
||||
* |
||||
* https://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. |
||||
*/ |
||||
|
||||
package org.springframework.docs.dataaccess.jdbc.jdbcJdbcTemplateidioms; |
||||
|
||||
import javax.sql.DataSource; |
||||
|
||||
import org.springframework.jdbc.core.JdbcTemplate; |
||||
import org.springframework.stereotype.Repository; |
||||
|
||||
// tag::snippet[]
|
||||
@Repository |
||||
public class JdbcCorporateEventRepository implements CorporateEventRepository { |
||||
|
||||
private JdbcTemplate jdbcTemplate; |
||||
|
||||
// Implicitly autowire the DataSource constructor parameter
|
||||
public JdbcCorporateEventRepository(DataSource dataSource) { |
||||
this.jdbcTemplate = new JdbcTemplate(dataSource); |
||||
} |
||||
|
||||
// JDBC-backed implementations of the methods on the CorporateEventRepository follow...
|
||||
} |
||||
// end::snippet[]
|
||||
@ -0,0 +1,25 @@
@@ -0,0 +1,25 @@
|
||||
package org.springframework.docs.dataaccess.jdbc.jdbcJdbcTemplateidioms; |
||||
|
||||
import org.apache.commons.dbcp2.BasicDataSource; |
||||
|
||||
import org.springframework.context.annotation.Bean; |
||||
import org.springframework.context.annotation.ComponentScan; |
||||
import org.springframework.context.annotation.Configuration; |
||||
|
||||
// tag::snippet[]
|
||||
@Configuration |
||||
@ComponentScan("org.springframework.docs.dataaccess.jdbc") |
||||
public class JdbcCorporateEventRepositoryConfiguration { |
||||
|
||||
@Bean(destroyMethod = "close") |
||||
BasicDataSource dataSource() { |
||||
BasicDataSource dataSource = new BasicDataSource(); |
||||
dataSource.setDriverClassName("org.hsqldb.jdbcDriver"); |
||||
dataSource.setUrl("jdbc:hsqldb:hsql://localhost:"); |
||||
dataSource.setUsername("sa"); |
||||
dataSource.setPassword(""); |
||||
return dataSource; |
||||
} |
||||
|
||||
} |
||||
// end::snippet[]
|
||||
@ -0,0 +1,41 @@
@@ -0,0 +1,41 @@
|
||||
/* |
||||
* Copyright 2002-2024 the original author or authors. |
||||
* |
||||
* 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 |
||||
* |
||||
* https://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. |
||||
*/ |
||||
|
||||
package org.springframework.docs.dataaccess.jdbc.jdbcJdbcTemplateidioms |
||||
|
||||
import org.apache.commons.dbcp2.BasicDataSource |
||||
import org.springframework.context.annotation.Bean |
||||
import org.springframework.context.annotation.Configuration |
||||
import org.springframework.docs.dataaccess.jdbc.JdbcCorporateEventDao |
||||
import javax.sql.DataSource |
||||
|
||||
@Configuration |
||||
class JdbcCorporateEventDaoConfiguration { |
||||
|
||||
// tag::snippet[] |
||||
@Bean |
||||
fun corporateEventDao(dataSource: DataSource) = JdbcCorporateEventDao() |
||||
|
||||
@Bean(destroyMethod = "close") |
||||
fun dataSource() = BasicDataSource().apply { |
||||
driverClassName = "org.hsqldb.jdbcDriver" |
||||
url = "jdbc:hsqldb:hsql://localhost:" |
||||
username = "sa" |
||||
password = "" |
||||
} |
||||
// end::snippet[] |
||||
|
||||
} |
||||
@ -0,0 +1,38 @@
@@ -0,0 +1,38 @@
|
||||
/* |
||||
* Copyright 2002-2024 the original author or authors. |
||||
* |
||||
* 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 |
||||
* |
||||
* https://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. |
||||
*/ |
||||
|
||||
package org.springframework.docs.dataaccess.jdbc.jdbcJdbcTemplateidioms |
||||
|
||||
import org.apache.commons.dbcp2.BasicDataSource |
||||
import org.springframework.context.annotation.Bean |
||||
import org.springframework.context.annotation.ComponentScan |
||||
import org.springframework.context.annotation.Configuration |
||||
|
||||
// tag::snippet[] |
||||
@Configuration |
||||
@ComponentScan("org.springframework.docs.dataaccess.jdbc") |
||||
class JdbcCorporateEventRepositoryConfiguration { |
||||
|
||||
@Bean(destroyMethod = "close") |
||||
fun dataSource() = BasicDataSource().apply { |
||||
driverClassName = "org.hsqldb.jdbcDriver" |
||||
url = "jdbc:hsqldb:hsql://localhost:" |
||||
username = "sa" |
||||
password = "" |
||||
} |
||||
|
||||
} |
||||
// end::snippet[] |
||||
@ -0,0 +1,26 @@
@@ -0,0 +1,26 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?> |
||||
<beans xmlns="http://www.springframework.org/schema/beans" |
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
||||
xmlns:context="http://www.springframework.org/schema/context" |
||||
xsi:schemaLocation=" |
||||
http://www.springframework.org/schema/beans |
||||
https://www.springframework.org/schema/beans/spring-beans.xsd |
||||
http://www.springframework.org/schema/context |
||||
https://www.springframework.org/schema/context/spring-context.xsd"> |
||||
|
||||
<!-- tag::snippet[] --> |
||||
<bean id="corporateEventDao" class="org.springframework.docs.dataaccess.jdbc.jdbcJdbcTemplateidioms.JdbcCorporateEventDao"> |
||||
<property name="dataSource" ref="dataSource"/> |
||||
</bean> |
||||
|
||||
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close"> |
||||
<property name="driverClassName" value="${jdbc.driverClassName}"/> |
||||
<property name="url" value="${jdbc.url}"/> |
||||
<property name="username" value="${jdbc.username}"/> |
||||
<property name="password" value="${jdbc.password}"/> |
||||
</bean> |
||||
|
||||
<context:property-placeholder location="jdbc.properties"/> |
||||
<!-- end::snippet[] --> |
||||
|
||||
</beans> |
||||
@ -0,0 +1,25 @@
@@ -0,0 +1,25 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?> |
||||
<beans xmlns="http://www.springframework.org/schema/beans" |
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
||||
xmlns:context="http://www.springframework.org/schema/context" |
||||
xsi:schemaLocation=" |
||||
http://www.springframework.org/schema/beans |
||||
https://www.springframework.org/schema/beans/spring-beans.xsd |
||||
http://www.springframework.org/schema/context |
||||
https://www.springframework.org/schema/context/spring-context.xsd"> |
||||
|
||||
<!-- tag::snippet[] --> |
||||
<!-- Scans within the base package of the application for @Component classes to configure as beans --> |
||||
<context:component-scan base-package="org.springframework.docs.dataaccess.jdbc.jdbcJdbcTemplateidioms" /> |
||||
|
||||
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close"> |
||||
<property name="driverClassName" value="${jdbc.driverClassName}"/> |
||||
<property name="url" value="${jdbc.url}"/> |
||||
<property name="username" value="${jdbc.username}"/> |
||||
<property name="password" value="${jdbc.password}"/> |
||||
</bean> |
||||
|
||||
<context:property-placeholder location="jdbc.properties"/> |
||||
<!-- end::snippet[] --> |
||||
|
||||
</beans> |
||||
Loading…
Reference in new issue