码迷,mamicode.com
首页 > 编程语言 > 详细

spring boot配置多数据源

时间:2021-05-24 15:52:36      阅读:0      评论:0      收藏:0      [点我收藏+]

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

1、pom.xml文件

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- 测试 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<!-- mysql 依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- springboot-web组件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</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数据源<br>
* 作者: 项少龙<br>
* 联系方式:QQ1343741755<br>
*/
// 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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!