博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringBoot+多数据源(MySQL)
阅读量:6158 次
发布时间:2019-06-21

本文共 4776 字,大约阅读时间需要 15 分钟。

hot3.png

1.引入数据源:

    (1)在application.local.properties 配置多个数据源连接:

spring.datasource.primary.url=XXXXXXXX?useUnicode=true&characterEncoding=UTF-8spring.datasource.primary.username=XXXXXXXXspring.datasource.primary.password=XXXXXXXXspring.datasource.primary.driver-class-name=com.mysql.jdbc.Driverspring.datasource.secondary.url=XXXXXXXX?useUnicode=true&characterEncoding=UTF-8spring.datasource.secondary.username=XXXXXXXXspring.datasource.secondary.password=XXXXXXXXspring.datasource.secondary.driver-class-name=com.mysql.jdbc.Driver

备注:在引入多数据源的时候需要在application.properties配置连接池避免连接超时的问题

spring.datasource.driver-class-name=com.mysql.jdbc.Driverspring.datasource.druid.initial-size=1spring.datasource.druid.min-idle=1spring.datasource.druid.max-active=20spring.datasource.druid.test-on-borrow=truespring.datasource.druid.stat-view-servlet.allow=true

(2)springBoot启动Application类:

@EnableTransactionManagement@SpringBootApplication@ComponentScan(basePackages = {        "com.***.***.***.config"  此处扫描的应该为数据源配置所在的类包})public class SccSupplierApplication {    private static Logger logger = LoggerFactory.getLogger(ServletInitializer.class);    public static void main(String[] args) {        logger.info("SccSupplierApplication starting");        ConfigurableApplicationContext app = SpringApplication.run(SccSupplierApplication.class, args);        logger.info("SccSupplierApplication start success");    }}

(3)数据源的配置:

数据源-01@Configuration@MapperScan(basePackages = {"***.***.***.sysMapper"}, sqlSessionTemplateRef  = "test1SqlSessionTemplate")public class DataSource1 {    @Bean(name = "test1DataSource")    @ConfigurationProperties(prefix ="spring.datasource.primary")    @Primary    public DataSource testDataSource() {        return DataSourceBuilder.create().build();    }    @Bean(name = "test1SqlSessionFactory")    @Primary    public SqlSessionFactory testSqlSessionFactory(@Qualifier("test1DataSource") DataSource dataSource) throws Exception {        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();        bean.setDataSource(dataSource);        Resource[] resources2 = new PathMatchingResourcePatternResolver().getResources("classpath*:**/mapper/*Mapper.xml");        Resource[] resources = new PathMatchingResourcePatternResolver().getResources("classpath*:**/sysMapper/**/*Mapper.xml");        Resource[] resources3 = new Resource[11];        for (int i = 0; i <=10; i++) {            resources3[i]=resources2[i];        }        Resource[] allResource = (Resource[]) ArrayUtils.addAll(resources3, resources);        bean.setMapperLocations(allResource);        return bean.getObject();    }    @Bean(name = "test1TransactionManager")    @Primary    public PlatformTransactionManager testTransactionManager(@Qualifier("test1DataSource") DataSource dataSource) {        return new DataSourceTransactionManager(dataSource);    }    @Bean(name = "test1SqlSessionTemplate")    @Primary    public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("test1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {        return new SqlSessionTemplate(sqlSessionFactory);    }}
数据源-02@Configuration@MapperScan(basePackages ={"com.***.***.mpoMapper"},sqlSessionTemplateRef  = "test2SqlSessionTemplate")public class DataSource2 {    @Bean(name = "test2DataSource")    @ConfigurationProperties(prefix ="spring.datasource.secondary")    public DataSource testDataSource() {        return DataSourceBuilder.create().build();    }    @Bean(name = "test2SqlSessionFactory")    public SqlSessionFactory testSqlSessionFactory(@Qualifier("test2DataSource") DataSource dataSource) throws Exception {        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();        bean.setDataSource(dataSource);        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:**/mpoMapper/*Mapper.xml"));        return bean.getObject();    }    @Bean(name = "test2TransactionManager")    public PlatformTransactionManager testTransactionManager(@Qualifier("test2DataSource") DataSource dataSource) {        return new DataSourceTransactionManager(dataSource);    }    @Bean(name = "test2SqlSessionTemplate")    public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("test2SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {        return new SqlSessionTemplate(sqlSessionFactory);    }}

171851_KrMs_2346823.png

备注:IDEA和Eclipse不太一样,关于扫XML,映射文件的XML需要放在Resource包下,并且需要在POM中加:

src/main/resources
**/*.xml
false

 

最后运行成功。

很多第一次引入多数据源会出现 无效绑定此类异常,这些根本原因就是因为没有扫到对应的Mapper或者XML。

                                                                                                                -----2018-4-13 17:25

 

 

转载于:https://my.oschina.net/u/2346823/blog/1795272

你可能感兴趣的文章
我的友情链接
查看>>
如何做地图标注
查看>>
生产环境该如何选择lvs的工作模式,和哪一种算法
查看>>
iOS 正则表达式判断UITextField是否为全汉字,全字母,全数字,数字和字母
查看>>
NHibernate初探-SQLDialects
查看>>
ElasticSearch之动态映射和模板
查看>>
完全卸载 RAC_clusterware的方法
查看>>
深入理解指针函数
查看>>
shell编程详解(一)
查看>>
我的2012
查看>>
CHARTING图表控件介绍及下载地址
查看>>
JAVA题目一道
查看>>
视频上传、转码、切面、存储的思路
查看>>
mp4无法用于直播流
查看>>
IDEA安装Spring Initializer插件
查看>>
php:自定义session
查看>>
PHP专题-开发基础(七)
查看>>
让 Windows 7 定时待机
查看>>
配置isc-dhcrelay需要注意的事项
查看>>
isc-dhcp监听网口的实现步骤
查看>>