Mybatis 多数据库支持实践(MySQL+Oracle)

根据这篇博客去尝试 https://blog.csdn.net/qq_35981283/article/details/79503571

但是发现始终读取不到 databaseId

经过调试找到了原因。

首先从 mybatis 的自动配置说起

@Configuration
@ConditionalOnClass({SqlSessionFactory.class, SqlSessionFactoryBean.class})
@ConditionalOnBean({DataSource.class})
@EnableConfigurationProperties({MybatisProperties.class})
@AutoConfigureAfter({DataSourceAutoConfiguration.class})
public class MybatisAutoConfiguration {

@ConditionalOnClass是Springboot实现自动配置的重要支撑之一。其用途是判断当前classpath下是否存在指定类,若是则将当前的配置装载入spring容器。

ConditionalOnBean则是查看是否有这个 bean,也就是这个配置加载的条件是有 SqlSessionFactory 这个类以及 DataSource 的 bean,这也就是为啥需要@AutoConfigureAfter 保证 DataSourceAutoConfiguration 这个类先加载了。

然后就是看 MybatisAutoConfiguration 的实现,里面有一个方法是初始化 sqlSessionFactory

@Bean
    @ConditionalOnMissingBean
    public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
        SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
        factory.setDataSource(dataSource);
        factory.setVfs(SpringBootVFS.class);
        if (StringUtils.hasText(this.properties.getConfigLocation())) {
            factory.setConfigLocation(this.resourceLoader.getResource(this.properties.getConfigLocation()));
        }

        factory.setConfiguration(this.properties.getConfiguration());
        if (!ObjectUtils.isEmpty(this.interceptors)) {
            factory.setPlugins(this.interceptors);
        }

        if (this.databaseIdProvider != null) {
            factory.setDatabaseIdProvider(this.databaseIdProvider);
        }

        if (StringUtils.hasLength(this.properties.getTypeAliasesPackage())) {
            factory.setTypeAliasesPackage(this.properties.getTypeAliasesPackage());
        }

        if (StringUtils.hasLength(this.properties.getTypeHandlersPackage())) {
            factory.setTypeHandlersPackage(this.properties.getTypeHandlersPackage());
        }

        if (!ObjectUtils.isEmpty(this.properties.resolveMapperLocations())) {
            factory.setMapperLocations(this.properties.resolveMapperLocations());
        }

        return factory.getObject();
    }

在这个方法里判断如果已经有 databaseIdProvider 这个 bean的话,会给这个 SQLSessionFactory 设置一个dataBaseIdProvider。而我恰恰在 spring boot 启动的时候加载了这么一个 bean

 @Bean
    public DatabaseIdProvider getDatabaseIdProvider(){
        DatabaseIdProvider databaseIdProvider = new VendorDatabaseIdProvider();
        Properties properties = new Properties();
        properties.setProperty("Oracle","oracle");
        properties.setProperty("MySQL","mysql");
        properties.setProperty("DB2","db2");
        properties.setProperty("Derby","derby");
        properties.setProperty("H2","h2");
        properties.setProperty("HSQL","hsql");
        properties.setProperty("Informix","informix");
        properties.setProperty("MS-SQL","ms-sql");
        properties.setProperty("PostgreSQL","postgresql");
        properties.setProperty("Sybase","sybase");
        properties.setProperty("Hana","hana");
        databaseIdProvider.setProperties(properties);
        return databaseIdProvider;
    }

一切看上去合情合理,那是为啥呢?打个断点在sqlSessionFactory方法中,发现果然没到这个方法中。那就说明这个bean 已经有了,搜索代码,果然代码的其他地方也加载了sqlSessionFactory,而那个方法里没有去设置 databasIdProvider,果断加入,问题解决。

 

踩坑记录:

1.` oracle 不能用

2.时间格式化

3.group by

4.default '' 和null

5.group_concat

6.insert

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将 MyBatis-Plus 从 MySQL 切换到 Oracle 数据库,需要进行以下步骤: 1. 修改数据库驱动和连接信息 在 pom.xml 文件中将 MySQL数据库驱动和连接信息替换为 Oracle 的,例如: ``` <dependency> <groupId>com.oracle</groupId> <artifactId>ojdbc8</artifactId> <version>19.3.0.0</version> </dependency> ``` 同时,在 application.yml 或 application.properties 文件中修改数据库连接信息,例如: ``` spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver spring.datasource.url=jdbc:oracle:thin:@localhost:1521:orcl spring.datasource.username=your_username spring.datasource.password=your_password ``` 2. 修改 MyBatis-Plus 的配置 在 MyBatis-Plus 的配置文件中,需要将 MySQL 的方言替换为 Oracle 的,例如: ``` mybatis-plus: mapper-locations: classpath*:mapper/**/*.xml global-config: db-config: id-type: ID_WORKER table-prefix: mp_ field-strategy: not_empty logic-delete-value: 1 logic-not-delete-value: 0 sql-injector: com.baomidou.mybatisplus.core.injector.LogicSqlInjector configuration: map-underscore-to-camel-case: true log-impl: org.apache.ibatis.logging.stdout.StdOutImpl dialect-type: oracle ``` 3. 修改 SQL 语句 在 XML 映射文件中,需要将 MySQL 的 SQL 语句替换为 Oracle 的,例如: ``` <select id="getUserById" resultType="User"> SELECT * FROM user WHERE id = #{id} </select> ``` 修改为: ``` <select id="getUserById" resultType="User"> SELECT * FROM user WHERE id = #{id} </select> ``` 需要注意的是,Oracle 数据库中的 SQL 语法与 MySQL 有所不同,需要进行相应的修改。 完成以上步骤后,即可将 MyBatis-Plus 从 MySQL 切换到 Oracle 数据库

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值