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)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
- springboot整合mybatis的超詳細過程(配置模式+注解模式)
- 解析整合mybatis-spring需要的maven依賴配置問題
- springboot mybatis druid配置多數(shù)據(jù)源教程
- springboot配置多數(shù)據(jù)源后mybatis攔截器失效的解決
- SpringBoot配置MyBatis-Plus實現(xiàn)增刪查改
- 使用Spring掃描Mybatis的mapper接口的三種配置
- mybatis spring配置SqlSessionTemplate的使用方式
- SpringBoot集成Mybatis+xml格式的sql配置文件操作
- Spring整合Mybatis具體代碼實現(xiàn)流程
相關(guān)文章
SpringBoot中的PropertySource原理詳解
這篇文章主要介紹了SpringBoot中的PropertySource原理詳解,PropertySource?是一個非常重要的概念,它允許您在應(yīng)用程序中定義屬性,并將這些屬性注入到?Spring?環(huán)境中,需要的朋友可以參考下2023-07-07Java之MultipartFile和File類型互轉(zhuǎn)方式
這篇文章主要介紹了Java之MultipartFile和File類型互轉(zhuǎn)方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09springboot過濾器執(zhí)行兩次的解決及跨域過濾器問題
這篇文章主要介紹了springboot過濾器執(zhí)行兩次的解決及跨域過濾器問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12