spring boot配置多數據源

一、pom.xml文件java

<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> mysql 依賴 --><dependency><groupId>mysqlgroupId><artifactId>mysql-connector-javaartifactId>dependency> springboot-web組件 --><dependency><groupId>org.springframework.bootgroupId><artifactId>spring-boot-starter-webartifactId>dependency>

二、application.propertiesmysql

###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-8spring.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-8spring.datasource.test2.username = root
spring.datasource.test2.password = 12345678

三、DataSource1Config.javaweb

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);
}

}

四、UserDaoTest1.javaspring

package com.xiangwen.respostory1;import lombok.extern.slf4j.Slf4j;

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

 

五、UserServices1.javasql

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
@Slf4jpublic class UserServicer1 {
@Autowiredprivate 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;
}
}
相關文章
相關標籤/搜索