mybatis配置双数据源 spring boot下mybatis配置双数据源的实例

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

mybatis配置双数据源 spring boot下mybatis配置双数据源的实例

一撸向北   2021-09-28 我要评论
想了解spring boot下mybatis配置双数据源的实例的相关内容吗,一撸向北在本文为您仔细讲解mybatis配置双数据源的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:mybatis配置,配置双数据源,springboot配置双数据源,下面大家一起来学习吧。

最近项目上遇到需要双数据源的来实现需求,并且需要基于spring boot,mybatis的方式来实现,在此做简单记录。

单一数据源配置

单一数据源配置的话并没有什么特别的,在spring boot框架下,只需要在配置文件内添加对应的配置项即可,spring boot会自动初始化需要用到的bean。

配置信息如下。这里使用的是德鲁伊的数据源配置方式

#datasource配置
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://xxx
spring.datasource.username=root
spring.datasource.password=123456
#mybatis配置
#mybatis  xmlMapper文件路径
mybatis.mapper-locations=classpath:META-INF/mybatis/mapper/*Mapper.xml
mybatis.configuration.map-underscore-to-camel-case=true
#mappers mapper接口文件路径 多个接口时逗号隔开
mapper.mappers=com.xxxx.xxxx
mapper.not-empty=false
mapper.identity=MYSQL

在使用mapper的时候,直接使用spring的注解注入即可。

多个数据源配置

假如需要新增配置一个数据源,那么在spring boot 框架下如何实现呢?在多数据源的情况下,数据源配置需要添加两份,数据源、mybatis等使用到的bean不能再依赖spring boot替我们完成。

多数据源配置文件

配置文件改成如下,第二个数据源的配置前缀需要自定义为另外的。

#datasource 1配置
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://xxx
spring.datasource.username=root
spring.datasource.password=123456
#datasource 2配置,前缀改为second以区分第一个数据源
second.datasource.type=com.alibaba.druid.pool.DruidDataSource
second.datasource.driver-class-name=com.mysql.jdbc.Driver
second.datasource.url=jdbc:mysql://xxx
second.datasource.username=root
second.datasource.password=123456

多数据源配置类

编写第一个数据源使用的配置类,如下所示。

对于Datasource的bean定义,需要使用@ConfigurationProperties(prefix = "spring.datasource")前缀匹配来指定使用第一个数据源的配置,同时还需要使用注解@Primary来指定当有依赖注入需要注入datasource时,优先使用@Primary注解修饰的datasource。

对于SqlSessionFactory定义,我们无法依赖spring boot做自动化配置实现,有一些动作需要我们手动处理。首先是mapper.xml文件路径的指定,这样mapper接口才能注册到mybatis容器中;假如你定义的的mapper接口没有对应的MapperXml,你还需要手动指定mapper接口的包路径作为参数,调用addMappers的方法,进行扫描注册,手动注册接口到mybatis容器中,一般这个过程在解析MapperXml文件时会由mybatis框架实现。

还有就是SqlSessionTemplate,DataSourceTransactionManager的定义,第一个数据源都需要配置为优先注入。

上面所有的配置第一个数据源相关bean优先注入都是为了方便spring容器,管理第一个数据源的mapper接口的代理类实例bean。spring boot实现Mapper代理类实例的注册时,是从容器中获取一个SqlSessionTemplatebean,然后调用SqlSessionTemplate.getMapper()方法获取一个实例的,因此SqlSessionTemplate优先注入者,spring容器管理的Mapper代理类就是对应数据源定义的。所以第一个数据源的Mapper使用时,可以直接使用@Resource注解或者别的依赖注解来使用。

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
/**
 * @author garine
 * @date 2018年11月16日
 **/
@Configuration
public class OdsMybatisConfig {
    @Bean
    @ConfigurationProperties(prefix = "spring.datasource")
    @Primary
    public DataSource odsDataSource(){
        return DataSourceBuilder.create().build();
    }
    @Bean
    @Primary
    public SqlSessionFactory odsSqlSessionFactory(DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        //设置mapper.xml文件路径
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:META-INF/mybatis/mapper/*Mapper.xml"));
        //设置mapper接口的扫描包路径
        //sqlSessionFactory.getConfiguration().addMappers("com.xxx.mapper");
        return bean.getObject();
    }
    @Bean
    @Primary
    public DataSourceTransactionManager odsTransactionManager(DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }
    @Bean
    @Primary
    public SqlSessionTemplate odsSqlSessionTemplate(SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
    
}

下面是第二个数据源的配置类。针对第二个数据源配置,方法内容基本一致,但是需要注意的是,由于第一个数据源设置了优先配置,那么所有依赖注入默认都将注入第一个数据源的配置,所以第二个数据源配置需要额外指定使用何种bean注入。

datasource的定义需要使用 @Qualifier注解指定值,在依赖注入时使用 @Qualifier和指定值就可以注入目标bean。wmsSqlSessionFactory方法 使用@Qualifier(“wmsDatasource”)注解可以注入第二个数据源bean。

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
/**
 * @author garine
 * @date 2018年11月16日
 **/
@Configuration
public class WmsMybatisConfig {
    @Bean
    @ConfigurationProperties(prefix = "second.datasource")
    @Qualifier("wmsDatasource")
    public DataSource wmsDataSource(){
        return DataSourceBuilder.create().build();
    }
    @Bean
    @Qualifier("wmsSqlSessionFactory")
    public SqlSessionFactory wmsSqlSessionFactory(@Qualifier("wmsDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:META-INF/mybatis/wms/mapper/*Mapper.xml"));
        bean.getObject();
        SqlSessionFactory sqlSessionFactory = bean.getObject();
        //设置wms数据源额外的mapper.java注册
        //sqlSessionFactory.getConfiguration().addMappers("com.xx.maper");
        return sqlSessionFactory;
    }
    @Bean
    public DataSourceTransactionManager wmsTransactionManager(@Qualifier("wmsDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }
    @Bean
    @Qualifier("wmsSqlSessionTemplate")
    public SqlSessionTemplate wmsSqlSessionTemplate( @Qualifier("wmsSqlSessionFactory") SqlSessionFactory wmsSqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(wmsSqlSessionFactory);
    }
}

通过上面的配置就可以实现双数据源配置,下面是使用方式。

  • 第一个数据源定义的maper由spring容器管理,可以直接使用@Resource注解使用
  • 第二个数据源使用可能比较麻烦,代码如下
//依赖注入第二个数据源的SqlSession
@Resource
@Qualifier("wmsSqlSessionTemplate")
SqlSessionTemplate wmsSqlSessionTemplate;
//使用时手动获取Mapper然后调用接口方法
wmsSqlSessionTemplate.getMapper(ReturnResultRecoderMapper.class).selectTest()

最后需要注意一点就是,使用上面的配置方式,mybatis的结果处理器对下划线结果集合并没有自动转换为驼峰方式,需要手动在sql中定义别名。

例如实体

class People{
    private String peopleGender;
}

mybatis执行sql,结果集映射为People类。

sekect people_gender from people;

people_gender这个结果无法自动映射到peopleGender,需要执行以下sql才能映射上

sekect people_gender as peopleGendler from people;

所以这个配置过程应该是漏了某些配置导致结果处理器的名称映射不起作用,这个问题先mark。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

猜您喜欢

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们