1. 修改application.yml
注意
- 连接多个数据库时,url要更改为jdbc-url,否则会报错。
- 多数据源的配置中需要指定具体的名称来区分不同的数据库(上述配置中的dev和local,名称可以根据具体需求自定义)
- mybatis配置别名不起作用,因此注释掉了。
#mybatis:
# type-aliases-package: usts.eie.sbmp.pojo
spring:
datasource:
dev:
username: root
password: root
jdbc-url: jdbc:mysql://114.55.174.146:3306/hello?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
driver-class-name: com.mysql.cj.jdbc.Driver
local:
username: root
password: root
jdbc-url: jdbc:mysql://localhost:3306/ssmbuild?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
driver-class-name: com.mysql.cj.jdbc.Driver
2. 添加数据库连接配置文件
dev数据源配置文件
@Configuration
@MapperScan(basePackages = "usts.eie.sbmp.dao.dev",sqlSessionFactoryRef = "devSqlSessionFactory")
public class DevDataSourceConfig {
@Primary
@Bean(name = "devDataSource")
@ConfigurationProperties("spring.datasource.dev")
public DataSource masterDataSource(){
return DataSourceBuilder.create().build();
}
@Bean(name = "devSqlSessionFactory")
public SqlSessionFactory sqlSessionFactory(@Qualifier("devDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
sessionFactoryBean.setDataSource(dataSource);
sessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources("classpath:mapper/dev/*.xml"));
return sessionFactoryBean.getObject();
}
}
local数据源配置文件
package usts.eie.sbmp.config;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
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.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import javax.sql.DataSource;
@Configuration
@MapperScan(basePackages = "usts.eie.sbmp.dao.local",sqlSessionFactoryRef = "localSqlSessionFactory")
public class LocalDataSourceConfig {
@Primary
@Bean(name = "localDataSource")
@ConfigurationProperties("spring.datasource.local")
public DataSource masterDataSource(){
return DataSourceBuilder.create().build();
}
@Bean(name = "localSqlSessionFactory")
public SqlSessionFactory sqlSessionFactory(@Qualifier("localDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
sessionFactoryBean.setDataSource(dataSource);
sessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources("classpath:mapper/local/*.xml"));
return sessionFactoryBean.getObject();
}
}
注意
不同配置文件通过@MapperScan注解的内容来区分不同数据库下的mapper文件,由于这里连接的是两个数据库,所以这里两个值不能一样,即第一个和第二个数据库的mapper应该放在两个不同的目录下。否则启动会报找不到表的错误。
注解内容的对应关系如下图所示
3. 修改主启动类
4. 其他
除此之外,pojo,mapper,service的写法和之前一样。
注意:由于没有为pojo中的类配置别名,所以在写对应的 mapper.xml文件时,要把类名写完整 如下图所示
项目总体结构如下图所示
相关文章
暂无评论...