之前是没有用过的,然后因为工作就必须用到操作多数据源,也是面向百度编程了,也碰到了很多问题,然后就寻思着记录一下把。
首先看一下包结构吧
pom文件我就不给了,我觉得都能了解多数据源操作的人不会连pom文件都不知道咋写把。
包结构中最重要的就是在dao层下再做细分,把你要操作的数据库对应的dao接口分开,我这里因为我操作的mysql和oracle所以就这么命名了,YeepayDao操作的是mysql里的一个库,PayUserDao操作的是Oracle里的某一个库。
config里就是做数据源的配置了,其他就正常写就行了。
这就是包的结构。
配置文件(application.properties)
如果是yml也可以模仿着来,都是一样的。这一块的写法会影响到后面config包里的数据源配置的写法。
Config包里的东西
MysqlDataSourceConfig和OracleDataSourceConfig就数据源的配置
首先@Primary 表示默认的,必须且指定一个,我指定的是Mysql。
然后就是类上的@MapperScan,里面有个属性”basePackages“,这里就是为什么我说把dao层下做细分的原因了。
@Value就是从配置文件里读取信息出来
MysqlDataSourceConfig
package com.block2025.yeepay.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
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;
/**
* @program: yeepay
* @description:
* @author: LIKEGAKKI
* @create: 2020-09-08 16:18
**/
@Configuration
@MapperScan(basePackages = "com.block2025.yeepay.dao.mysql", sqlSessionTemplateRef = "mysqlSqlSessionTemplate")
public class MysqlDataSourceConfig {
@Value("${spring.datasource.url}")
private String url;
@Value("${spring.datasource.username}")
private String user;
@Value("${spring.datasource.password}")
private String password;
@Value("${spring.datasource.driver-class-name}")
private String driverClass;
@Bean(name = "mysqlDataSource")
@Primary
public DataSource mysqlDataSource(){
DruidDataSource dataSource = new DruidDataSource();
dataSource.setUrl(url);
dataSource.setUsername(user);
dataSource.setPassword(password);
dataSource.setDriverClassName(driverClass);
return dataSource;
}
@Bean(name = "mysqlTransactionManager")
@Primary
public DataSourceTransactionManager mysqlTransactionManager(@Qualifier("mysqlDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "mysqlSqlSessionFactory")
@Primary
public SqlSessionFactory mysqlSqlSessionFactory(@Qualifier("mysqlDataSource") DataSource dataSource) throws Exception {
final SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));
org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
configuration.setMapUnderscoreToCamelCase(true);
configuration.setCallSettersOnNulls(true);
configuration.setLogImpl(org.apache.ibatis.logging.stdout.StdOutImpl.class);
bean.setConfiguration(configuration);
return bean.getObject();
}
@Bean(name = "mysqlSqlSessionTemplate")
@Primary
public SqlSessionTemplate mysqlSqlSessionTemplate(@Qualifier("mysqlSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
OracleDataSourceConfig
package com.block2025.yeepay.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
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.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
/**
* @program: yeepay
* @description:
* @author: LIKEGAKKI
* @create: 2020-09-08 16:18
**/
@Configuration
@MapperScan(basePackages = "com.block2025.yeepay.dao.oracle", sqlSessionTemplateRef = "oracleSqlSessionTemplate")
public class OracleDataSourceConfig {
@Value("${oracle.datasource.url}")
private String url;
@Value("${oracle.datasource.username}")
private String user;
@Value("${oracle.datasource.password}")
private String password;
@Value("${oracle.datasource.driverClassName}")
private String driverClass;
@Bean(name = "oracleDataSource")
public DataSource oracleDataSource() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(driverClass);
dataSource.setUrl(url);
dataSource.setUsername(user);
dataSource.setPassword(password);
return dataSource;
}
@Bean(name = "oracleTransactionManager")
public DataSourceTransactionManager oracleTransactionManager() {
return new DataSourceTransactionManager(oracleDataSource());
}
@Bean(name = "oracleSqlSessionFactory")
public SqlSessionFactory oracleSqlSessionFactory(@Qualifier("oracleDataSource") DataSource oracleDataSource) throws Exception {
final SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(oracleDataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));
org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
configuration.setMapUnderscoreToCamelCase(true);
configuration.setCallSettersOnNulls(true);
configuration.setLogImpl(org.apache.ibatis.logging.stdout.StdOutImpl.class);
bean.setConfiguration(configuration);
return bean.getObject();
}
@Bean(name = "oracleSqlSessionTemplate")
public SqlSessionTemplate oracleSqlSessionTemplate(@Qualifier("oracleSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}