zl程序教程

您现在的位置是:首页 >  .Net

当前栏目

SpringBoot-技术专区-整合mybatis 如何使用多数据源

2023-02-18 16:23:40 时间

 整合

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.3.1</version>
</dependency>

单库配置:            

   引入之后,默认情况下,Spring Boot会自动为我们配置好一个DataSource,它会在classpath中搜索H2、hsqldb等内存数据库的jar包,如果找到了,就会自动配置一个内存数据库的DataSource。

  如果在application.yml或application.propertie中指定了spring.datasource.*的相关配置参数,Spring Boot就会使用该配置创建一个DataSource。

  然后会自动创建使用该DataSource的SqlSessionFactoryBean以及SqlSessionTemplate。会自动扫描你的Mappers,连接到SqlSessionTemplate,并注册到Spring上下文中。

spring.datasource.url=jdbc:mysql://localhost/test
spring.datasource.username=dbuser
spring.datasource.password=dbpass
spring.datasource.driver-class-name=com.mysql.jdbc.Drive

多库配置:            

    由于业务需要,项目要同时使用多个数据库进行业务开发:

  首先,我们必须在application.properties中自定义两个数据源的配置,一个使用first.datasource.*,另一个使用second.datasource.*,为了能使别人一眼看出连接的是什么库,可以使用数据库命名,比如user库,则可以使用user.datasource.*,在使用多数据源的时候,所有必要配置都不能省略。

first.datasource.url=jdbc:mysql://localhost/first
first.datasource.username=dbuser1
first.datasource.password=dbpass1
first.datasource.driver-class-name=com.mysql.jdbc.Driver
first.datasource.type=com.alibaba.druid.pool.DruidDataSource//我用的是Druid,也可以不加用默认的

second.datasource.url=jdbc:mysql://localhost/second
second.datasource.username=dbuser2
second.datasource.password=dbpass2
second.datasource.driver-class-name=com.mysql.jdbc.Driver
second.datasource.type=com.alibaba.druid.pool.DruidDataSource
 

直接上代码,我的做法是将两个数据源用两个配置类创建:

@Configuration
@MapperScan(basePackages = {"com.user.server.dao"}, 
  sqlSessionTemplateRef = "userSqlSessionTemplate") public class UserMybatisConfig {
   @Bean(name = "userDataSource") @Primary //必须加此注解,不然报错,下一个类则不需要添加 @ConfigurationProperties(prefix = "first.datasource") // prefix值必须是application.properteis中对应属性的前缀 public DataSource userDataSource() { return DataSourceBuilder.create().build(); }
   @Bean public SqlSessionFactory userSqlSessionFactory(@Qualifier("userDataSource")
    DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); //添加XML目录 ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); try { bean.setMapperLocations(resolver.getResources("classpath*:com/user/server/dao/mapping/*.xml")); return bean.getObject(); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } } @Bean public SqlSessionTemplate userSqlSessionTemplate(@Qualifier("userSqlSessionFactory")
      SqlSessionFactory sqlSessionFactory) throws Exception { SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactory); // 使用上面配置的Factory return template; } } @Configuration @MapperScan(basePackages = {"com.airmi.server.dao"}, sqlSessionTemplateRef = "autoTestSqlSessionTemplate") public class AutoTestMybatisConfig { @Bean @ConfigurationProperties(prefix = "autotest.datasource") public DataSource autoTestDataSource() { return DataSourceBuilder.create().build(); }
  @Bean public SqlSessionTemplate autoTestSqlSessionTemplate(@Qualifier("autoTestSqlSessionFactory")
     SqlSessionFactory sqlSessionFactory) throws Exception { SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactory); return template; } @Bean public SqlSessionFactory autoTestSqlSessionFactory(@Qualifier("autoTestDataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); //添加XML目录 ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); try { bean.setMapperLocations(resolver.getResources("classpath*:com/airmi/server/dao/mapping/*.xml")); return bean.getObject(); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } } }

  @Primary //该注解表示在同一个接口有多个实现类可以注入的时候,默认选择哪一个,而不是让autowire注解报错,官网要求当多个数据源时,必须指定一个datasource,另一个datasource则不用添加。

  @Qualifier 根据名称进行注入,通常是在具有相同的多个类型的实例的一个注入(例如有多个DataSource类型的实例)。

  @MapperScan(basePackages = {"com.user.server.dao"}, sqlSessionTemplateRef = "userSqlSessionTemplate") basePackages为mapper所在的包,sqlSessionTemplateRef要引用的实例。

user代码结构如下:

 

相关阅读:

如何通过技术手段 “干掉” 视频APP里讨厌的广告?

通过技术手段 “干掉” 视频APP里讨厌的广告之(腾讯视频)

抓包神器之Charles,常用功能都在这里了

 

推荐阅读:

2018,如何从小白升级到大牛程序员呢?

htt2.0的时代真的来了

如何站在巨人的肩膀上,将自己的产品赋予AI的能力?百度UNIT

 

学习分享:

深度机器学习56G视频资源分享
————————————————
版权声明:本文为CSDN博主「互扯程序」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/mxw2552261/article/details/78640062