欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

spring boot下mybatis配置雙數(shù)據(jù)源的實例

 更新時間:2021年09月28日 11:00:23   作者:一擼向北  
這篇文章主要介紹了spring boot下mybatis配置雙數(shù)據(jù)源的實例,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

最近項目上遇到需要雙數(shù)據(jù)源的來實現(xiàn)需求,并且需要基于spring boot,mybatis的方式來實現(xiàn),在此做簡單記錄。

單一數(shù)據(jù)源配置

單一數(shù)據(jù)源配置的話并沒有什么特別的,在spring boot框架下,只需要在配置文件內(nèi)添加對應(yīng)的配置項即可,spring boot會自動初始化需要用到的bean。

配置信息如下。這里使用的是德魯伊的數(shù)據(jù)源配置方式

#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的注解注入即可。

多個數(shù)據(jù)源配置

假如需要新增配置一個數(shù)據(jù)源,那么在spring boot 框架下如何實現(xiàn)呢?在多數(shù)據(jù)源的情況下,數(shù)據(jù)源配置需要添加兩份,數(shù)據(jù)源、mybatis等使用到的bean不能再依賴spring boot替我們完成。

多數(shù)據(jù)源配置文件

配置文件改成如下,第二個數(shù)據(jù)源的配置前綴需要自定義為另外的。

#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以區(qū)分第一個數(shù)據(jù)源
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

多數(shù)據(jù)源配置類

編寫第一個數(shù)據(jù)源使用的配置類,如下所示。

對于Datasource的bean定義,需要使用@ConfigurationProperties(prefix = "spring.datasource")前綴匹配來指定使用第一個數(shù)據(jù)源的配置,同時還需要使用注解@Primary來指定當(dāng)有依賴注入需要注入datasource時,優(yōu)先使用@Primary注解修飾的datasource。

對于SqlSessionFactory定義,我們無法依賴spring boot做自動化配置實現(xiàn),有一些動作需要我們手動處理。首先是mapper.xml文件路徑的指定,這樣mapper接口才能注冊到mybatis容器中;假如你定義的的mapper接口沒有對應(yīng)的MapperXml,你還需要手動指定mapper接口的包路徑作為參數(shù),調(diào)用addMappers的方法,進行掃描注冊,手動注冊接口到mybatis容器中,一般這個過程在解析MapperXml文件時會由mybatis框架實現(xiàn)。

還有就是SqlSessionTemplate,DataSourceTransactionManager的定義,第一個數(shù)據(jù)源都需要配置為優(yōu)先注入。

