“Spring Boot 集成 MyBatis Plus 多数据源”的版本间的差异
跳到导航
跳到搜索
Jihongchang(讨论 | 贡献) (建立内容为“1”的新页面) |
Jihongchang(讨论 | 贡献) |
||
第1行: | 第1行: | ||
− | 1 | + | pom.xml<syntaxhighlight lang="xml"> |
+ | <?xml version="1.0" encoding="UTF-8"?> | ||
+ | <project xmlns="http://maven.apache.org/POM/4.0.0" | ||
+ | xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
+ | xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
+ | <modelVersion>4.0.0</modelVersion> | ||
+ | |||
+ | <groupId>io.github.jihch</groupId> | ||
+ | <artifactId>spring-boot-mybatis-plus-hikari-multiple-data-sources-integrated-mysql</artifactId> | ||
+ | <version>1.0-SNAPSHOT</version> | ||
+ | |||
+ | <parent> | ||
+ | <groupId>org.springframework.boot</groupId> | ||
+ | <artifactId>spring-boot-starter-parent</artifactId> | ||
+ | <version>2.4.13</version> | ||
+ | </parent> | ||
+ | |||
+ | <properties> | ||
+ | <maven.compiler.source>8</maven.compiler.source> | ||
+ | <maven.compiler.target>8</maven.compiler.target> | ||
+ | </properties> | ||
+ | |||
+ | <dependencies> | ||
+ | <dependency> | ||
+ | <groupId>org.springframework.boot</groupId> | ||
+ | <artifactId>spring-boot-starter-test</artifactId> | ||
+ | <scope>test</scope> | ||
+ | </dependency> | ||
+ | |||
+ | <dependency> | ||
+ | <groupId>org.projectlombok</groupId> | ||
+ | <artifactId>lombok</artifactId> | ||
+ | </dependency> | ||
+ | |||
+ | <dependency> | ||
+ | <groupId>com.baomidou</groupId> | ||
+ | <artifactId>mybatis-plus-boot-starter</artifactId> | ||
+ | <version>3.5.3</version> | ||
+ | </dependency> | ||
+ | |||
+ | <dependency> | ||
+ | <groupId>org.springframework.boot</groupId> | ||
+ | <artifactId>spring-boot-starter-web</artifactId> | ||
+ | <version>2.4.0</version> | ||
+ | </dependency> | ||
+ | |||
+ | <dependency> | ||
+ | <groupId>mysql</groupId> | ||
+ | <artifactId>mysql-connector-java</artifactId> | ||
+ | </dependency> | ||
+ | |||
+ | <dependency> | ||
+ | <groupId>org.springframework.boot</groupId> | ||
+ | <artifactId>spring-boot-configuration-processor</artifactId> | ||
+ | <optional>true</optional> | ||
+ | </dependency> | ||
+ | |||
+ | </dependencies> | ||
+ | |||
+ | </project> | ||
+ | </syntaxhighlight>application.yml<syntaxhighlight lang="yaml"> | ||
+ | spring: | ||
+ | datasource: | ||
+ | hikari: | ||
+ | datasource-world: | ||
+ | jdbc-url: jdbc:mysql://127.0.0.1:3306/world | ||
+ | username: root | ||
+ | password: 123456 | ||
+ | datasource-testdb: | ||
+ | jdbc-url: jdbc:mysql://127.0.0.1:3306/testdb | ||
+ | username: root | ||
+ | password: 123456 | ||
+ | |||
+ | logging: | ||
+ | level: | ||
+ | org: | ||
+ | springframework: | ||
+ | beans: | ||
+ | factory: DEBUG | ||
+ | </syntaxhighlight>DataSourceTestdbConfig.java<syntaxhighlight lang="java"> | ||
+ | package io.github.jihch.config; | ||
+ | |||
+ | import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean; | ||
+ | import com.zaxxer.hikari.HikariDataSource; | ||
+ | import org.apache.ibatis.session.SqlSessionFactory; | ||
+ | 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 javax.sql.DataSource; | ||
+ | |||
+ | @Configuration | ||
+ | @MapperScan(basePackages = "io.github.jihch.mapper.testdb", sqlSessionTemplateRef = "sqlSessionTemplateTestdb") | ||
+ | public class DataSourceTestdbConfig { | ||
+ | |||
+ | @Bean("dataSourceTestdb") | ||
+ | @ConfigurationProperties(prefix = "spring.datasource.hikari.datasource-testdb") | ||
+ | public DataSource dataSourceTestdb() { | ||
+ | return new HikariDataSource(); | ||
+ | } | ||
+ | |||
+ | @Bean | ||
+ | public SqlSessionFactory sqlSessionFactoryTestdb(@Qualifier("dataSourceTestdb") DataSource dataSource) throws Exception { | ||
+ | MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean(); | ||
+ | bean.setDataSource(dataSource); | ||
+ | return bean.getObject(); | ||
+ | } | ||
+ | |||
+ | @Bean | ||
+ | public SqlSessionTemplate sqlSessionTemplateTestdb(@Qualifier("sqlSessionFactoryTestdb") SqlSessionFactory sqlSessionFactory) { | ||
+ | return new SqlSessionTemplate(sqlSessionFactory); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | </syntaxhighlight>DataSourceWorldConfig.java<syntaxhighlight lang="java"> | ||
+ | package io.github.jihch.config; | ||
+ | |||
+ | import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean; | ||
+ | import com.zaxxer.hikari.HikariDataSource; | ||
+ | import org.apache.ibatis.session.SqlSessionFactory; | ||
+ | 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 javax.sql.DataSource; | ||
+ | |||
+ | @Configuration | ||
+ | @MapperScan(basePackages = "io.github.jihch.mapper.world", sqlSessionTemplateRef = "sqlSessionTemplateWorld") | ||
+ | public class DataSourceWorldConfig { | ||
+ | |||
+ | @Bean | ||
+ | @ConfigurationProperties(prefix = "spring.datasource.hikari.datasource-world") | ||
+ | public DataSource dataSourceWorld() { | ||
+ | return new HikariDataSource(); | ||
+ | } | ||
+ | |||
+ | @Bean | ||
+ | public SqlSessionFactory sqlSessionFactoryWorld(@Qualifier("dataSourceWorld") DataSource dataSource) | ||
+ | throws Exception { | ||
+ | MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean(); | ||
+ | bean.setDataSource(dataSource); | ||
+ | return bean.getObject(); | ||
+ | } | ||
+ | |||
+ | @Bean | ||
+ | public SqlSessionTemplate sqlSessionTemplateWorld(@Qualifier("sqlSessionFactoryWorld") SqlSessionFactory sqlSessionFactory) { | ||
+ | return new SqlSessionTemplate(sqlSessionFactory); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | </syntaxhighlight> |
2023年2月17日 (五) 09:24的版本
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>io.github.jihch</groupId>
<artifactId>spring-boot-mybatis-plus-hikari-multiple-data-sources-integrated-mysql</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.13</version>
</parent>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
</project>
application.yml
spring:
datasource:
hikari:
datasource-world:
jdbc-url: jdbc:mysql://127.0.0.1:3306/world
username: root
password: 123456
datasource-testdb:
jdbc-url: jdbc:mysql://127.0.0.1:3306/testdb
username: root
password: 123456
logging:
level:
org:
springframework:
beans:
factory: DEBUG
DataSourceTestdbConfig.java
package io.github.jihch.config;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.zaxxer.hikari.HikariDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
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 javax.sql.DataSource;
@Configuration
@MapperScan(basePackages = "io.github.jihch.mapper.testdb", sqlSessionTemplateRef = "sqlSessionTemplateTestdb")
public class DataSourceTestdbConfig {
@Bean("dataSourceTestdb")
@ConfigurationProperties(prefix = "spring.datasource.hikari.datasource-testdb")
public DataSource dataSourceTestdb() {
return new HikariDataSource();
}
@Bean
public SqlSessionFactory sqlSessionFactoryTestdb(@Qualifier("dataSourceTestdb") DataSource dataSource) throws Exception {
MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
bean.setDataSource(dataSource);
return bean.getObject();
}
@Bean
public SqlSessionTemplate sqlSessionTemplateTestdb(@Qualifier("sqlSessionFactoryTestdb") SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
DataSourceWorldConfig.java
package io.github.jihch.config;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.zaxxer.hikari.HikariDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
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 javax.sql.DataSource;
@Configuration
@MapperScan(basePackages = "io.github.jihch.mapper.world", sqlSessionTemplateRef = "sqlSessionTemplateWorld")
public class DataSourceWorldConfig {
@Bean
@ConfigurationProperties(prefix = "spring.datasource.hikari.datasource-world")
public DataSource dataSourceWorld() {
return new HikariDataSource();
}
@Bean
public SqlSessionFactory sqlSessionFactoryWorld(@Qualifier("dataSourceWorld") DataSource dataSource)
throws Exception {
MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
bean.setDataSource(dataSource);
return bean.getObject();
}
@Bean
public SqlSessionTemplate sqlSessionTemplateWorld(@Qualifier("sqlSessionFactoryWorld") SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
}