springboot2多数据源配置,springboot2.0实现多数据源

  springboot2多数据源配置,springboot2.0实现多数据源

  

目录

1.引入冲突包2.性能配置3.分别配置两个数据源4.大刀目录5.实体6。制图人文件7.控制器测试8.结果验证

 

  

1. 引入jar包

pom.xml文件

 

  ?可扩展标记语言版本=1.0 编码=UTF八号?项目xmlns= http://maven。阿帕奇。org/POM/4。0 .0 xmlns : xsi= http://www。w3。org/2001/XML schema-instance xsi :架构位置= http://maven。阿帕奇。org/POM/4。0 .0 https://maven.apache.org/xsd/maven-4.0.0.xsd模型版本4 .0 .0/模型版本父groupIdorg.springframework.boot/groupId artifactId spring-boot-starter-parent/artifactId版本2.5-从存储库查找父级-/父级groupIdcom.multi.datasource/groupId artifact id演示/artifactId 0。0 .数字一(one)版-快照/版本名称演示/名称描述Spring Boot的演示项目/描述属性Java。版本1.8/Java。版本/属性依赖项groupIdorg.springframework.boot/groupId artifactId弹簧靴启动器/artifactId/依赖项groupIdorg.springframework.boot/groupId artifact id spring-boot-starter-web/artifact id/依赖项groupIdorg.mybatis.spring.boot/groupId artifactId my batis-spring-boot-starter/artifactId 1。3 .注射毒品版/版本/依赖项依赖项groupIdcom.alibaba/groupId人工制品德鲁伊/神器

  ;/groupId> <artifactId>lombok</artifactId> <version>1.16.22</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> </configuration> </plugin> </plugins> </build></project>

 

  

2. properties配置

分别准备两个数据源

 

  

