日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
SpringBoot+Mybatis多數(shù)據(jù)源配置和切換

前言

在項目開發(fā)中,經(jīng)常會涉及到一個應(yīng)用程序調(diào)用多個數(shù)據(jù)的情況。今天介紹一個SpringBoot+mybatis的多數(shù)據(jù)源的解決方案。

創(chuàng)新互聯(lián)專注于企業(yè)全網(wǎng)營銷推廣、網(wǎng)站重做改版、潼關(guān)網(wǎng)站定制設(shè)計、自適應(yīng)品牌網(wǎng)站建設(shè)、H5場景定制商城網(wǎng)站定制開發(fā)、集團公司官網(wǎng)建設(shè)、外貿(mào)網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計等建站業(yè)務(wù),價格優(yōu)惠性價比高,為潼關(guān)等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。

數(shù)據(jù)庫準(zhǔn)備

創(chuàng)建兩個數(shù)據(jù)庫,兩個數(shù)據(jù)庫都有Im_person表,兩個表中無數(shù)據(jù)。

代碼結(jié)構(gòu)

說明:我這里只是為了體現(xiàn)效果,就省略了service步驟。各位大牛開發(fā),不喜勿噴,理解萬歲,嘻嘻??!

  1. application.yml中配置兩個數(shù)據(jù)源,配置如下:
master:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/db1?useUnicode=true&characterEncoding=utf-8&useSSL=false&allowMultiQueries=true&serverTimezone=GMT%2B8
username: root
password: 123456

slave:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/db2?useUnicode=true&characterEncoding=utf-8&useSSL=false&allowMultiQueries=true&serverTimezone=GMT%2B8
username: root
password: 123456

2.SpringBoot多數(shù)據(jù)源配置類。

@Configuration
@MapperScan(basePackages = "com.zhangls.multipledatasource.dao.master", sqlSessionFactoryRef = "masterSqlSessionFactory")
public class MasterDataSourceConfig {
@Value("${master.datasource.driver-class-name}")
private String driverClassName;

@Value("${master.datasource.url}")
private String url;

@Value("${master.datasource.username}")
private String username;

@Value("${master.datasource.password}")
private String password;

@Bean(name = "masterDataSource")
@Primary
public DataSource dataSource() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(this.driverClassName);
dataSource.setUrl(this.url);
dataSource.setUsername(this.username);
dataSource.setPassword(this.password);
return dataSource;
}

@Bean(name = "masterSqlSessionFactory")
@Primary
public SqlSessionFactory sqlSessionFactory(@Qualifier("masterDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();
List mapperLocations = new ArrayList<>();
mapperLocations.add("classpath*:/mapper/master/*.xml");
List resources = new ArrayList();
if (mapperLocations != null) {
for (String mapperLocation : mapperLocations) {
try {
Resource[] mappers = resourceResolver.getResources(mapperLocation);
resources.addAll(Arrays.asList(mappers));
} catch (IOException e) {
// ignore
}
}
}

bean.setMapperLocations(resources.toArray(new Resource[resources.size()]));
return bean.getObject();
}

@Bean(name = "masterTransactionManager")
@Primary
public DataSourceTransactionManager transactionManager(@Qualifier("masterDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}

@Bean(name = "masterSqlSessionTemplate")
@Primary
public SqlSessionTemplate testSqlSessionTemplate(
@Qualifier("masterSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
@Configuration
@MapperScan(basePackages = "com.zhangls.multipledatasource.dao.slave", sqlSessionFactoryRef = "slaveSqlSessionFactory")
public class SlaveDataSourceConfig {
@Value("${slave.datasource.driver-class-name}")
private String driverClassName;

@Value("${slave.datasource.url}")
private String url;

@Value("${slave.datasource.username}")
private String username;

@Value("${slave.datasource.password}")
private String password;

@Bean(name = "slaveDataSource")
public DataSource dataSource() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(this.driverClassName);
dataSource.setUrl(this.url);
dataSource.setUsername(this.username);
dataSource.setPassword(this.password);
return dataSource;
}

@Bean(name = "slaveSqlSessionFactory")
public SqlSessionFactory sqlSessionFactory(@Qualifier("slaveDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(
new PathMatchingResourcePatternResolver().getResources("classpath*:/mapper/slave/*.xml"));
return bean.getObject();
}

@Bean(name = "slaveTransactionManager")
public DataSourceTransactionManager transactionManager(@Qualifier("slaveDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}

@Bean(name = "slaveSqlSessionTemplate")
public SqlSessionTemplate testSqlSessionTemplate(
@Qualifier("masterSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}

說明:兩個數(shù)據(jù)的代碼基本相同,不同的是要配置好掃描包的路徑,以及mybatis的SQL文件的路徑。所以不同的連接DAO和mapper文件要放在不同的文件夾,方便配置管理。

實現(xiàn)效果

以上的配置已經(jīng)實現(xiàn)了多數(shù)據(jù)源的配置,下面實現(xiàn)一個功能。通過一個接口同時向兩個庫的兩個表中導(dǎo)入不同的數(shù)據(jù)。

controller代碼如下:

@RestController
public class DataSourceController {
@Resource
private MasterPersonMapper masterPersonMapper;
@Resource
private SlavePersonMapper slavePersonMapper;

@GetMapping("/datasource")
public String datasource() {
ImPerson person1 = new ImPerson();
person1.setPersonId("1");
person1.setGender("男");
person1.setBirthday(new Date());
person1.setLocation("中國");
masterPersonMapper.insertSelective(person1);

ImPerson person2 = new ImPerson();
person2.setPersonId("2");
person2.setGender("女");
person2.setBirthday(new Date());
person2.setLocation("中國北京");
slavePersonMapper.insertSelective(person2);
return "導(dǎo)入成功";
}
}

執(zhí)行:

結(jié)果:

可以看到兩個庫中的兩個表都導(dǎo)入了數(shù)據(jù)。


網(wǎng)站欄目:SpringBoot+Mybatis多數(shù)據(jù)源配置和切換
URL網(wǎng)址:http://m.5511xx.com/article/dpgpdgc.html