偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

SpringBoot+Mybatis多數(shù)據(jù)源配置和切換

數(shù)據(jù)庫(kù) 其他數(shù)據(jù)庫(kù)
今天介紹一個(gè)SpringBoot+mybatis的多數(shù)據(jù)源的解決方案。

前言

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

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

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

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

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

  1. application.yml中配置兩個(gè)數(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<String> mapperLocations = new ArrayList<>();
mapperLocations.add("classpath*:/mapper/master/*.xml");
List<Resource> 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);
}
}

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

實(shí)現(xiàn)效果

以上的配置已經(jīng)實(shí)現(xiàn)了多數(shù)據(jù)源的配置,下面實(shí)現(xiàn)一個(gè)功能。通過(guò)一個(gè)接口同時(shí)向兩個(gè)庫(kù)的兩個(gè)表中導(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("中國(guó)");
masterPersonMapper.insertSelective(person1);

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

執(zhí)行:

結(jié)果:

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

責(zé)任編輯:姜華 來(lái)源: 今日頭條
相關(guān)推薦

2024-10-30 10:22:17

2023-06-07 08:08:37

MybatisSpringBoot

2023-09-07 08:39:39

copy屬性數(shù)據(jù)源

2020-12-31 07:55:33

spring bootMybatis數(shù)據(jù)庫(kù)

2020-03-13 14:05:14

SpringBoot+數(shù)據(jù)源Java

2020-06-02 07:55:31

SpringBoot多數(shù)據(jù)源

2022-05-10 10:43:35

數(shù)據(jù)源動(dòng)態(tài)切換Spring

2024-04-30 09:17:06

SpringBootMybatis動(dòng)態(tài)數(shù)據(jù)源

2022-12-19 07:21:35

Hutool-db數(shù)據(jù)庫(kù)JDBC

2023-11-27 07:33:55

2009-06-15 13:24:46

JBoss數(shù)據(jù)源

2010-12-27 09:59:11

ODBC數(shù)據(jù)源

2020-11-24 09:56:12

數(shù)據(jù)源讀寫分離

2025-04-14 01:00:00

Calcite電商系統(tǒng)MySQL

2009-08-14 10:26:27

ibatis多數(shù)據(jù)源

2023-10-31 07:52:53

多數(shù)據(jù)源管理后端

2022-05-18 12:04:19

Mybatis數(shù)據(jù)源Spring

2024-11-20 09:12:56

2025-01-09 11:21:25

2023-02-06 14:44:00

嚴(yán)選數(shù)據(jù)源DB
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)