View Javadoc
1   /*
2    *    Copyright 2010-2022 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 org.mybatis.jpetstore.mapper;
17  
18  import javax.sql.DataSource;
19  
20  import org.mybatis.spring.SqlSessionFactoryBean;
21  import org.mybatis.spring.annotation.MapperScan;
22  import org.springframework.context.annotation.Bean;
23  import org.springframework.context.annotation.Configuration;
24  import org.springframework.jdbc.core.JdbcTemplate;
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("org.mybatis.jpetstore.mapper")
32  public class MapperTestContext {
33  
34    @Bean
35    DataSource dataSource() {
36      return new EmbeddedDatabaseBuilder().generateUniqueName(true).setType(EmbeddedDatabaseType.HSQL)
37          .setScriptEncoding("UTF-8").ignoreFailedDrops(true).addScript("database/jpetstore-hsqldb-schema.sql")
38          .addScripts("database/jpetstore-hsqldb-dataload.sql").build();
39    }
40  
41    @Bean
42    PlatformTransactionManager transactionManager() {
43      return new DataSourceTransactionManager(dataSource());
44    }
45  
46    @Bean
47    SqlSessionFactoryBean sqlSessionFactory() {
48      SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
49      factoryBean.setDataSource(dataSource());
50      factoryBean.setTypeAliasesPackage("org.mybatis.jpetstore.domain");
51      return factoryBean;
52    }
53  
54    @Bean
55    JdbcTemplate jdbcTemplate() {
56      return new JdbcTemplate(dataSource());
57    }
58  
59  }