mybtisplus 多数据源 集成 mysql jdbc 集成 neo4j jdbc相关代码
时间: 2023-11-22 12:34:24 浏览: 44
mybatis-plus 多数据源集成 mysql 和 neo4j 的代码示例:
首先在 pom.xml 中添加 mybatis-plus-boot-starter 和 neo4j-jdbc-driver 依赖:
```xml
<dependencies>
<!-- mybatis-plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3.1</version>
</dependency>
<!-- mysql jdbc -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.24</version>
</dependency>
<!-- neo4j jdbc -->
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-jdbc-driver</artifactId>
<version>4.3.1</version>
</dependency>
</dependencies>
```
然后在 application.properties 中配置两个数据源的相关信息:
```properties
# mysql 数据源配置
spring.datasource.mysql.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
spring.datasource.mysql.username=root
spring.datasource.mysql.password=root
spring.datasource.mysql.driver-class-name=com.mysql.cj.jdbc.Driver
# neo4j 数据源配置
spring.datasource.neo4j.url=jdbc:neo4j:bolt://localhost:7687
spring.datasource.neo4j.username=neo4j
spring.datasource.neo4j.password=123456
spring.datasource.neo4j.driver-class-name=org.neo4j.jdbc.Driver
```
接着在 MybatisPlusConfig.java 中配置多数据源:
```java
@Configuration
public class MybatisPlusConfig {
@Bean
@ConfigurationProperties(prefix = "spring.datasource.mysql")
public DataSource mysqlDataSource() {
return DataSourceBuilder.create().build();
}
@Bean
@ConfigurationProperties(prefix = "spring.datasource.neo4j")
public DataSource neo4jDataSource() {
return DataSourceBuilder.create().build();
}
@Bean
@Primary
public DataSource multipleDataSource(@Qualifier("mysqlDataSource") DataSource mysqlDataSource,
@Qualifier("neo4jDataSource") DataSource neo4jDataSource) {
Map<Object, Object> targetDataSources = new HashMap<>();
targetDataSources.put(DbType.MYSQL, mysqlDataSource);
targetDataSources.put(DbType.NEO4J, neo4jDataSource);
DynamicDataSource dynamicDataSource = new DynamicDataSource();
dynamicDataSource.setTargetDataSources(targetDataSources);
dynamicDataSource.setDefaultTargetDataSource(mysqlDataSource);
return dynamicDataSource;
}
@Bean
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource);
return sqlSessionFactoryBean.getObject();
}
}
```
上述代码中,我们定义了两个数据源:mysqlDataSource 和 neo4jDataSource,使用 @ConfigurationProperties 注解读取 application.properties 中的配置信息。然后通过 DynamicDataSource 类将这两个数据源动态切换。
最后在实体类对应的 Mapper 中使用注解 @DS 指定数据源,示例代码如下:
```java
@Mapper
public interface UserMapper extends BaseMapper<User> {
@DS(DbType.MYSQL)
@Select("select * from user")
List<User> selectMysqlUsers();
@DS(DbType.NEO4J)
@Select("match (n:User) return n")
List<User> selectNeo4jUsers();
}
```
上述代码中,我们在查询 mysql 和 neo4j 数据库的方法上分别使用了不同的数据源。其中 @DS 注解是自定义的一个切面注解,用于切换数据源。DbType 是一个枚举类,用于标识数据源类型。
完整示例代码可以参考以下链接:
https://github.com/zhengweikeng/spring-boot-demo/tree/master/mybatis-plus-multi-datasource
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)