日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

SpringBoot中mybatis配置多数据源

發布時間:2025/3/11 javascript 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SpringBoot中mybatis配置多数据源 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

首先需要創建多個數據庫

簡單的user表

CREATE TABLE `user` (`id` int NOT NULL AUTO_INCREMENT,`name` varchar(255) DEFAULT NULL,`age` int DEFAULT NULL,PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;

導入項目依賴

<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jdbc</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.1.4</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency><dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId><version>1.1.10</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies>

application.properties

spring.datasource.one.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.one.username=root spring.datasource.one.password=feng10.10 spring.datasource.one.url=jdbc:mysql://localhost:3306/m_1?characterEncoding=utf8&serverTimezone=UTCspring.datasource.two.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.two.username=root spring.datasource.two.password=feng10.10 spring.datasource.two.url=jdbc:mysql://localhost:3306/m_2?characterEncoding=utf8&serverTimezone=UTC# 多數據源配置時,mybatis這里設置的配置是不生效的!! #mybatis.mapper-locations=classpath*:/mapper1/*.xml, classpath*:/mapper2/*.xml #mybatis.type-aliases-package=com.zlf.mybatisDemo.domain

手動配置數據源

DataSourceConfig.java

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;import javax.sql.DataSource;/*** 配置多數據源*/ @Configuration public class DataSourceConfig {//將配置文件中對應的屬性注入改數據源中@ConfigurationProperties("spring.datasource.one")@BeanDataSource dsOne(){return DruidDataSourceBuilder.create().build();}//將配置文件中對應的屬性注入改數據源中@ConfigurationProperties("spring.datasource.two")@BeanDataSource dsTwo(){return DruidDataSourceBuilder.create().build();} }

手動配置myBatis配置

import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.SqlSessionTemplate; import org.mybatis.spring.annotation.MapperScan; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.support.PathMatchingResourcePatternResolver;import javax.annotation.Resource; import javax.sql.DataSource;/*** 配置myBaits配置 1* 在@MapperScan注解中指定Mapper接口所在的位直, 同時指定SqlSessionFactory的實例名, 則該位置下的 Mapper將使用 SqlSessionFactory 實例。* 提供 SqlSessionFactory 的實例, 直接創建出來, 同時將 DataSource 的實例設置給 SqlSessionFactory,* 這里創建的 SqlSessionFactory 實例也就是@MapperScan 注解中 sqlSessionFactoryRef參數指定的實例 。*/ @Configuration @MapperScan(value = "com.zlf.mybatisDemo.mapper1",sqlSessionFactoryRef = "sqlSessionFactoryBean1") public class MyBatisConfigOne {@Resource(name = "dsOne")DataSource dataSource;/*** 提供 SqlSessionFactory 的實例, 直接創建出來, 同時將 DataSource 的實例設置給 SqlSessionFactory,* 這里創建的 SqlSessionFactory 實例也就是@MapperScan 注解中 sqlSessionFactoryRef參數指定的實例 。* @return* @throws Exception*/@BeanSqlSessionFactory sqlSessionFactoryBean1() throws Exception{SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();factoryBean.setDataSource(dataSource);// 需要手動設置mapper文件的路徑factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:/mapper1/*.xml"));return factoryBean.getObject();}/*** 提供一個 SqlSessionTemplate 實例。 這是一個線程安全類,主要用來管理 MyBatis 中的 SqlSession 操作。* @return* @throws Exception*/@BeanSqlSessionTemplate sqlSessionTemplate1() throws Exception{return new SqlSessionTemplate(sqlSessionFactoryBean1());}} import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.SqlSessionTemplate; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.support.PathMatchingResourcePatternResolver;import javax.annotation.Resource; import javax.sql.DataSource;/*** 配置myBaits配置 2* 在@MapperScan注解中指定Mapper接口所在的位直, 同時指定SqlSessionFactory的實例名, 則該位置下的 Mapper將使用 SqlSessionFactory 實例。* 提供 SqlSessionFactory 的實例, 直接創建出來, 同時將 DataSource 的實例設置給 SqlSessionFactory,* 這里創建的 SqlSessionFactory 實例也就是@MapperScan 注解中 sqlSessionFactoryRef參數指定的實例 。*/ @Configuration @MapperScan(value = "com.zlf.mybatisDemo.mapper2",sqlSessionFactoryRef = "sqlSessionFactoryBean2") public class MyBatisConfigTwo {@Resource(name = "dsTwo")DataSource dataSource;/*** 提供 SqlSessionFactory 的實例, 直接創建出來, 同時將 DataSource 的實例設置給 SqlSessionFactory,* 這里創建的 SqlSessionFactory 實例也就是@MapperScan 注解中 sqlSessionFactoryRef參數指定的實例 。* @return* @throws Exception*/@BeanSqlSessionFactory sqlSessionFactoryBean2() throws Exception{SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();sqlSessionFactoryBean.setDataSource(dataSource);// 需要手動設置mapper文件的路徑sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:/mapper2/*.xml"));return sqlSessionFactoryBean.getObject();}/*** 提供一個 SqlSessionTemplate 實例。 這是一個線程安全類,主要用來管理 MyBatis 中的 SqlSession 操作。* @return* @throws Exception*/@BeanSqlSessionTemplate sqlSessionTemplate2() throws Exception {return new SqlSessionTemplate(sqlSessionFactoryBean2());}}

項目結構

啟動類

import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication//@MapperScan(basePackages = {"com.zlf.mybatisDemo.mapper1","com.zlf.mybatisDemo.mapper2"}) 已經在配置類中設置了 public class MybatisDemoApplication {public static void main(String[] args) {SpringApplication.run(MybatisDemoApplication.class, args);}}

測試結果

測試類

import com.zlf.mybatisDemo.domain.User; import com.zlf.mybatisDemo.mapper1.userMapper1; import com.zlf.mybatisDemo.mapper2.userMapper2; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest;import java.util.List;@SpringBootTest class MybatisDemoApplicationTests {@AutowireduserMapper1 userMapper1;@AutowireduserMapper2 userMapper2;@Testvoid TestMybatis(){List<User> users1 = userMapper1.findAll();List<User> users2 = userMapper2.findAll();System.out.println("第一個數據源:m_1");System.out.println(users1);System.out.println("第二個數據源:m_2");System.out.println(users2);}}

總結

以上是生活随笔為你收集整理的SpringBoot中mybatis配置多数据源的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。