上面所有的配置第一個數(shù)據(jù)源相關(guān)bean優(yōu)先注入都是為了方便spring容器,管理第一個數(shù)據(jù)源的mapper接口的代理類實例bean。spring boot實現(xiàn)Mapper代理類實例的注冊時,是從容器中獲取一個SqlSessionTemplatebean,然后調(diào)用SqlSessionTemplate.getMapper()方法獲取一個實例的,因此SqlSessionTemplate優(yōu)先注入者,spring容器管理的Mapper代理類就是對應(yīng)數(shù)據(jù)源定義的。所以第一個數(shù)據(jù)源的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);
        //設(shè)置mapper.xml文件路徑
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:META-INF/mybatis/mapper/*Mapper.xml"));
        //設(shè)置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);
    }
    
}

下面是第二個數(shù)據(jù)源的配置類。針對第二個數(shù)據(jù)源配置,方法內(nèi)容基本一致,但是需要注意的是,由于第一個數(shù)據(jù)源設(shè)置了優(yōu)先配置,那么所有依賴注入默認都將注入第一個數(shù)據(jù)源的配置,所以第二個數(shù)據(jù)源配置需要額外指定使用何種bean注入。

datasource的定義需要使用 @Qualifier注解指定值,在依賴注入時使用 @Qualifier和指定值就可以注入目標bean。wmsSqlSessionFactory方法 使用@Qualifier(“wmsDatasource”)注解可以注入第二個數(shù)據(jù)源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();
        //設(shè)置wms數(shù)據(jù)源額外的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);
    }
}

通過上面的配置就可以實現(xiàn)雙數(shù)據(jù)源配置,下面是使用方式。

  • 第一個數(shù)據(jù)源定義的maper由spring容器管理,可以直接使用@Resource注解使用
  • 第二個數(shù)據(jù)源使用可能比較麻煩,代碼如下
//依賴注入第二個數(shù)據(jù)源的SqlSession
@Resource
@Qualifier("wmsSqlSessionTemplate")
SqlSessionTemplate wmsSqlSessionTemplate;
//使用時手動獲取Mapper然后調(diào)用接口方法
wmsSqlSessionTemplate.getMapper(ReturnResultRecoderMapper.class).selectTest()

最后需要注意一點就是,使用上面的配置方式,mybatis的結(jié)果處理器對下劃線結(jié)果集合并沒有自動轉(zhuǎn)換為駝峰方式,需要手動在sql中定義別名。

例如實體

class People{
    private String peopleGender;
}

mybatis執(zhí)行sql,結(jié)果集映射為People類。

sekect people_gender from people;

people_gender這個結(jié)果無法自動映射到peopleGender,需要執(zhí)行以下sql才能映射上

sekect people_gender as peopleGendler from people;

所以這個配置過程應(yīng)該是漏了某些配置導(dǎo)致結(jié)果處理器的名稱映射不起作用,這個問題先mark。

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java JDBC自定義封裝工具類的步驟和完整代碼

    Java JDBC自定義封裝工具類的步驟和完整代碼

    這篇文章主要給大家介紹了關(guān)于Java JDBC自定義封裝工具類的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-02-02
  • SpringBoot中的PropertySource原理詳解

    SpringBoot中的PropertySource原理詳解

    這篇文章主要介紹了SpringBoot中的PropertySource原理詳解,PropertySource?是一個非常重要的概念,它允許您在應(yīng)用程序中定義屬性,并將這些屬性注入到?Spring?環(huán)境中,需要的朋友可以參考下
    2023-07-07
  • Java之MultipartFile和File類型互轉(zhuǎn)方式

    Java之MultipartFile和File類型互轉(zhuǎn)方式

    這篇文章主要介紹了Java之MultipartFile和File類型互轉(zhuǎn)方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • springboot過濾器執(zhí)行兩次的解決及跨域過濾器問題

    springboot過濾器執(zhí)行兩次的解決及跨域過濾器問題

    這篇文章主要介紹了springboot過濾器執(zhí)行兩次的解決及跨域過濾器問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • 華為技術(shù)專家講解JVM內(nèi)存模型(收藏)

    華為技術(shù)專家講解JVM內(nèi)存模型(收藏)

    這篇文章主要介紹了華為技術(shù)專家講解JVM內(nèi)存模型(收藏)的相關(guān)知識,本文給大家介紹的非常詳細,具有一定的收藏借鑒價值,需要的朋友可以參考下
    2021-05-05
  • java中最大的整數(shù)用法分析

    java中最大的整數(shù)用法分析

    這篇文章主要介紹了java中最大的整數(shù)用法,結(jié)合具體實例形式分析了java計算類java.math.BigInteger具體使用技巧,需要的朋友可以參考下
    2017-06-06
  • Java實現(xiàn)表達式二叉樹

    Java實現(xiàn)表達式二叉樹

    這篇文章主要為大家詳細介紹了如何利用Java實現(xiàn)表達式二叉樹,感興趣的小伙伴們可以參考一下
    2016-08-08
  • idea如何配置springboot熱部署

    idea如何配置springboot熱部署

    這篇文章主要介紹了idea如何配置springboot熱部署問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • jax-ws handler 的詳解及簡單實例

    jax-ws handler 的詳解及簡單實例

    這篇文章主要介紹了 jax-ws handler 的詳解及簡單實例的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下
    2017-09-09
  • java?http請求獲取圖片并返回文件流給前端的方法步驟

    java?http請求獲取圖片并返回文件流給前端的方法步驟

    作為一名Java后端開發(fā)者,掌握如何從后端返回文件流至前端是基本技能之一,這篇文章主要介紹了java?http請求獲取圖片并返回文件流給前端的方法步驟,需要的朋友可以參考下
    2024-09-09

最新評論