今天做项目正好有需求需要连接其他数据库,我们的目的是直连对方程序的数据库然后把数据拉取过来,因为都是我们的产品所以沟通很方便,我也是参照了网上很多的例子,没啥难度的东西。
pom文件
<dependency> <!-- mysql数据库-->
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.15</version>
<scope>runtime</scope>
</dependency>
<dependency><!--Druid连接池-->
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.12</version>
</dependency>
<dependency><!--sqlsever数据库-->
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>sqljdbc4</artifactId>
<version>4.0</version>
</dependency>
application.properties
spring.datasource.one.url=jdbc:mysql://ip:端口/数据库?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&serverTimezone=Asia/Shanghai
spring.datasource.one.username=账号
spring.datasource.one.password=密码
spring.datasource.one.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.second.url=jdbc:sqlserver://ip:端口;DatabaseName=数据库
spring.datasource.second.username=账号
spring.datasource.second.password=密码
spring.datasource.second.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
mysql
@Configuration
@MapperScan(basePackages = DataBase1.PACKAGE, sqlSessionFactoryRef = "masterSqlSessionFactory")
public class DataBase1 {
static final String PACKAGE = "com.mtkj.dao.mysql"; //dao所在包,配置mysql对应mybatis
static final String MAPPER_LOCATION = "classpath:mapper1/*.xml"; //dao对应的xml文件
@Value("${spring.datasource.one.url}")
private String url;
@Value("${spring.datasource.one.username}")
private String user;
@Value("${spring.datasource.one.password}")
private String password;
@Value("${spring.datasource.one.driver-class-name}")
private String driverClass;
@Bean(name = "masterDataSource")
@Primary
public DataSource masterDataSource() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(driverClass);
dataSource.setUrl(url);
dataSource.setUsername(user);
dataSource.setPassword(password);
return dataSource;
}
@Bean(name = "masterTransactionManager")
@Primary
public DataSourceTransactionManager masterTransactionManager() {
return new DataSourceTransactionManager(masterDataSource());
}
@Bean(name = "masterSqlSessionFactory")
@Primary
public SqlSessionFactory masterSqlSessionFactory(@Qualifier("masterDataSource") DataSource masterDataSource)
throws Exception {
final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(masterDataSource);
sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources(DataBase1.MAPPER_LOCATION));
return sessionFactory.getObject();
}
}
sqlserver
@Configuration
@MapperScan(basePackages = DataBase2.PACKAGE, sqlSessionFactoryRef = "secondSqlSessionFactory")
public class DataBase2 {
static final String PACKAGE = "com.mtkj.dao.sqlserver"; //dao所在包
static final String MAPPER_LOCATION = "classpath:mapper2/*.xml";
@Value("${spring.datasource.second.url}")
private String url;
@Value("${spring.datasource.second.username}")
private String user;
@Value("${spring.datasource.second.password}")
private String password;
@Value("${spring.datasource.second.driver-class-name}")
private String driverClass;
@Bean(name = "secondDataSource")
public DataSource clusterDataSource() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(driverClass);
dataSource.setUrl(url);
dataSource.setUsername(user);
dataSource.setPassword(password);
return dataSource;
}
@Bean(name = "secondTransactionManager")
public DataSourceTransactionManager clusterTransactionManager() {
return new DataSourceTransactionManager(clusterDataSource());
}
@Bean(name = "secondSqlSessionFactory")
public SqlSessionFactory clusterSqlSessionFactory(@Qualifier("secondDataSource") DataSource clusterDataSource)
throws Exception {
final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(clusterDataSource);
sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources(DataBase2.MAPPER_LOCATION));
return sessionFactory.getObject();
}
}
实测没问题,经验少慢慢积累吧。