在上一篇文章我們已經整合完畢了JPA,并且可以正常訪問冈敛,本文我們整合多個數(shù)據(jù)源待笑。首先我們要在配置文件中配置兩個數(shù)據(jù)源(分包原理實現(xiàn))
spring.datasource.test1.driverClassName = com.mysql.jdbc.Driver
spring.datasource.test1.url = jdbc:mysql://localhost:3306/my-first?useUnicode=true&characterEncoding=utf-8
spring.datasource.test1.username = root
spring.datasource.test1.password = root
spring.datasource.test2.driverClassName = com.mysql.jdbc.Driver
spring.datasource.test2.url = jdbc:mysql://localhost:3306/my-second?useUnicode=true&characterEncoding=utf-8
spring.datasource.test2.username = root
spring.datasource.test2.password = root
隨后創(chuàng)建數(shù)據(jù)源配置文件
配置文件此處只展示一個
package com.lwq.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.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
@Configuration // 注冊到springboot容器中
@MapperScan(basePackages = "com.lwq.test01", sqlSessionFactoryRef = "test1SqlSessionFactory")
public class DataSource1Config {
/**
* @methodDesc: 功能描述:(配置test1數(shù)據(jù)庫)
*/
@Bean(name = "test1DataSource")
@Primary
@ConfigurationProperties(prefix = "spring.datasource.test1")
public DataSource testDataSource() {
return DataSourceBuilder.create().build();
}
/**
* @methodDesc: 功能描述:(test1 sql會話工廠)
*/
@Bean(name = "test1SqlSessionFactory")
@Primary
public SqlSessionFactory testSqlSessionFactory(@Qualifier("test1DataSource") DataSource dataSource)
throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
return bean.getObject();
}
/**
* @methodDesc: 功能描述:(test1 事物管理)
* @QQ:644064779
*/
@Bean(name = "test1TransactionManager")
@Primary
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);
}
}
注意:@Primary注解必須要在某一個配置中聲明暮蹂,此處為默認數(shù)據(jù)源癌压,如果不指定,啟動會報錯
然后創(chuàng)建兩個dao
package com.lwq.test01.dao;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
public interface User1Mapper {
@Insert("insert into users values(null,#{name},#{age});")
public int addUser(@Param("name") String name, @Param("age") Integer age);
}
在controller中完成調用
@RequestMapping("/insertTest01")
public String insertTest01(String name, Integer age) {
user1Mapper.addUser(name,age);
return "success";
}
@RequestMapping("/insertTest02")
public String insertTest02(String name, Integer age) {
user2Mapper.addUser(name, age);
return "success";
}
訪問即刻訪問到不同的數(shù)據(jù)源