首页 > 运营 > 帮助中心 > 正文

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

2020-07-28 14:28:50
字体:
来源:转载
供稿:网友

前言

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

整合

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

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

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

 

单库配置:

引入之后,默认情况下,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/firstfirst.datasource.username=dbuser1first.datasource.password=dbpass1first.datasource.driver-class-name=com.mysql.jdbc.Driverfirst.datasource.type=com.alibaba.druid.pool.DruidDataSource//我用的是Druid,也可以不加用默认的second.datasource.url=jdbc:mysql://localhost/secondsecond.datasource.username=dbuser2second.datasource.password=dbpass2second.datasource.driver-class-name=com.mysql.jdbc.Driversecond.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);   }  } }
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表