mysql和oracle兼容


Mybatis中提供了兩個常用的內置參數: _parameter和_databaseId
_parameter:代表整個參數
單個參數:_parameter就是這個參數
多個參數:參數會被封裝為一個map:_parameter就是代表這個map
_databaseId:如果配置了databaseIdProvider標簽
_databaseId 就是代表當前數據庫的別名oracle或者mysql

如果你的項目是spring.springmvc,mybatis 可以在配置文件中加上配置:
//數據源
<bean id="dataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource">
<property name="driver" value="${jdbc.dbDriver}" />
<property name="driverUrl" value="${jdbc.dburl}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="alias" value="Pool_dbname" />
<property name="prototypeCount" value="0" />
<property name="maximumConnectionCount" value="50" />
<property name="minimumConnectionCount" value="2" />
<property name="simultaneousBuildThrottle" value="50" />
<property name="houseKeepingTestSql" value="select CURRENT_DATE from dual" />
</bean>
//mybatis 文件配置
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:/你自己的xml文件位置" />
<property name="databaseIdProvider" ref="databaseIdProvider" />
</bean>
<!-- databaseIdProvider -->
<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>

這樣配置好以后,你就可以使用(_databaseId)你所配置的這個參數了;比如下面這個例子:
<select id="" parameterType="string" resultType="int">
select count(1) from table where
<if test="_databaseId == 'mysql'">
clum = ''
</if>
<if test="_databaseId == 'oracle'">
clum like ''
</if>
</select>

對於 _parameter 的用法可以看下面的例子:
_parameter相當於傳入的參數employee,判斷employee是否為空,若不為空則執行where條件
<select id="" resultType="com.hand.mybatis.bean.Employee">
SELECT * FROM emp
<if test="_parameter!=null">
where ename=#{_parameter.eName}
</if>
</select>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM