java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > MyBatis-Plus多数据源

MyBatis-Plus多数据源的示例代码

作者:guiying712

本文主要介绍了MyBatis-Plus多数据源的示例代码,包括依赖配置、数据源配置、Mapper 和 Service 的定义,具有一定的参考价值,感兴趣的可以了解一下

下面是一个完整的 MyBatis-Plus 多数据源支持的示例代码,包括依赖配置、数据源配置、Mapper 和 Service 的定义。

1. 添加依赖

在 pom.xml 中添加 MyBatis-Plus 和多数据源相关的依赖:

<dependencies>
    <!-- MyBatis-Plus 依赖 -->
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>3.4.3.4</version>
    </dependency>
    <!-- HikariCP 连接池依赖 -->
    <dependency>
        <groupId>com.zaxxer</groupId>
        <artifactId>HikariCP</artifactId>
        <version>3.4.5</version>
    </dependency>
    <!-- MySQL 驱动依赖 -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.23</version>
    </dependency>
</dependencies>

2. 配置数据源

在 application.yml 中配置多个数据源:

spring:
  datasource:
    primary:
      jdbc-url: jdbc:mysql://localhost:3306/primary_db
      username: root
      password: root
      driver-class-name: com.mysql.cj.jdbc.Driver
    secondary:
      jdbc-url: jdbc:mysql://localhost:3306/secondary_db
      username: root
      password: root
      driver-class-name: com.mysql.cj.jdbc.Driver

3. 数据源配置类

创建数据源配置类 DataSourceConfig.java

import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.zaxxer.hikari.HikariDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
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 org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

@Configuration
public class DataSourceConfig {

    @Primary
    @Bean(name = "primaryDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.primary")
    public DataSource primaryDataSource() {
        return new HikariDataSource();
    }

    @Bean(name = "secondaryDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.secondary")
    public DataSource secondaryDataSource() {
        return new HikariDataSource();
    }

    @Primary
    @Bean(name = "primaryTransactionManager")
    public DataSourceTransactionManager primaryTransactionManager(@Qualifier("primaryDataSource") DataSource primaryDataSource) {
        return new DataSourceTransactionManager(primaryDataSource);
    }

    @Bean(name = "secondaryTransactionManager")
    public DataSourceTransactionManager secondaryTransactionManager(@Qualifier("secondaryDataSource") DataSource secondaryDataSource) {
        return new DataSourceTransactionManager(secondaryDataSource);
    }

    @Primary
    @Bean(name = "primarySqlSessionFactory")
    public SqlSessionFactory primarySqlSessionFactory(@Qualifier("primaryDataSource") DataSource primaryDataSource) throws Exception {
        MybatisSqlSessionFactoryBean sessionFactoryBean = new MybatisSqlSessionFactoryBean();
        sessionFactoryBean.setDataSource(primaryDataSource);
        return sessionFactoryBean.getObject();
    }

    @Bean(name = "secondarySqlSessionFactory")
    public SqlSessionFactory secondarySqlSessionFactory(@Qualifier("secondaryDataSource") DataSource secondaryDataSource) throws Exception {
        MybatisSqlSessionFactoryBean sessionFactoryBean = new MybatisSqlSessionFactoryBean();
        sessionFactoryBean.setDataSource(secondaryDataSource);
        return sessionFactoryBean.getObject();
    }
}

4. 指定 Mapper 扫描路径

在不同的包路径下创建 Mapper 接口,并使用 @MapperScan 注解指定使用的 SqlSessionFactory

创建 PrimaryDataSourceConfig.java

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@MapperScan(basePackages = "com.example.primary.mapper", sqlSessionFactoryRef = "primarySqlSessionFactory")
public class PrimaryDataSourceConfig {
}

创建 SecondaryDataSourceConfig.java

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@MapperScan(basePackages = "com.example.secondary.mapper", sqlSessionFactoryRef = "secondarySqlSessionFactory")
public class SecondaryDataSourceConfig {
}

5. 创建实体类和 Mapper 接口

创建两个实体类 PrimaryEntity 和 SecondaryEntity

// PrimaryEntity.java
package com.example.primary.entity;

import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;

@TableName("primary_table")
public class PrimaryEntity {
    @TableId
    private Long id;
    private String name;

    // Getters and Setters
}

// SecondaryEntity.java
package com.example.secondary.entity;

import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;

@TableName("secondary_table")
public class SecondaryEntity {
    @TableId
    private Long id;
    private String name;

    // Getters and Setters
}

创建 Mapper 接口:

// PrimaryMapper.java
package com.example.primary.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.primary.entity.PrimaryEntity;

public interface PrimaryMapper extends BaseMapper<PrimaryEntity> {
}

// SecondaryMapper.java
package com.example.secondary.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.secondary.entity.SecondaryEntity;

public interface SecondaryMapper extends BaseMapper<SecondaryEntity> {
}

6. 创建 Service 类

创建 Service 类,分别处理不同数据源的业务逻辑:

// PrimaryService.java
package com.example.primary.service;

import com.example.primary.entity.PrimaryEntity;
import com.example.primary.mapper.PrimaryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PrimaryService {

    @Autowired
    private PrimaryMapper primaryMapper;

    public List<PrimaryEntity> getAll() {
        return primaryMapper.selectList(null);
    }
}

// SecondaryService.java
package com.example.secondary.service;

import com.example.secondary.entity.SecondaryEntity;
import com.example.secondary.mapper.SecondaryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SecondaryService {

    @Autowired
    private SecondaryMapper secondaryMapper;

    public List<SecondaryEntity> getAll() {
        return secondaryMapper.selectList(null);
    }
}

7. 创建 Controller 类

最后,为每个数据源创建对应的控制器以提供 RESTful 接口:

// PrimaryController.java
package com.example.primary.controller;

import com.example.primary.entity.PrimaryEntity;
import com.example.primary.service.PrimaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/primary")
public class PrimaryController {

    @Autowired
    private PrimaryService primaryService;

    @GetMapping("/all")
    public List<PrimaryEntity> getAll() {
        return primaryService.getAll();
    }
}

// SecondaryController.java
package com.example.secondary.controller;

import com.example.secondary.entity.SecondaryEntity;
import com.example.secondary.service.SecondaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/secondary")
public class SecondaryController {

    @Autowired
    private SecondaryService secondaryService;

    @GetMapping("/all")
    public List<SecondaryEntity> getAll() {
        return secondaryService.getAll();
    }
}

通过上述步骤,就可以在 Spring Boot 项目中使用 MyBatis-Plus 实现多数据源支持了。这种配置方式可以方便地管理和使用不同的数据源,适应不同的业务需求。

到此这篇关于MyBatis-Plus多数据源的示例代码的文章就介绍到这了,更多相关MyBatis-Plus多数据源内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文