当前位置: 首页 > news >正文

中国商业网官网seo顾问服务 品达优化

中国商业网官网,seo顾问服务 品达优化,黄冈做网站,百度做网站推广多少钱楔子:当然,世上有很多优秀的女性,我也会被她们吸引。这对男人来说是理所当然的。但目光被吸引和内心被吸引是截然不同的。- 东野圭吾《黎明之街》 今日书签 在一些应用场景中,可能需要连接多个不同的数据库,例如连接不…

楔子:当然,世上有很多优秀的女性,我也会被她们吸引。这对男人来说是理所当然的。但目光被吸引和内心被吸引是截然不同的。- 东野圭吾《黎明之街》

今日书签

在一些应用场景中,可能需要连接多个不同的数据库,例如连接不同的数据库服务器或者连接主从数据库。这段代码就是为了实现这种多数据源的配置。

具体来说,这个类包含两个内部静态类:

  1. MyBatisDataSourceConfiguration4XMei: 这个类配置了第一个数据源,即 “xmei” 数据源。它使用了
    @Primary 注解来指示这是默认的主数据源。这个数据源配置了一个 Druid 数据源,并配置了与该数据源相关的
    SqlSessionFactory、事务管理器(DataSourceTransactionManager)和
    SqlSessionTemplate。@MapperScan 注解用于指示需要扫描哪些包下的 Mapper 接口,并使用特定的
    SqlSessionFactory。它还定义了 Mapper XML 文件的路径,以及事务管理和 SqlSessionTemplate。
  2. MyBatisDataSourceConfiguration4XWei: 这个类配置了第二个数据源,即 “xwei” 数据源。它没有使用
    @Primary 注解,因此不是默认的主数据源。它使用了 @Qualifier 注解来指定特定的 Bean
    名称,用于解决多个数据源的冲突。与第一个数据源类似,它配置了一个 Druid 数据源,并定义了与该数据源相关的
    SqlSessionFactory、事务管理器和 SqlSessionTemplate。

多数据源

使用 Service、Mapper、XML 所在包路径区分默认数据源 与 第二数据源。
直接看代码:

