spring boot配置多数据源

2021-05-28 11:05

阅读:472

标签:容器   man   tis   color   rgb   会话   qualifier   repo   pen   

1、pom.xml文件

dependency>
groupId>org.projectlombokgroupId>
artifactId>lombokartifactId>
dependency>

dependency>
groupId>org.springframework.bootgroupId>
artifactId>spring-boot-starterartifactId>
dependency>

dependency>
groupId>org.springframework.bootgroupId>
artifactId>spring-boot-starter-testartifactId>
scope>testscope>
dependency>
dependency>
groupId>org.mybatis.spring.bootgroupId>
artifactId>mybatis-spring-boot-starterartifactId>
version>1.1.1version>
dependency>

dependency>
groupId>mysqlgroupId>
artifactId>mysql-connector-javaartifactId>
dependency>

dependency>
groupId>org.springframework.bootgroupId>
artifactId>spring-boot-starter-webartifactId>
dependency>

2、application.properties

###datasource1
spring.datasource.test1.driver-class-name = com.mysql.jdbc.Driver
spring.datasource.test1.jdbc-url = jdbc:mysql://localhost:3306/xiangwen?useUnicode=true&characterEncoding=utf-8
spring.datasource.test1.username = root
spring.datasource.test1.password = 12345678
###datasource2
spring.datasource.test2.driver-class-name = com.mysql.jdbc.Driver
spring.datasource.test2.jdbc-url = jdbc:mysql://localhost:3306/xiangwu?useUnicode=true&characterEncoding=utf-8
spring.datasource.test2.username = root
spring.datasource.test2.password = 12345678

3、DataSource1Config.java

package com.xiangwen.datasource;

import javax.sql.DataSource;

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.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

/**
* 读取DataSource01数据源
* 作者: 项少龙
* 联系方式:QQ1343741755
*/ // DataSource01 @Configuration // 注册到springboot容器中 @MapperScan(basePackages = "com.xiangwen.repostory1", sqlSessionFactoryRef = "test1SqlSessionFactory") public class DataSource1Config { /** * * @methodDesc: 功能描述:(配置test01数据库) * @author: 项少龙 * @param: @return * @createTime:2017年9月17日 下午3:16:44 * @returnType:@return DataSource * @copyright:青春 * @QQ:1343741755 */ @Bean(name = "test1DataSource") @ConfigurationProperties(prefix = "spring.datasource.test1") public DataSource testDataSource() { return DataSourceBuilder.create().build(); } /** * * @methodDesc: 功能描述:(test1 sql会话工厂) * @author: 项少龙 * @param: @param * dataSource * @param: @return * @param: @throws * Exception * @createTime:2017年9月17日 下午3:17:08 * @returnType:@param dataSource * @returnType:@return * @returnType:@throws Exception SqlSessionFactory * @copyright:青春 * @QQ:1343741755 */ @Bean(name = "test1SqlSessionFactory") public SqlSessionFactory testSqlSessionFactory(@Qualifier("test1DataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); // bean.setMapperLocations( // new // PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/test1/*.xml")); return bean.getObject(); } /** * * @methodDesc: 功能描述:(test1 事物管理) * @author: 项少龙 * @param: @param * dataSource * @param: @return * @param: @throws * Exception * @createTime:2017年9月17日 下午3:17:08 * @returnType:@param dataSource * @returnType:@return * @returnType:@throws Exception SqlSessionFactory * @copyright:青春 * @QQ:1343741755 */ @Bean(name = "test1TransactionManager") public DataSourceTransactionManager testTransactionManager(@Qualifier("test1DataSource") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } @Bean(name = "test1SqlSessionTemplate") public SqlSessionTemplate testSqlSessionTemplate( @Qualifier("test1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception { return new SqlSessionTemplate(sqlSessionFactory); } }

4、UserDaoTest1.java

package com.xiangwen.respostory1;

import lombok.extern.slf4j.Slf4j;

@Mapper
public interface UserDaoTest1 {
    public int adduser(String name, int age) ;
}

 

5、UserServices1.java

package com.xiangwen.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xiangwen.dao1.UserDaoTest1;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class UserServicer1 {
@Autowired
private UserDaoTest1 userDaoTest1;

@Transactional(transactionManager = "test1TransactionManager")
public int adduser(String name, int age) {
int res = userDaoTest1.inserUser(name, age);
log.info("============进来了UserServer1");
int a = 1 / age;
return res;
}
}

 

spring boot配置多数据源

标签:容器   man   tis   color   rgb   会话   qualifier   repo   pen   

原文地址:https://www.cnblogs.com/wenwenzuiniucha/p/14783615.html


评论


亲,登录后才可以留言!