先看一下目录结构。
网上配置的多数据源有很多,但一定要配置适合自己的,中间很多坑,后边会一一标出来。
目的就是项目中不同的业务去读取不同的数据库,目录结构配置如上。
直接上代码
配置springboot的配置文件,配置好的配置文件
配置两个data类
DataSource1 类
package com.honghe.operations.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.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import javax.sql.DataSource;
//注解到spring容器中
@Configuration
//@MapperScan(basePackages = "com.honghe.operations.db.mapper.data1",sqlSessionFactoryRef = "data1SqlSessionFactory")
@MapperScan(basePackages = "com.honghe.operations.db.dao.centerservice.data1",sqlSessionFactoryRef = "data1SqlSessionFactory")
public class DataSource1 {
/**
* 返回data1数据库的数据源
* @return
*/
@Bean(name="data1Source")
//主数据源
@Primary
@ConfigurationProperties(prefix = "com.honghe.operations.datasource.DataSource1")
public DataSource dataSource(){
// return DataSourceBuilder.create().build();
DriverManagerDataSource ret = new DriverManagerDataSource();
ret.setDriverClassName("com.mysql.jdbc.Driver");
ret.setUsername("root");
ret.setPassword("bhjRjxwC8EBqaJC7");
ret.setUrl("jdbc:mysql://localhost:3306/hht_dmanager?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true&zeroDateTimeBehavior=convertToNull");
return ret;
}
/**
* 返回data1数据库的会话工厂
* @param ds
* @return
* @throws Exception
*/
@Bean(name = "data1SqlSessionFactory")
@Primary
public SqlSessionFactory sqlSessionFactory(@Qualifier("data1Source") DataSource ds) throws Exception{
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(ds);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:com/honghe/operations/db/mapper/centerservice/data1/*.xml"));
return bean.getObject();
}
/**
* 返回data1数据库的会话模板
* @param sessionFactory
* @return
* @throws Exception
*/
@Bean(name = "data1SqlSessionTemplate")
@Primary
public SqlSessionTemplate sqlSessionTemplate(@Qualifier("data1SqlSessionFactory") SqlSessionFactory sessionFactory) throws Exception{
return new SqlSessionTemplate(sessionFactory);
}
/**
* 返回data1数据库的事务
* @param ds
* @return
*/
@Bean(name = "data1TransactionManager")
@Primary
public DataSourceTransactionManager transactionManager(@Qualifier("data1Source") DataSource ds){
return new DataSourceTransactionManager(ds);
}
}
踩坑:
这三个地方小心配置,配置自己项目dao和mapper的路径,我已经踩过坑。
DataSource2
package com.honghe.operations.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.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import javax.sql.DataSource;
@Configuration//注解到spring容器中
@MapperScan(basePackages = "com.honghe.operations.db.dao.centerservice.data2", sqlSessionFactoryRef = "data2SqlSessionFactory")
public class DataSource2 {
/**
* 返回data2数据库的数据源
*
* @return
*/
@Bean(name = "data2Source")
@ConfigurationProperties(prefix = "com.honghe.operations.datasource.DataSource2")
public DataSource dataSource() {
// return DataSourceBuilder.create().build();
DriverManagerDataSource ret = new DriverManagerDataSource();
ret.setDriverClassName("com.mysql.jdbc.Driver");
ret.setUsername("root");
ret.setPassword("bhjRjxwC8EBqaJC7");
ret.setUrl("jdbc:mysql://localhost:3306/hht_device_operation?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true&zeroDateTimeBehavior=convertToNull");
return ret;
}
/**
* 返回data2数据库的会话工厂
*
* @param ds
* @return
* @throws Exception
*/
@Bean(name = "data2SqlSessionFactory")
public SqlSessionFactory sqlSessionFactory(@Qualifier("data2Source") DataSource ds) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(ds);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:com/honghe/operations/db/mapper/centerservice/data2/*.xml"));
return bean.getObject();
}
/**
* 返回data2数据库的会话模板
*
* @param sessionFactory
* @return
* @throws Exception
*/
@Bean(name = "data2SqlSessionTemplate")
public SqlSessionTemplate sqlSessionTemplate(@Qualifier("data2SqlSessionFactory") SqlSessionFactory sessionFactory) throws Exception {
return new SqlSessionTemplate(sessionFactory);
}
/**
* 返回data2数据库的事务
*
* @param ds
* @return
*/
@Bean(name = "data2TransactionManager")
public DataSourceTransactionManager transactionManager(@Qualifier("data2Source") DataSource ds) {
return new DataSourceTransactionManager(ds);
}
}
至此,配置完成。虽然只有配置文件和配置类需要配置。但是其中一定要搞明白自己项目db层的路径,否则就会报各种找不到文件的错,共勉