最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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的方法,進(jìn)行掃描注冊,手動注冊接口到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)先配置,那么所有依賴注入默認(rèn)都將注入第一個數(shù)據(jù)源的配置,所以第二個數(shù)據(jù)源配置需要額外指定使用何種bean注入。

datasource的定義需要使用 @Qualifier注解指定值,在依賴注入時使用 @Qualifier和指定值就可以注入目標(biāo)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線程池實現(xiàn)批量下載文件

    java線程池實現(xiàn)批量下載文件

    這篇文章主要為大家詳細(xì)介紹了java線程池實現(xiàn)批量下載文件,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-07-07
  • JPA中JpaRepository接口的使用方式

    JPA中JpaRepository接口的使用方式

    這篇文章主要介紹了JPA中JpaRepository接口的使用方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • Java算法之歸并排序舉例詳解

    Java算法之歸并排序舉例詳解

    這篇文章主要介紹了Java算法之歸并排序的相關(guān)資料,歸并排序是一種遞歸排序算法,通過將數(shù)組分成更小的子數(shù)組,遞歸地排序這些子數(shù)組,然后將它們合并成有序數(shù)組,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2025-04-04
  • Java實現(xiàn)字符串倒序輸出的常用方法小結(jié)

    Java實現(xiàn)字符串倒序輸出的常用方法小結(jié)

    這篇文章主要介紹了Java實現(xiàn)字符串倒序輸出的常用方法,通過三個實例從不同角度實現(xiàn)該功能,有不錯的借鑒價值,需要的朋友可以參考下
    2014-09-09
  • 詳解如何全注解方式構(gòu)建SpringMVC項目

    詳解如何全注解方式構(gòu)建SpringMVC項目

    這篇文章主要介紹了詳解如何全注解方式構(gòu)建SpringMVC項目,利用Eclipse構(gòu)建SpringMVC項目,非常具有實用價值,需要的朋友可以參考下
    2018-10-10
  • SpringBoot權(quán)限認(rèn)證-Sa-Token的使用詳解

    SpringBoot權(quán)限認(rèn)證-Sa-Token的使用詳解

    Sa-Token是一款輕量級Java權(quán)限認(rèn)證框架,它簡化了權(quán)限管理,提高了開發(fā)效率,本文通過實例介紹了Sa-Token的基本概念、與其他框架的比較、基本語法和高級用法,并探討了其核心原理和實際應(yīng)用場景,感興趣的朋友一起看看吧
    2024-09-09
  • Sharding Jdbc批量操作引發(fā)fullGC解決

    Sharding Jdbc批量操作引發(fā)fullGC解決

    這篇文章主要為大家介紹了Sharding Jdbc批量操作引發(fā)fullGC解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-11-11
  • Java 如何使用@Autowired注解自動注入bean

    Java 如何使用@Autowired注解自動注入bean

    這篇文章主要介紹了Java 使用@Autowired注解自動注入bean的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • 學(xué)習(xí)Java的static與final關(guān)鍵字

    學(xué)習(xí)Java的static與final關(guān)鍵字

    本篇文章給大家詳細(xì)分析了Java的static與final關(guān)鍵字知識點以及相關(guān)代碼分享,有需要的讀者跟著學(xué)習(xí)下吧。
    2018-03-03
  • 深入了解MyBatis分頁機制

    深入了解MyBatis分頁機制

    在企業(yè)項目的數(shù)據(jù)庫操作中,分頁查詢是一個常見需求,尤其當(dāng)數(shù)據(jù)量龐大時,MyBatis作為我們Java開發(fā)者的持久層框架,為分頁提供了靈活的支持,本篇文章我們將深入探討MyBatis的分頁機制,使我們在實際開發(fā)項目中運用自如,需要的朋友可以參考下
    2023-12-12

最新評論

黑河市| 赤壁市| 平远县| 泗水县| 迭部县| 富宁县| 兴海县| 永昌县| 沙洋县| 周宁县| 托克逊县| 民勤县| 江川县| 三河市| 沂南县| 兴安县| 德庆县| 临桂县| 兴业县| 治多县| 浪卡子县| 江永县| 河西区| 宝坻区| 若羌县| 丹巴县| 九江县| 兴仁县| 云林县| 望谟县| 成安县| 威信县| 阿克陶县| 澄城县| 湖口县| 大方县| 历史| 乐至县| 太仓市| 澄迈县| 嘉善县|