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 issues.gh324.spring;
17  
18  import javax.sql.DataSource;
19  
20  import org.apache.ibatis.session.SqlSessionFactory;
21  import org.mybatis.spring.SqlSessionFactoryBean;
22  import org.mybatis.spring.annotation.MapperScan;
23  import org.springframework.context.annotation.Bean;
24  import org.springframework.context.annotation.Configuration;
25  import org.springframework.jdbc.datasource.DataSourceTransactionManager;
26  import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
27  import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
28  import org.springframework.transaction.PlatformTransactionManager;
29  
30  @Configuration
31  @MapperScan("issues.gh324")
32  public class TestConfiguration {
33      @Bean
34      public DataSource dataSource() {
35          return new EmbeddedDatabaseBuilder()
36                  .setType(EmbeddedDatabaseType.HSQL)
37                  .generateUniqueName(true)
38                  .addScript("classpath:/issues/gh324/CreateDB.sql")
39                  .build();
40      }
41  
42      @Bean
43      public SqlSessionFactory sqlSessionFactory() throws Exception {
44          SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
45          factoryBean.setDataSource(dataSource());
46          return factoryBean.getObject();
47      }
48  
49      @Bean
50      public PlatformTransactionManager platformTransactionManager() {
51          return new DataSourceTransactionManager(dataSource());
52      }
53  
54      @Bean
55      SpringNameService nameService() {
56          return new SpringNameService();
57      }
58  }