在 Spring Boot 项目中使用 MyBatis 访问 Oracle 多数据库,需要进行以下步骤:

  1. pom.xml 文件中添加 MyBatis 和 Oracle 的依赖:
<dependencies>
    <!-- MyBatis -->
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>1.3.2</version>
    </dependency>
    <!-- Oracle -->
    <dependency>
        <groupId>com.oracle</groupId>
        <artifactId>ojdbc6</artifactId>
        <version>11.2.0.3</version>
    </dependency>
</dependencies>
  1. application.properties 文件中配置数据源和 MyBatis:
# 数据源1
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@localhost:1521:database1
spring.datasource.username=your_username
spring.datasource.password=your_password

# 数据源2
spring.datasource.db2.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.db2.url=jdbc:oracle:thin:@localhost:1521:database2
spring.datasource.db2.username=your_username
spring.datasource.db2.password=your_password

# MyBatis配置
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=com.example.demo.entity
  1. 创建两个数据源的配置类:
@Configuration
@MapperScan(basePackages = "com.example.demo.mapper1", sqlSessionTemplateRef = "db1SqlSessionTemplate")
public class DataSource1Config {
    @Bean(name = "db1DataSource")
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource db1DataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "db1SqlSessionFactory")
    public SqlSessionFactory db1SqlSessionFactory(@Qualifier("db1DataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        return bean.getObject();
    }

    @Bean(name = "db1TransactionManager")
    public DataSourceTransactionManager db1TransactionManager(@Qualifier("db1DataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "db1SqlSessionTemplate")
    public SqlSessionTemplate db1SqlSessionTemplate(@Qualifier("db1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}

@Configuration
@MapperScan(basePackages = "com.example.demo.mapper2", sqlSessionTemplateRef = "db2SqlSessionTemplate")
public class DataSource2Config {
    @Bean(name = "db2DataSource")
    @ConfigurationProperties(prefix = "spring.datasource.db2")
    public DataSource db2DataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "db2SqlSessionFactory")
    public SqlSessionFactory db2SqlSessionFactory(@Qualifier("db2DataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        return bean.getObject();
    }

    @Bean(name = "db2TransactionManager")
    public DataSourceTransactionManager db2TransactionManager(@Qualifier("db2DataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "db2SqlSessionTemplate")
    public SqlSessionTemplate db2SqlSessionTemplate(@Qualifier("db2SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}
  1. 在 mapper 包中创建两个 Mapper 接口并编写对应的 SQL 语句:
@Repository
public interface UserMapper1 {
    @Select("SELECT * FROM USER")
    List<User> getAllUsers();
}

@Repository
public interface UserMapper2 {
    @Select("SELECT * FROM USER")
    List<User> getAllUsers();
}
  1. 在 Service 中注入两个 Mapper 接口,并分别调用方法:
@Service
public class UserService {
    @Autowired
    private UserMapper1 userMapper1;

    @Autowired
    private UserMapper2 userMapper2;

    public List<User> getAllUsersFromDB1() {
        return userMapper1.getAllUsers();
    }

    public List<User> getAllUsersFromDB2() {
        return userMapper2.getAllUsers();
    }
}

这样就可以在 Spring Boot 项目中使用 MyBatis 访问 Oracle 多数据库了。在 Service 中根据需要调用不同的 Mapper 即可。

注意:

  • 以上代码示例仅供参考,具体实现可能因项目需求而有所不同。
  • 请根据您的实际情况配置数据源和 MyBatis,并编写相应的 Mapper 接口和 SQL 语句。
  • 为了安全起见,请不要将数据库用户名和密码直接写在代码或配置文件中,建议使用环境变量或配置中心存储敏感信息。
  • 建议使用统一的命名规范,例如使用 db1db2 等前缀来区分不同的数据源和组件。
  • 使用 @Qualifier 注解可以明确指定需要注入的 Bean,避免出现冲突。
  • 使用 @MapperScan 注解可以自动扫描 Mapper 接口,简化配置。
  • 使用 SqlSessionTemplate 可以方便地执行 SQL 语句和访问数据库。
Spring Boot 项目中使用 MyBatis 访问 Oracle 多数据库

原文地址: https://www.cveoy.top/t/topic/ovkT 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录