server.port=18888mybatis.mapper-locations=classpath:mapper/*.xmlmy1.datasource.url=jdbc:mysql://10.0.0.125:3306/wyl?autoReconnect=truemy1.datasource.driverClassName=com.mysql.cj.jdbc.Drivermy1.datasource.username=rootmy1.datasource.password=123456my2.datasource.url=jdbc:mysql://10.0.0.160:3306/wyl?autoReconnect=truemy2.datasource.driverClassName=com.mysql.cj.jdbc.Drivermy2.datasource.username=rootmy2.datasource.password=123456

 

  

3. 分别配置两个数据源

第一个数据源

 

  

package com.multi.datasource.config;import com.alibaba.druid.pool.DruidDataSource;import org.apache.ibatis.session.SqlSessionFactory;import org.mybatis.spring.SqlSessionFactoryBean;import org.mybatis.spring.annotation.MapperScan;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.beans.factory.annotation.Value;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.core.io.support.PathMatchingResourcePatternResolver;import org.springframework.jdbc.datasource.DataSourceTransactionManager;import javax.sql.DataSource;@Configuration@MapperScan(basePackages = My1DataSourceConfig.PACKAGE, sqlSessionFactoryRef = "my1SqlSessionFactory")public class My1DataSourceConfig { static final String PACKAGE = "com.multi.datasource.dao.my1"; static final String MAPPER_LOCATION = "classpath:mapper/*.xml"; @Value("${my1.datasource.url}") private String url; @Value("${my1.datasource.username}") private String user; @Value("${my1.datasource.password}") private String password; @Value("${my1.datasource.driverClassName}") private String driverClass; @Bean(name = "my1DataSource") public DataSource my1DataSource() { DruidDataSource dataSource = new DruidDataSource(); dataSource.setDriverClassName(driverClass); dataSource.setUrl(url); dataSource.setUsername(user); dataSource.setPassword(password); dataSource.setMaxWait(Integer.MAX_VALUE); dataSource.setTestOnBorrow(true); dataSource.setTestOnReturn(true); dataSource.setTestWhileIdle(true); return dataSource; } @Bean(name = "my1TransactionManager") public DataSourceTransactionManager my1TransactionManager() { return new DataSourceTransactionManager(my1DataSource()); } @Bean(name = "my1SqlSessionFactory") public SqlSessionFactory my1SqlSessionFactory(@Qualifier("my1DataSource") DataSource my1DataSource) throws Exception { final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); sessionFactory.setDataSource(my1DataSource); sessionFactory.setMapperLocations( new PathMatchingResourcePatternResolver().getResources(My1DataSourceConfig.MAPPER_LOCATION)); return sessionFactory.getObject(); }}

第二个数据源

 

  

package com.multi.datasource.config;import com.alibaba.druid.pool.DruidDataSource;import org.apache.ibatis.session.SqlSessionFactory;import org.mybatis.spring.SqlSessionFactoryBean;import org.mybatis.spring.annotation.MapperScan;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.beans.factory.annotation.Value;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.core.io.support.PathMatchingResourcePatternResolver;import org.springframework.jdbc.datasource.DataSourceTransactionManager;import javax.sql.DataSource;@Configuration@MapperScan(basePackages = My1DataSourceConfig.PACKAGE, sqlSessionFactoryRef = "my1SqlSessionFactory")public class My1DataSourceConfig { static final String PACKAGE = "com.multi.datasource.dao.my1"; static final String MAPPER_LOCATION = "classpath:mapper/*.xml"; @Value("${my1.datasource.url}") private String url; @Value("${my1.datasource.username}") private String user; @Value("${my1.datasource.password}") private String password; @Value("${my1.datasource.driverClassName}") private String driverClass; @Bean(name = "my1DataSource") public DataSource my1DataSource() { DruidDataSource dataSource = new DruidDataSource(); dataSource.setDriverClassName(driverClass); dataSource.setUrl(url); dataSource.setUsername(user); dataSource.setPassword(password); dataSource.setMaxWait(Integer.MAX_VALUE); dataSource.setTestOnBorrow(true); dataSource.setTestOnReturn(true); dataSource.setTestWhileIdle(true); return dataSource; } @Bean(name = "my1TransactionManager") public DataSourceTransactionManager my1TransactionManager() { return new DataSourceTransactionManager(my1DataSource()); } @Bean(name = "my1SqlSessionFactory") public SqlSessionFactory my1SqlSessionFactory(@Qualifier("my1DataSource") DataSource my1DataSource) throws Exception { final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); sessionFactory.setDataSource(my1DataSource); sessionFactory.setMapperLocations( new PathMatchingResourcePatternResolver().getResources(My1DataSourceConfig.MAPPER_LOCATION)); return sessionFactory.getObject(); }}

 

  

4. Dao目录

为了区分两个数据源,分别设置了不同的目录

 

  

package com.multi.datasource.dao.my1;import com.multi.datasource.entity.UserEntity;import org.apache.ibatis.annotations.Mapper;@Mapperpublic interface Test1Mapper { UserEntity query(); }
package com.multi.datasource.dao.my2;import com.multi.datasource.entity.UserEntity;import org.apache.ibatis.annotations.Mapper;@Mapperpublic interface Test2Mapper { UserEntity query(); }

 

  

5. Entity

package com.multi.datasource.entity;import lombok.Data;@Datapublic class UserEntity { private String userName;}

 

  

6. Mapper文件

从my1数据源查询

 

  

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.multi.datasource.dao.my1.Test1Mapper"><select id="query" resultType="com.multi.datasource.entity.UserEntity"> select user_name as userName from t_user </select></mapper>

从my2数据源查询

 

  

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.multi.datasource.dao.my2.Test2Mapper"> <select id="query" resultType="com.multi.datasource.entity.UserEntity"> select user_name as userName from t_user </select> </mapper>

 

  

7. Controller测试

package com.multi.datasource.controller;import com.multi.datasource.dao.my1.Test1Mapper;import com.multi.datasource.dao.my2.Test2Mapper;import com.multi.datasource.entity.UserEntity;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import javax.annotation.Resource;@RestControllerpublic class TestController { @Resource private Test1Mapper test1Mapper; @Resource private Test2Mapper test2Mapper; @RequestMapping("query") public void query() { UserEntity user1 = test1Mapper.query(); System.out.println("my1 dataSource:" + user1); UserEntity user2 = test2Mapper.query(); System.out.println("my2 dataSource:" + user2); }}

两个数据源,对应的user_name分别是zhangsan和lisi

 

  

 

  

 

  

 

  

8. 结果验证

访问 http://localhost:18888/query,结果如下

 

  

 

  到此这篇关于使用SpringBoot配置多数据源的经验分享的文章就介绍到这了,更多相关SpringBoot配置多数据源内容请搜索盛行IT以前的文章或继续浏览下面的相关文章希望大家以后多多支持盛行IT!

郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。

留言与评论(共有 条评论)
   
验证码: