zoukankan      html  css  js  c++  java
  • springboot 多数据源配置实现及分析

    今天遇到一个任务,需要把oracle上的数据迁移到mysql中,表的字段及结构并不完全相同,还需要写一些代码去对应。

    这种小工程适合用springboot快速搭建,用mybatis+spring去实现。

    但是我没怎么用过springboot,只能去网上找现成的代码,再根据具体的业务改一改:

    SpringBoot2.0之八 多数据源配置

    如果只会复制粘贴,那我永远都只能是一个麻瓜,所以必须要把这一段代码全部学会。

     1 @Configuration
     2 @MapperScan(basePackages = "com.somta.springboot.dao.master", sqlSessionTemplateRef  = "masterSqlSessionTemplate")
     3 public class MasterDataSourceConfiguration {
     4 
     5     @Value("${spring.datasource.master.driver-class-name}")
     6     private String driverClassName;
     7 
     8     @Value("${spring.datasource.master.url}")
     9     private String url;
    10 
    11     @Value("${spring.datasource.master.username}")
    12     private String username;
    13 
    14     @Value("${spring.datasource.master.password}")
    15     private String password;
    16 
    17     @Bean(name = "masterDataSource")
    18     @Primary
    19     public DataSource dataSource() {
    20         DruidDataSource dataSource = new DruidDataSource();
    21         dataSource.setDriverClassName(this.driverClassName);
    22         dataSource.setUrl(this.url);
    23         dataSource.setUsername(this.username);
    24         dataSource.setPassword(this.password);
    25         return dataSource;
    26     }
    27 
    28     @Bean(name = "masterSqlSessionFactory")
    29     @Primary
    30     public SqlSessionFactory sqlSessionFactory(@Qualifier("masterDataSource") DataSource dataSource) throws Exception {
    31         SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
    32         bean.setDataSource(dataSource);
    33         bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mybatis/master/**/Mysql_*Mapper.xml"));
    34         return bean.getObject();
    35     }
    36 
    37     @Bean(name = "masterTransactionManager")
    38     @Primary
    39     public DataSourceTransactionManager transactionManager(@Qualifier("masterDataSource") DataSource dataSource) {
    40         return new DataSourceTransactionManager(dataSource);
    41     }
    42 
    43     @Bean(name = "masterSqlSessionTemplate")
    44     @Primary
    45     public SqlSessionTemplate sqlSessionTemplate(@Qualifier("masterSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
    46         return new SqlSessionTemplate(sqlSessionFactory);
    47     }
    48 
    49 }

    这是整个配置中的核心就是这一段代码了,我们来仔细研究一下:

    @Configuration注解

    自spring3.0以后,用于定义配置类,可以替代xml文件。被注解的类中包含一个或多个@bean注解的方法,这些方法将会被AnnotationConfigApplicationContext或AnnotationConfigWebApplicationContext类进行扫描,并用于构建bean定义,初始化Spring容器。

    简单地说,就是把spring中的配置文件

    <beans>
        <bean>
        </bean>
        ....
    </beans>

    这种配置用注解的形式替换掉

    那么依次看代码中被@bean注解的方法:

     1 @Bean(name = "masterDataSource")
     2 @Primary
     3 public DataSource dataSource() {
     4       DruidDataSource dataSource = new DruidDataSource();
     5       dataSource.setDriverClassName(this.driverClassName);
     6       dataSource.setUrl(this.url);
     7       dataSource.setUsername(this.username);
     8       dataSource.setPassword(this.password);
     9       return dataSource;
    10 }

    这就相当于向spring容器中注册了一个id为"masterDataSource"的bean,同时在代码中设定好参数,就配置好数据源。

    1 @Bean(name = "masterSqlSessionFactory")
    2 @Primary
    3 public SqlSessionFactory sqlSessionFactory(@Qualifier("masterDataSource") DataSource dataSource) throws Exception {
    4         SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
    5         bean.setDataSource(dataSource);
    6         bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mybatis/master/**/Mysql_*Mapper.xml"));
    7         return bean.getObject();
    8     }

    向ico容器注册一个id为"masterDataSource"的bean,SqlSessionFactory是myBatis的核心类,用于生成sqlSession

    MyBatis常用对象SqlSessionFactory和SqlSession介绍和运用

    1 @Bean(name = "masterTransactionManager")
    2     @Primary
    3     public DataSourceTransactionManager transactionManager(@Qualifier("masterDataSource") DataSource dataSource) {
    4         return new DataSourceTransactionManager(dataSource);
    5     }

    注册一个id为"masterTranscationManager"的bean,DataSourceTransactionManager是spring事务管理的核心类

    Spring3.1.0实现原理分析(二十二).Dao事务分析之事务管理器DataSourceTransactionManager

    1 @Bean(name = "masterSqlSessionTemplate")
    2     @Primary
    3     public SqlSessionTemplate sqlSessionTemplate(@Qualifier("masterSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
    4         return new SqlSessionTemplate(sqlSessionFactory);
    5     }

    注册一个id为"masterSqlSessionTemplate"的bean,SqlSessionTemplate是spring事务管理的核心类

    https://www.cnblogs.com/daxin/p/3544188.html

    还有个需要注意的地方时,以上bean在引入其他依赖bean时,使用的时注解@Qualifier("beanName")。这样做是因为要配置两个数据源,DataSource、SqlSessionFaction、DataSourceTransactionManager、SqlSessionTemplate还会在另一个数据源中配置,需要通过名称来区分。

    上述代码加注解的作用相当于以下xml配置文件:

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
     4     xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
     5     xsi:schemaLocation="http://www.springframework.org/schema/beans 
     6       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     7       http://www.springframework.org/schema/aop 
     8       http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
     9       http://www.springframework.org/schema/context 
    10       http://www.springframework.org/schema/context/spring-context-3.0.xsd
    11       http://www.springframework.org/schema/tx 
    12       http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    13  ">
    14  
    15     <bean id="dataSource"
    16         class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    17         <property name="driverClassName">
    18             <value>org.gjt.mm.mysql.Driver</value>
    19         </property>
    20         <property name="url">
    21             <value>jdbc:mysql://localhost:3306/zdy?useUnicode=true&characterEncoding=UTF-8
    22             </value>
    23         </property>
    24         <property name="username">
    25             <value>root</value>
    26         </property>
    27         <property name="password">
    28             <value>1111</value>
    29         </property>
    30     </bean>
    31     
    32         <!-- 获取会话工厂,并注入Mybatis,和dateSource数据库链接 -->
    33     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    34         <property name="dataSource" ref="dataSource"></property>
    35         <property name="configLocation" value="bs/Mybatis.xml"></property>
    36     </bean>
    37     
    38         <!-- 在会话工厂中取出SqlSessionTemplate这个对象 -->
    39      <bean id="sqlsessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
    40         <constructor-arg index="0" ref="sqlSessionFactory" />
    41     </bean>
    42         
    43         <!-- 把sqlsessionTemplate注入到UserMapper中去。UserMapper才能对数据进行操作 -->
    44     <bean id="user" class="bs.UserImpl">
    45     <property name="sqlsession" ref="sqlsessionTemplate">
    46     </property>
    47     </bean>

    还有一个注解@MapperScan,用于注解扫描到的对应的mapper,然后在使用mapper下的bean的数据库操作就会使用上述配置的数据源及相关bena。

    没怎么用过springboot,不太熟悉springboot的文件结构,这个mapper好像类似传统ssm框架下的dao层文件,等我再学习学习。

    写了半天才发现这个玩意这些注解配置和之前的ssm框架原理是一样的,只是通过注解简化了大量操作,而且搭建起来也更加便捷。

  • 相关阅读:
    1012 The Best Rank (25 分)(排序)
    1011. World Cup Betting (20)(查找元素)
    1009 Product of Polynomials (25 分)(模拟)
    1008 Elevator (20 分)(数学问题)
    1006 Sign In and Sign Out (25 分)(查找元素)
    1005 Spell It Right (20 分)(字符串处理)
    Kafka Connect 出现ERROR Failed to flush WorkerSourceTask{id=local-file-source-0}, timed out while wait
    flume、kafka、avro组成的消息系统
    Java23种设计模式总结【转载】
    Java编程 思维导图
  • 原文地址:https://www.cnblogs.com/cxy2016/p/9973502.html
Copyright © 2011-2022 走看看