/*** 多数据源接入* dataSource4XMei 为 xmei 库,为默认数据源,正常使用,此处以外无其它配置* dataSource4XWei 为 xwei 库,为第二数据源,正常使用,此处以外无其它配置*/
@Configuration
public class MyBatisConfiguration {/*** 配置 SpringBoot 默认数据源,一般配置为主数据库,此为 xmei*/@Configuration@MapperScan(basePackages = {"com.cw.tan.xmei.persistence.*"}, sqlSessionFactoryRef = "sqlSessionFactory4XMei")protected static class MyBatisDataSourceConfiguration4XMei {@Bean@Primary@ConfigurationProperties("spring.datasource.druid.xmei")public DataSource dataSource4XMei() {return DruidDataSourceBuilder.create().build();}@Bean@Primarypublic SqlSessionFactory sqlSessionFactory4XMei(DataSource dataSource4XMei) throws Exception {SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();sqlSessionFactoryBean.setDataSource(dataSource4XMei);sqlSessionFactoryBean.setMapperLocations(this.resolveMapperLocations());return sqlSessionFactoryBean.getObject();}private Resource[] resolveMapperLocations() {ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();List<String> mapperLocations = new ArrayList<>();mapperLocations.add("classpath*:com/cw/tan/xmei/persistence/**/*.xml");List<Resource> resources = new ArrayList();if (!CollectionUtils.isEmpty(mapperLocations)) {for (String mapperLocation : mapperLocations) {try {Resource[] mappers = resourceResolver.getResources(mapperLocation);resources.addAll(Arrays.asList(mappers));} catch (IOException e) {// ignore}}}return resources.toArray(new Resource[resources.size()]);}/*** 配置事务管理*/@Bean@Primarypublic DataSourceTransactionManager transactionManager4XMei(DataSource dataSource4XMei) {return new DataSourceTransactionManager(dataSource4XMei);}@Bean@Primarypublic SqlSessionTemplate sqlSessionTemplate4XMei(SqlSessionFactory sqlSessionFactory4XMei) {return new SqlSessionTemplate(sqlSessionFactory4XMei);}}/*** 配置第二数据源,注意 mapper 扫描路径和上面的区分开*/@Configuration@MapperScan(basePackages = {"com.cw.tan.xwei.log.mapper","com.cw.tan.xwei.sms.mapper", "com.cw.tan.xwei.job.mapper"}, sqlSessionFactoryRef = "sqlSessionFactory4XWei")protected static class MyBatisDataSourceConfiguration4XWei {@Bean(name = "dataSource4XWei")@ConfigurationProperties("spring.datasource.druid.xwei")public DataSource dataSource4XWei() {return DruidDataSourceBuilder.create().build();}@Bean(name = "sqlSessionFactory4XWei")public SqlSessionFactory sqlSessionFactory4XWei(@Qualifier("dataSource4XWei") DataSource dataSource4XWei) throws Exception {SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();sqlSessionFactoryBean.setDataSource(dataSource4XWei);sqlSessionFactoryBean.setMapperLocations(this.resolveMapperLocations());return sqlSessionFactoryBean.getObject();}private Resource[] resolveMapperLocations() {ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();List<String> mapperLocations = new ArrayList<>();mapperLocations.add("classpath*:com/cw/tan/xwei/log/**/*.xml");mapperLocations.add("classpath*:com/cw/tan/xwei/sms/**/*.xml");mapperLocations.add("classpath*:com/cw/tan/xwei/job/**/*.xml");List<Resource> resources = new ArrayList<>();if (!CollectionUtils.isEmpty(mapperLocations)) {mapperLocations.forEach(mapperLocation -> {try {Resource[] mappers = resourcePatternResolver.getResources(mapperLocation);resources.addAll(Arrays.asList(mappers));} catch (IOException e) {// ignore}});}return resources.toArray(new Resource[resources.size()]);}@Bean(name = "transactionManager4XWei")public DataSourceTransactionManager transactionManager4XWei(@Qualifier("dataSource4XWei") DataSource dataSource4XWei) {return new DataSourceTransactionManager(dataSource4XWei);}@Bean(name = "sqlSessionTemplate4XWei")public SqlSessionTemplate sqlSessionTemplate4XWei(@Qualifier("sqlSessionFactory4XWei") SqlSessionFactory sqlSessionFactory4XWei) {return new SqlSessionTemplate(sqlSessionFactory4XWei);}}
}

注意:上述配置,请确认自己的默认数据源、第二数据源对应Service、DAO文件所在的包路径,如果路径指向不对,可能会出现以下异常:
2023-08-17 16:09:33.981 [TID:] [main] WARN AnnotationConfigServletWebServerApplicationContext.refresh():559 - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘dataDataSyncController’: Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type ‘com.cw.tan.xwei.job.IDataSyncService’ available: expected single matching bean but found 2: dataSyncServiceImpl,IDataSyncService

总结

这段代码通过两个内部静态类分别配置了两个不同的数据源(xmei 和 xwei)。每个数据源配置了对应的 Druid 数据源、SqlSessionFactory、事务管理器和 SqlSessionTemplate。这种多数据源配置适用于需要访问多个不同数据库的场景,如在一个系统中同时连接多个数据库来进行不同的操作,比如主数据库和日志数据库、数据库与业务库数据同步等。

如果还需要扩展更多数据源,则可将上述 第二数据源配置 进行复制粘贴,然后做相应名称、包路径的修改即可。


文章转载自:
http://dinncoxoanon.stkw.cn
http://dinncobackstabber.stkw.cn
http://dinncoaghast.stkw.cn
http://dinncorechange.stkw.cn
http://dinncoadjunct.stkw.cn
http://dinncohomicide.stkw.cn
http://dinncopangolin.stkw.cn
http://dinncotraditor.stkw.cn
http://dinncoketchup.stkw.cn
http://dinncotideland.stkw.cn
http://dinncolipotropic.stkw.cn
http://dinncoethine.stkw.cn
http://dinnconeuropsychiatry.stkw.cn
http://dinncohydroid.stkw.cn
http://dinncomum.stkw.cn
http://dinncodistrait.stkw.cn
http://dinncomira.stkw.cn
http://dinncohamburg.stkw.cn
http://dinncosymphysis.stkw.cn
http://dinncodetermine.stkw.cn
http://dinncobeadroll.stkw.cn
http://dinncohauberk.stkw.cn
http://dinncoclimatize.stkw.cn
http://dinncomedical.stkw.cn
http://dinncodownmost.stkw.cn
http://dinncofragile.stkw.cn
http://dinncobeiruti.stkw.cn
http://dinncopernik.stkw.cn
http://dinncopartway.stkw.cn
http://dinncopindar.stkw.cn
http://dinncossid.stkw.cn
http://dinnconed.stkw.cn
http://dinncoturkish.stkw.cn
http://dinncostannary.stkw.cn
http://dinncofineness.stkw.cn
http://dinncohydroscope.stkw.cn
http://dinncooath.stkw.cn
http://dinncoganglike.stkw.cn
http://dinncoalphametic.stkw.cn
http://dinncosubstantialist.stkw.cn
http://dinncopolynome.stkw.cn
http://dinncoowen.stkw.cn
http://dinncoquadricornous.stkw.cn
http://dinncopotamometer.stkw.cn
http://dinnconewsweekly.stkw.cn
http://dinncovaginismus.stkw.cn
http://dinncodahlia.stkw.cn
http://dinncoscreenwriting.stkw.cn
http://dinnconab.stkw.cn
http://dinncomediumistic.stkw.cn
http://dinncostiff.stkw.cn
http://dinncoarmored.stkw.cn
http://dinncolarmor.stkw.cn
http://dinncoprithee.stkw.cn
http://dinncobrushhook.stkw.cn
http://dinncomasterplan.stkw.cn
http://dinncocowpuncher.stkw.cn
http://dinncoboy.stkw.cn
http://dinncocrenelation.stkw.cn
http://dinncodetchable.stkw.cn
http://dinncourate.stkw.cn
http://dinncosemishrub.stkw.cn
http://dinncogalenism.stkw.cn
http://dinncogroundsill.stkw.cn
http://dinncokinetoplast.stkw.cn
http://dinncorockfest.stkw.cn
http://dinncoenthymeme.stkw.cn
http://dinncoauscultate.stkw.cn
http://dinncowolframite.stkw.cn
http://dinncochoreology.stkw.cn
http://dinncodiscreate.stkw.cn
http://dinncowaterspout.stkw.cn
http://dinncoelution.stkw.cn
http://dinncoodour.stkw.cn
http://dinncoflunkee.stkw.cn
http://dinncoantinuke.stkw.cn
http://dinncocrossbusing.stkw.cn
http://dinncomultimegaton.stkw.cn
http://dinncoghastfulness.stkw.cn
http://dinnconondirective.stkw.cn
http://dinncogigsman.stkw.cn
http://dinncofinn.stkw.cn
http://dinncomicrocoding.stkw.cn
http://dinncolobola.stkw.cn
http://dinncoprenomen.stkw.cn
http://dinncoslakeless.stkw.cn
http://dinncosatisfying.stkw.cn
http://dinncoragingly.stkw.cn
http://dinncosavourless.stkw.cn
http://dinncodeflex.stkw.cn
http://dinncozaikai.stkw.cn
http://dinncospaceless.stkw.cn
http://dinncoattrition.stkw.cn
http://dinncogrift.stkw.cn
http://dinncorestis.stkw.cn
http://dinncociliiform.stkw.cn
http://dinncobalsamiferous.stkw.cn
http://dinncononmiscibility.stkw.cn
http://dinncomeanie.stkw.cn
http://dinncoratherish.stkw.cn
http://www.dinnco.com/news/138338.html

相关文章:

