欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

MyBatis-Plus多數(shù)據(jù)源的示例代碼

 更新時(shí)間:2024年05月23日 09:49:46   作者:guiying712  
本文主要介紹了MyBatis-Plus多數(shù)據(jù)源的示例代碼,包括依賴配置、數(shù)據(jù)源配置、Mapper 和 Service 的定義,具有一定的參考價(jià)值,感興趣的可以了解一下

下面是一個(gè)完整的 MyBatis-Plus 多數(shù)據(jù)源支持的示例代碼,包括依賴配置、數(shù)據(jù)源配置、Mapper 和 Service 的定義。

1. 添加依賴

在 pom.xml 中添加 MyBatis-Plus 和多數(shù)據(jù)源相關(guān)的依賴:

<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 驅(qū)動(dòng)依賴 -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.23</version>
    </dependency>
</dependencies>

2. 配置數(shù)據(jù)源

在 application.yml 中配置多個(gè)數(shù)據(jù)源:

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. 數(shù)據(jù)源配置類

創(chuàng)建數(shù)據(jù)源配置類 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 掃描路徑

在不同的包路徑下創(chuàng)建 Mapper 接口,并使用 @MapperScan 注解指定使用的 SqlSessionFactory。

創(chuàng)建 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 {
}

創(chuàng)建 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. 創(chuàng)建實(shí)體類和 Mapper 接口

創(chuàng)建兩個(gè)實(shí)體類 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
}

創(chuàng)建 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. 創(chuàng)建 Service 類

創(chuàng)建 Service 類,分別處理不同數(shù)據(jù)源的業(yè)務(wù)邏輯:

// 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. 創(chuàng)建 Controller 類

最后,為每個(gè)數(shù)據(jù)源創(chuàng)建對(duì)應(yīng)的控制器以提供 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 項(xiàng)目中使用 MyBatis-Plus 實(shí)現(xiàn)多數(shù)據(jù)源支持了。這種配置方式可以方便地管理和使用不同的數(shù)據(jù)源,適應(yīng)不同的業(yè)務(wù)需求。

到此這篇關(guān)于MyBatis-Plus多數(shù)據(jù)源的示例代碼的文章就介紹到這了,更多相關(guān)MyBatis-Plus多數(shù)據(jù)源內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 如何使用hutool做本地緩存的工具類

    如何使用hutool做本地緩存的工具類

    這篇文章主要介紹了如何使用hutool做本地緩存的工具類,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-10-10
  • Java?ArrayList遍歷foreach與iterator時(shí)remove的區(qū)別

    Java?ArrayList遍歷foreach與iterator時(shí)remove的區(qū)別

    這篇文章主要介紹了Java?ArrayList遍歷foreach與iterator時(shí)remove的區(qū)別,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下
    2022-07-07
  • java解析Excel文件的方法實(shí)例詳解

    java解析Excel文件的方法實(shí)例詳解

    在日常工作中,我們常常會(huì)進(jìn)行文件讀寫操作,除去我們最常用的純文本文件讀寫,更多時(shí)候我們需要對(duì)Excel中的數(shù)據(jù)進(jìn)行讀取操作,下面這篇文章主要給大家介紹了關(guān)于java解析Excel文件的方法,需要的朋友可以參考下
    2022-06-06
  • 面試官:怎么做JDK8的垃圾收集器的調(diào)優(yōu)(面試常問)

    面試官:怎么做JDK8的垃圾收集器的調(diào)優(yōu)(面試常問)

    這篇文章主要介紹了面試官:怎么做JDK8的垃圾收集器的調(diào)優(yōu),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-08-08
  • JDK1.6集合框架bug 6260652解析

    JDK1.6集合框架bug 6260652解析

    這篇文章主要為大家解析了JDK1.6集合框架bug:c.toArray might (incorrectly) not return Object[] (see 6260652),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-10-10
  • mybatis 如何判斷l(xiāng)ist集合是否包含指定數(shù)據(jù)

    mybatis 如何判斷l(xiāng)ist集合是否包含指定數(shù)據(jù)

    這篇文章主要介紹了mybatis 判斷l(xiāng)ist集合是否包含指定數(shù)據(jù)的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • java編程之基于SpringBoot框架實(shí)現(xiàn)掃碼登錄

    java編程之基于SpringBoot框架實(shí)現(xiàn)掃碼登錄

    本文將介紹基于SpringBoot + Vue + Android實(shí)現(xiàn)的掃碼登錄demo的總體思路,文中附含詳細(xì)示例代碼,有需要的朋友可以借鑒參考下,希望能夠有所幫助
    2021-09-09
  • Java多線程之CAS算法實(shí)現(xiàn)線程安全

    Java多線程之CAS算法實(shí)現(xiàn)線程安全

    這篇文章主要介紹了java中如何通過CAS算法實(shí)現(xiàn)線程安全,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,下面小編和大家一起來學(xué)習(xí)一下吧
    2019-05-05
  • java實(shí)現(xiàn)京東登陸示例分享

    java實(shí)現(xiàn)京東登陸示例分享

    這篇文章主要介紹了使用java實(shí)現(xiàn)的京東商城登陸示例,需要的朋友可以參考下
    2014-03-03
  • 解決idea反編譯失敗無法查看jar包的源碼問題

    解決idea反編譯失敗無法查看jar包的源碼問題

    在IntelliJ IDEA中查看jar包源碼的兩種方法:下載反編譯軟件JD-GUI或安裝JavaBytecodeDecompiler插件
    2024-12-12

最新評(píng)論