View Javadoc
1   /*
2    *    Copyright 2016-2025 the original author or 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 examples.spring;
17  
18  import javax.sql.DataSource;
19  
20  import org.mybatis.dynamic.sql.util.spring.NamedParameterJdbcTemplateExtensions;
21  import org.springframework.context.annotation.Bean;
22  import org.springframework.context.annotation.Configuration;
23  import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
24  import org.springframework.jdbc.datasource.DataSourceTransactionManager;
25  import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
26  import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
27  import org.springframework.transaction.PlatformTransactionManager;
28  
29  @Configuration
30  public class SpringConfiguration {
31      @Bean
32      public DataSource datasource() {
33          return new EmbeddedDatabaseBuilder()
34                  .setType(EmbeddedDatabaseType.HSQL)
35                  .generateUniqueName(true)
36                  .addScript("classpath:/examples/simple/CreateSimpleDB.sql")
37                  .build();
38      }
39  
40      @Bean
41      public NamedParameterJdbcTemplate template(DataSource dataSource) {
42          return new NamedParameterJdbcTemplate(dataSource);
43      }
44  
45      @Bean
46      public PlatformTransactionManager transactionManager(DataSource dataSource) {
47          return new DataSourceTransactionManager(dataSource);
48      }
49  
50      @Bean
51      public NamedParameterJdbcTemplateExtensions templateExtensions(NamedParameterJdbcTemplate template) {
52          return new NamedParameterJdbcTemplateExtensions(template);
53      }
54  }