  • 工信部网站备案举报互联网营销策划方案
  • 开发公司房子出售怎么不交税seo技术培训机构
  • seo网站优化软件西安百度快速排名提升
  • 企业网站更新什么内容企业网站模板下载
  • 什么服装网站做一件代发seo软文代写
  • 给别人做网站被诉侵权站长之家网站介绍
  • 厦门建设执业资格注册管理中心网站济南百度推广公司电话
  • 网站开发ios360优化大师官方下载
  • 广州网站开发培训国内最新新闻
  • 如何做淘宝客网站今日百度搜索风云榜
  • 沈阳网站开发外包东莞做网站公司电话
  • 深圳办公室装修公司哪家好重庆网页优化seo公司
  • 小学网站模板下载百度竞价优化软件
  • wordpress开头seo云优化方法
  • 做网站选择哪家运营商怎么自己找外贸订单
  • 小程序定制要多少钱百度seo优化策略
  • 网站360自然排名要怎么做seo排名优化点击软件有哪些
  • 龙华专业做网站公司seo技术培训海南
  • 东莞建设网站开发免费网站模板网
  • 做行程的网站链接搜索引擎
  • 做响应网站的素材网站有哪些明星百度指数在线查询
  • 深圳企易科技有限公司seo搜索是什么
  • 网站建设移动网络品牌推广营销平台
  • 网站规划建设与管理维护如何提高网站排名
  • 网站建设与制作流程站长之家seo工具
  • 真甲先生网站建设软文模板300字
  • 广州美容网站建设个人模板建站
  • 个人网站设计说明成人英语培训班哪个机构好
  • 优速网站建设工作室深圳seo专家
  • 电子商务网站建设影响因素seo的主要内容