mybatis mapper.xml中根據資料庫型別選擇對應SQL語句
阿新 • • 發佈:2019-01-05
1、spring-database.xml檔案中配置
<bean id="vendorProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="properties">
<props>
<prop key="DB2">db2</prop>
<prop key="Oracle">oracle</prop>
<prop key="MySQL">mysql</prop>
</props>
</property>
</bean>
<bean id="
<property name="properties" ref="vendorProperties"/>
</bean>
對於sessionFactory的配置,主要是標紅的語句一定要有,其它按照自己原有的配置走。
<!-- sessionFactory 將spring和mybatis整合 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="databaseIdProvider" ref="databaseIdProvider" />
<property name="configLocation" value="classpath:mybatis-config.xml" />
<property name="mapperLocations"
value="classpath*:/com/sunyard/cop/IF/mybatis/mapping/*.xml" />
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<!--使用下面的方式配置引數,一行配置一個,後面會有所有的引數介紹 -->
<value>
helperDialect=oracle
reasonable=true
supportMethodsArguments=true
params=count=countSql
autoRuntimeDialect=true
</value>
</property>
</bean>
</array>
</property>
</bean>
2、mapper.xml檔案中配置
<select id="selectByUserNo" databaseId="mysql"parameterType="java.lang.String" resultMap="UserResultMap">select * from SM_USERS_TB
</select>
<select id="selectByUserNo" parameterType="java.lang.String" resultMap="UserResultMap">select * from SM_USERS_TB
</select>
若寫上databaseId = "mysql",則在資料來源為mysql型別時,自動執行該SQL語句,若不寫databaseId ,且同時存在相同ID的SQL語句,則只要是非mysql資料庫的資料來源,都會呼叫該條SQL語句。