一、属性文件配置多数据源
#社区
spring.datasource.commonunity.driver-class-name=org.postgresql.Driver
spring.datasource.commonunity.url=jdbc:postgresql://**:5432/test_**
spring.datasource.commonunity.username=postgres
spring.datasource.commonunity.password=postgres
#flyway
spring.datasource.flyway.driver-class-name=org.postgresql.Driver
spring.datasource.flyway.url=jdbc:postgresql://**:5432/test_**
spring.datasource.flyway.username=postgres
spring.datasource.flyway.password=postgres
二、创建Datasource Bean
@Bean:是一个方法级别上的注解,主要用在@Configuration注解的类里,也可以用在@Component注解的类里。添加的bean的id为方法名
@Primary:指定在同一个接口有多个实现类可以注入的时候,默认选择哪一个,而不是让@Autowire注解报错(一般用于多数据源的情况下)
@Qualifier:指定名称的注入,当一个接口有多个实现类的时候使用
@SpringBootConfiguration public class DataSourceConfig { @Bean @Primary @ConfigurationProperties(prefix = "spring.datasource.commonunity") public DataSource commonunityDataSource() { return DataSourceBuilder.create().build(); } @Bean @ConfigurationProperties(prefix = "spring.datasource.flyway") public DataSource flywayDataSource() { return DataSourceBuilder.create().build(); } }
三、创建DataSourceTransactionManager、SqlSessionFactory、SqlSessionTemplate Bean
SqlSessionFactory:是MyBatis的关键对象,它是个单个数据库映射关系经过编译后的内存镜像.SqlSessionFactory对象的实例可以通过SqlSessionFactoryBuilder对象类获得,而SqlSessionFactoryBuilder则可以从XML配置文件或一个预先定制的Configuration的实例构建出SqlSessionFactory的实例.每一个MyBatis的应用程序都以一个SqlSessionFactory对象的实例为核心.同时SqlSessionFactory也是线程安全的,SqlSessionFactory一旦被创建,应该在应用执行期间都存在.在应用运行期间不要重复创建多次,建议使用单例模式.SqlSessionFactory是创建SqlSession的工厂
SqlSessionTemplate:是MyBatis-Spring的核心。这个类负责管理MyBatis的SqlSession,调用MyBatis的SQL方法,翻译异常。SqlSessionTemplate是线程安全的,可以被多个DAO所共享使用。
当调用SQL方法时,包含从映射器getMapper()方法返回的方法,SqlSessionTemplate将会保证使用的SqlSession是和当前Spring的事务相关的。此外,它管理session的生命周期,包含必要的关闭,提交或回滚操作。
SqlSessionTemplate实现了SqlSession,这就是说要对MyBatis的SqlSession进行简易替换。SqlSessionTemplate对象可以使用SqlSessionFactory作为构造方法的参数来创建
@SpringBootConfiguration @MapperScan(basePackages = {"com.ryj.test.multipleDataSource.dao.commonunity"},sqlSessionFactoryRef = "sqlSessionFactoryCommonunity" ) public class MybatisCommonunityConfig { @Autowired @Qualifier("commonunityDataSource") private DataSource commonunityDataSource; @Bean public DataSourceTransactionManager transactionManagerCommonunity() { return new DataSourceTransactionManager(commonunityDataSource); } @Bean public SqlSessionFactory sqlSessionFactoryCommonunity() throws Exception { SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); sessionFactory.setMapperLocations(resolver.getResources("classpath:/mapper/commonunity/*.xml")); sessionFactory.setDataSource(commonunityDataSource); return sessionFactory.getObject(); } @Bean public SqlSessionTemplate sqlSessionTemplateCommonunity() throws Exception { SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactoryCommonunity()); // 使用上面配置的Factory return template; } }
@SpringBootConfiguration @MapperScan(basePackages = {"com.ryj.test.multipleDataSource.dao.flyway"},sqlSessionFactoryRef = "sqlSessionFactoryFlyway") public class MybatisFlywayConfig { @Autowired @Qualifier("flywayDataSource") private DataSource flywayDataSource; @Bean public DataSourceTransactionManager transactionManagerFlyway() { return new DataSourceTransactionManager(flywayDataSource); } @Bean public SqlSessionFactory sqlSessionFactoryFlyway() throws Exception { SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); sessionFactory.setMapperLocations(resolver.getResources("classpath:/mapper/flyway/*.xml")); sessionFactory.setDataSource(flywayDataSource); return sessionFactory.getObject(); } @Bean public SqlSessionTemplate sqlSessionTemplateFlyway() throws Exception { SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactoryFlyway()); // 使用上面配置的Factory return template; } }