有两种方式进行数据源配置,一种比较简单,使用配置文件加上DruidDataSourceBuilder.create().build()创建数据源就可以了,根据相关的资料,我觉得这种配置,只适合于对数据进行监控的情况,比如对于结合mybatis的情况,就不太适用了,我没有找到配置实体类扫描的地方。一种就是手写配置然后自己配置数据源的config就像参考文章1一样。
1.添加依赖
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.23</version> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.3</version> </dependency> <dependency> <groupId>com.microsoft.sqlserver</groupId> <artifactId>mssql-jdbc</artifactId> <version>6.1.0.jre8</version> </dependency>
|
2.编写配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| datasource: sqlserver: driverClassName: com.microsoft.sqlserver.jdbc.SQLServerDriver url: jdbc:sqlserver://xxx:5433;DatabaseName=xxx username: xxx password: xxx connection-timeout: 20000 maximum-pool-size: 5 sqlserver2: driverClassName: com.microsoft.sqlserver.jdbc.SQLServerDriver url: jdbc:sqlserver://xxx:5433;DatabaseName=xxx username: xxx password: xxx connection-timeout: 20000 maximum-pool-size: 5
|
3.编写第一个数据源配置
注意其中的PACKAGE和MAPPER_LOCATION,是你mapper包的位置和xml文件的位置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| import com.alibaba.druid.pool.DruidDataSource; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.annotation.MapperScan; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
@Configuration
@MapperScan(basePackages = SqlServerConfig.PACKAGE, sqlSessionFactoryRef = "sqlserverSqlSessionFactory") public class SqlServerConfig {
static final String PACKAGE = "com.proheng.gis.mapper.sqlserver"; static final String MAPPER_LOCATION = "classpath*:mapper/sqlserver/*.xml";
@Value("${datasource.sqlserver.url}") private String url;
@Value("${datasource.sqlserver.username}") private String user;
@Value("${datasource.sqlserver.password}") private String password;
@Value("${datasource.sqlserver.driverClassName}") private String driverClass;
@Bean(name = "sqlserverDataSource") @Primary public DataSource masterDataSource() { DruidDataSource dataSource = new DruidDataSource(); dataSource.setDriverClassName(driverClass); dataSource.setUrl(url); dataSource.setUsername(user); dataSource.setPassword(password); return dataSource; }
@Bean(name = "sqlserverTransactionManager") @Primary public DataSourceTransactionManager masterTransactionManager() { return new DataSourceTransactionManager(masterDataSource()); }
@Bean(name = "sqlserverSqlSessionFactory") @Primary public SqlSessionFactory masterSqlSessionFactory(@Qualifier("sqlserverDataSource") DataSource masterDataSource) throws Exception { final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); sessionFactory.setDataSource(masterDataSource); sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver() .getResources(SqlServerConfig.MAPPER_LOCATION)); return sessionFactory.getObject(); } }
|
4.编写实体类
1 2 3
| public class RealTime { private Long id; }
|
5.编写mapper
1 2 3 4 5 6 7
| import java.util.List;
public interface RealTimeMapper { List<RealTime> searchHistoryAll(); }
|
6.编写mapper.xml
注意其中的namespace应该换成你自己的包名和实体包路径。
1 2 3 4 5 6 7 8 9 10
| <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.proheng.gis.mapper.sqlserver.RealTimeMapper"> <select id="searchHistoryAll" resultType="com.proheng.gis.entity.sqlserver.RealTime"> SELECT * FROM realtime </select> </mapper>
|
7.自动注入mapper
1 2 3 4 5 6 7
| @Autowired private RealTimeMapper realTimeMapper;
@RequestMapping("/test") public List<RealTime> test(){ return realTimeMapper.searchHistoryAll(); }
|
8.编写第二个数据源配置
第二个数据源配置和第一个数据源配置的区别就是要把SqlServerConfig中的@Primary去掉,然后名称啊,什么都换掉,比如:(注意里面的名字)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| @Configuration
@MapperScan(basePackages = SqlServerConfig2.PACKAGE, sqlSessionFactoryRef = "sqlserver2SqlSessionFactory") public class SqlServerConfig2 {
static final String PACKAGE = "com.proheng.gis.mapper.sqlserver2"; static final String MAPPER_LOCATION = "classpath*:mapper/sqlserver2/*.xml";
@Value("${datasource.sqlserver2.url}") private String url;
@Value("${datasource.sqlserver2.username}") private String user;
@Value("${datasource.sqlserver2.password}") private String password;
@Value("${datasource.sqlserver2.driverClassName}") private String driverClass;
@Bean(name = "sqlserver2DataSource") public DataSource masterDataSource() { DruidDataSource dataSource = new DruidDataSource(); dataSource.setDriverClassName(driverClass); dataSource.setUrl(url); dataSource.setUsername(user); dataSource.setPassword(password); return dataSource; }
@Bean(name = "sqlserver2TransactionManager") public DataSourceTransactionManager masterTransactionManager() { return new DataSourceTransactionManager(masterDataSource()); }
@Bean(name = "sqlserver2SqlSessionFactory") public SqlSessionFactory masterSqlSessionFactory(@Qualifier("sqlserver2DataSource") DataSource masterDataSource) throws Exception { final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); sessionFactory.setDataSource(masterDataSource); sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver() .getResources(SqlServerConfig2.MAPPER_LOCATION)); return sessionFactory.getObject(); } }
|
总结
其实使用Druid配置数据源也好,使用mybatis配置多数据源也好,步骤和原理和使用jpa配置多数据源差不多(Post not found: Spring Boot多数据源及动态数据源 Spring Boot多数据源及动态数据源)
问题
(1) 项目启动失败
将:”classpath:mapper/sqlserver/.xml”,换成了:”classpath:mapper/sqlserver/*.xml”