|
1
|
|
|
package easytests.config; |
|
2
|
|
|
|
|
3
|
|
|
import javax.sql.DataSource; |
|
4
|
|
|
import org.apache.ibatis.datasource.pooled.PooledDataSource; |
|
5
|
|
|
import org.apache.ibatis.session.SqlSessionFactory; |
|
6
|
|
|
import org.mybatis.spring.SqlSessionFactoryBean; |
|
7
|
|
|
import org.mybatis.spring.annotation.MapperScan; |
|
8
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
9
|
|
|
import org.springframework.context.annotation.Bean; |
|
10
|
|
|
import org.springframework.context.annotation.Configuration; |
|
11
|
|
|
import org.springframework.context.annotation.PropertySource; |
|
12
|
|
|
import org.springframework.core.env.Environment; |
|
13
|
|
|
import org.springframework.jdbc.datasource.DataSourceTransactionManager; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @author malinink |
|
17
|
|
|
*/ |
|
18
|
|
|
@Configuration |
|
19
|
|
|
@MapperScan("easytests.core.mappers") |
|
20
|
|
|
@PropertySource("classpath:database.properties") |
|
21
|
|
|
public class DatabaseConfig { |
|
22
|
|
|
|
|
23
|
|
|
@Autowired |
|
24
|
|
|
private Environment env; |
|
25
|
|
|
|
|
26
|
|
|
@Bean |
|
27
|
|
|
public DataSource getDataSource() { |
|
28
|
|
|
final PooledDataSource dataSource = new PooledDataSource(); |
|
29
|
|
|
dataSource.setDriver(env.getProperty("driver")); |
|
30
|
|
|
dataSource.setUrl(env.getProperty("url")); |
|
31
|
|
|
dataSource.setUsername(env.getProperty("username")); |
|
32
|
|
|
dataSource.setPassword(env.getProperty("password")); |
|
33
|
|
|
return dataSource; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
@Bean |
|
37
|
|
|
public DataSourceTransactionManager transactionManager() { |
|
38
|
|
|
return new DataSourceTransactionManager(getDataSource()); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
@Bean |
|
42
|
|
|
public SqlSessionFactory sqlSessionFactory() throws Exception { |
|
43
|
|
|
final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); |
|
44
|
|
|
sessionFactory.setDataSource(getDataSource()); |
|
45
|
|
|
return sessionFactory.getObject(); |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
|