• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

JAVA开发之springBoot2.0搭建双数据源

springboot 搞代码 4年前 (2022-01-09) 30次浏览 已收录 0个评论

最近因为项目需要,需要在程序中同时访问不同的数据库。之前使用springboot注解写代码确实方便快捷,但是如果需要自己需改其中的一些东西来方便自己使用,改起来有点烦,不知道从哪里入手。写个这个记录下。

首先是pom.xml,都是通用的,这里就不贴出来了。

其次就是application.properties配置,笔者因为不同的环境,所以新建了两个application-dev.properties和application-pro.properties配置文件。

application.properties:指定当前使用那个环境的配置.

spring.profiles.active=dev
application-dev.properties的配置(application-pro.properties类似dev的配置)
#datasource1spring.datasource.username=**spring.datasource.password=*****spring.datasource.jdbc-url=jdbc:mysql://***************useUnicode=true&characterEncoding=utf-8&useSSL=falsespring.datasource.driver-class-name=com.mysql.jdbc.Driver#datasource2spring.datasource.ds1.username=***spring.datasource.ds1.password=******spring.datasource.ds1.jdbc-url=jdbc:mysql://***************useUnicode=true&characterEncoding=utf-8&useSSL=falsespring.datasource.ds1.driver-class-name=com.mysql.jdbc.Drive

不同数据源的MyBatis (Config1)

@Configuration@MapperScan(basePackages = "com.pet.petgame2.mapper.dao", sqlSessionFactoryRef = "petgame2SessionFactory")public class Config1 {    @Bean(name = "petgame2")    @ConfigurationProperties(prefix = "spring.datasource")    @Primary    public DataSource dataSource(){        return DataSourceBuilder.create().build();    }    @Bean(name = "petgame2SessionFactory")    @Primary    public SqlSessionFactory sessionFactory(@Qualifier("petgame2") DataSource dataSource) throws Exception{        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();        factoryBean.setDataSource(dataSource);        factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:Mapper/pet2Mapper/*.xml"));        return factoryBean.getObject();    }    @Bean(name = "petgame2TransactionManager")    @Primary    public DataSourceTransactionManager transactionManager(@Qualifier("petgame2") DataSource dataSource){        return new DataSourceTransactionManager(dataSource);    }}

不同数据源的MyBatis (Config2)

@Configuration@MapperScan(basePackages = "com.pet.petgame2.mapper.dao2", sqlSessionFactoryRef = "petgamesqlSessionFactory")public class Config2 {    @Bean(name = "petgame")    @ConfigurationProperties(prefix = "spring.datasource.ds1")    public DataSource dataSource(){        return DataSourceBuilder.create().build();    }    @Bean(name = "petgamesqlSessionFactory")    public SqlSessionFactory sessionFactory(@Qualifier("petgame") DataSource dataSource) throws Exception{        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();        factoryBean.setDataSource(dataSource);        factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:Mapper/petMapper/*.xml"));        return factoryBean.getObject();    }    @Bean(name = "petgameTransactionManager")    public DataSourceTransactionManager transactionManager(@Qualifier("petgame") DataSource dataSource){        return new DataSourceTransactionManager(dataSource);    }}

@MapperScan注解会自动扫描mapper的接口文件, factoryBean.setMapperLocations会扫描mapper接口文件对应的Mapper.XML文件,注意这两个的路径不要写错。@Primary设定默认的一个数据库链接配置,必须指定其中一个数据源为默认。

*注意:如果使用了springboot2.0自带的Hikari作为连接池,需要注意properties中的

spring.datasource.url改为spring.datasource.jdbc-url

或者修改config中DataSource为DataSourceProperties,如下

@Bean(name = "petgame2DataSourceProperties")    @Qualifier("petgame2DataSourceProperties")    @ConfigurationProperties(prefix = "spring.datasource")    @Primary    public DataSourceProperties properties(){        return new DataSourceProperties();    }    @Bean(name = "petgame2")    @ConfigurationProperties(prefix = "spring.datasource")    @Primary    public DataSource dataSource(){//        return DataSourceBuilder.create().build();        return properties().initializeDataSourceBuilder<p style="color:transparent">本文来源gao!%daima.com搞$代*!码网1</p>().build();    }

以上就是JAVA开发之springBoot2.0搭建双数据源的详细内容,更多请关注搞代码gaodaima其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:JAVA开发之springBoot2.0搭建双数据源

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址