mybatis中databaseIdProvider支持多厂商数据库配置详解

databaseIdProvider实现机制

MyBatis 根据加载的数据源获取是什么厂商的数据库,并根据mapper.xml中配置的databaseId找到对应厂商的sql,不同厂商的数据库执行语句的语法以及函数存在差异。

如何配置

配置databaseIdProvider (在注入dataSource之前配置)

<bean id="vendorProperties"  class="org.springframework.beans.factory.config.PropertiesFactoryBean"> 
     <property name="properties"> 
         <props> 
             <prop key="Oracle">oracle</prop> 
            <prop key="MySQL">mysql</prop> 
         </props> 
     </property> 
      </bean> 
 
 <bean id="databaseIdProvider" class="org.apache.ibatis.mapping.VendorDatabaseIdProvider"> 
      <property name="properties" ref="vendorProperties" /> 
 </bean> 
 
 <!-- myBatis文件 -->
 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  <property name="dataSource" ref="dataSource" />
  <property name="databaseIdProvider" ref="databaseIdProvider" />
  <!-- 自动扫描entity目录, 省掉Configuration.xml里的手工配置  -->
  <property name="configLocation" value="classpath:META-INF/spring/beans/mybatis-config.xml"></property>
  <property name="mapperLocations" value="classpath*:com/ais/brm/web/mapper/**/*.xml" />  
 </bean>

vendorProperties   配置的是项目需要识别的厂商的数据库(上图中配置了两种 mysql 和 oracle)

databaseIdProvider  是为了让VendorDatabaseIdProvider去匹配当前的数据源

最后需要把databaseIdProvider 放置到 databaseIdProvider

配置到此结束,我们来看看我们的实现测试

<select id="queryTables" resultMap="tablesManageResult" parameterType="com.houses365.model.Tables" databaseId="mysql">
        select table_name from information_schema.tables where table_schema=#{db} and table_type='base table'
        <include refid="paginationSql"/>
 </select>
 
 <select id="queryTables" resultMap="tablesManageResult" parameterType="com.houses365.model.Tables" databaseId="oracle">
        select  table_name from user_tables
        <include refid="paginationSql"/>
 </select>

经过测试数据源:

mysql数据库执行上面的sql

oracle数据库执行下面的sql







  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

开发小菜-逸仙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值