自从微服务之后,基本上很少再使用多数据源了. 但是不排除某些情况下还会使用, 在此记录下spring-boot的多数据源. 按着套路走–:
一:添加相关依赖
<!–集成mybatis–>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1 </dependency>
<!– 数据库驱动 –>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!– Druid 数据连接池依赖 –>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.18</version>
</dependency>
二:配置文件中定义数据源
master.datasource.url=jdbc:mysql://localhost:3306/boot1
master.datasource.username=root
master.datasource.password=root
master.datasource.driverClassName=com.mysql.jdbc.Driver
slave.datasource.url=jdbc:mysql://localhost:3306/boot2
slave.datasource.username=root
slave.datasource.password=root
slave.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.max-idle=10
spring.datasource.max-wait=3000
spring.datasource.min-idle=5
spring.datasource.initial-size=5
#配置mybatis扫描文件,不同的数据源扫描不同SQL文件
master.location=classpath:/mapper/master/*.xml
slave.location=classpath:/mapper/slave/*.xml
三:编写创建数据源工具类
public class DataSourceUtils {
public final static DataSource createDataSource(String driverClass,String url,String user,String password) {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(driverClass);
dataSource.setUrl(url);
dataSource.setUsername(user);
dataSource.setPassword(password);
//dataSource.setMaxActive(maxActive);
//dataSource.setInitialSize(5);
return dataSource;
}
}
四:编写数据源配置
@MapperScan分包扫描,@Primary代表默认数据源,多个数据源必须有一个是指定为默认的启动数据源, 否则报错;
@Configuration
@MapperScan(basePackages = MasterDataSourceConfig.PACKAGE, sqlSessionFactoryRef = "masterSqlSessionFactory")
public class MasterDataSourceConfig {
static final String PACKAGE = "com.surd.springboot127.dao.master";
@Value("${master.location}")
private String mapper_location ;
@Value("${master.datasource.url}")
private String url;
@Value("${master.datasource.username}")
private String user;
@Value("${master.datasource.password}")
private String password;
@Value("${master.datasource.driverClassName}")
private String driverClass;
@Bean(name = "masterDataSource")
@Primary
public DataSource masterDataSource() {
return DataSourceUtils.createDataSource(driverClass,url,user,password);
}
@Bean(name = "masterTransactionManager")
@Primary
public DataSourceTransactionManager masterTransactionManager() {
return new DataSourceTransactionManager(masterDataSource());
}
@Bean(name = "masterSqlSessionFactory")
@Primary
public SqlSessionFactory masterSqlSessionFactory(
@Qualifier("masterDataSource")DataSource masterDataSource)
throws Exception {
final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(masterDataSource);
sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources(mapper_location));
return sessionFactory.getObject();
}
}
@Configuration
@MapperScan(basePackages = SlaveDataSourceConfig.PACKAGE, sqlSessionFactoryRef = "slaveSqlSessionFactory")
public class SlaveDataSourceConfig {
static final String PACKAGE = "com.surd.springboot127.dao.slave";
@Value("${slave.location}")
private String mapper_location;
@Value("${slave.datasource.url}")
private String url;
@Value("${slave.datasource.username}")
private String user;
@Value("${slave.datasource.password}")
private String password;
@Value("${slave.datasource.driverClassName}")
private String driverClass;
@Bean(name = "slaveDataSource")
public DataSource slaveDataSource() {
return DataSourceUtils.createDataSource(driverClass,url,user,password);
}
@Bean(name = "slaveTransactionManager")
public DataSourceTransactionManager clusterTransactionManager(
@Qualifier("slaveDataSource") DataSource slaveDataSource) {
return new DataSourceTransactionManager(slaveDataSource);
}
@Bean(name = "slaveSqlSessionFactory")
public SqlSessionFactory clusterSqlSessionFactory(
@Qualifier("slaveDataSource") DataSource slaveDataSource)
throws Exception {
final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(slaveDataSource);
sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources(mapper_location));
return sessionFactory.getObject();
}
}
五:使用
@Service
public class UserService {
@Autowired
private UserDao userDao;
@Autowired
private SchoolDao schoolDao;
public User getUser() {
User user = userDao.getUser();
SchoolVo schoolVo = schoolDao.findByName("清华");
user.setSchoolVo(schoolVo);
return user;
}
public SchoolVo getSchool() {
SchoolVo schoolVo = schoolDao.findByName("清华");
return schoolVo;
}
}
六:看下最终的目录结构
到此结束;