温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Spring Boot 整合mybatis 使用多数据源的实现方法

发布时间:2020-08-21 19:53:48 来源:脚本之家 阅读:220 作者:互扯程序 栏目:编程语言

前言

本篇教程偏向实战,程序猿直接copy代码加入到自己的项目中做简单的修修改改便可使用,而对于springboot以及mybatis不在此进行展开介绍,如有读者希望了解可以给我留言,并持续关注,我后续会慢慢更新。(黑色区域代码部分,安卓手机可手动向左滑动,来查看全部代码)

整合

其实整合很简单,如果是用gradle的话,在build.gradle文件里加入

compile('org.mybatis.spring.boot:mybatis-spring-boot-starter:1.3.1')

如果是用maven的话在pom.xml文件里加入

Spring Boot 整合mybatis 使用多数据源的实现方法 

单库配置:

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

如果在application.yml或application.property中指定了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.Driver

更多参数请查看DataSourceProperties

多库配置:

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

首先,我们必须在application.property中自定义两个数据源的配置,一个使用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代码结构如下:

Spring Boot 整合mybatis 使用多数据源的实现方法 

总结

以上所述是小编给大家介绍的Spring Boot 整合mybatis 使用多数据源的实现方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对亿速云网站的支持!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI