一、使用场景
不同的数据库的Sql语法有所不同,为了保证在不同的数据库中都能执行,我们需要在MyBatis的Mapper.xml文件中编写sql语句时对当前连接的数据库的类型进行判断,然后编写适应不同数据库的sql语句。现在我们就是要解决如何在Mapper.xml中区分连接的数据库的类型。
二、解决方法
mybatis提供了databaseIdProvider实现了对数据库类型的获取,从而实现了在Mapper.xml文件中对数据库类型的判断。废话不多说,直接上配置。
spring配置文件如下:
<?xml version="1.0" encoding="UTF-8" ?> beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans classpath:/org/springframework/beans/factory/xml/spring-beans-3.0.xsd http://www.springframework.org/schema/aop classpath:/org/springframework/aop/config/spring-aop-3.0.xsd http://www.springframework.org/schema/context classpath:/org/springframework/context/config/spring-context-3.0.xsd http://www.springframework.org/schema/tx classpath:/org/springframework/transaction/config/spring-tx-3.0.xsd"> <!-- IoC配置 --> <!-- 扫描类包,将标注Spring注解的类自动转化Bean,同时完成Bean的注入 --> <context:component-scan base-package="com.shr.dao" /> <context:component-scan base-package="com.shr.service" /> <!-- DAO配置 --> <context:property-placeholder location="classpath:config.properties"/> <bean id="mysql" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${mysql_driver}"/> <property name="url" value="${mysql_url}"/> <property name="username" value="${mysql_username}"/> <property name="password" value="${mysql_password}"/> </bean> <bean id="oracle" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${ora_driver}"/> <property name="url" value="${ora_url}"/> <property name="username" value="${ora_username}"/> <property name="password" value="${ora_password}"/> </bean> <!-- 添加对数据库类型的判断 --> <bean id="vendorProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> <property name="properties"> <props> <prop key="Oracle">oracle</prop> <!-- 取别名,名称可任意,并不是上面配置的oralce数据源bean的id --> <prop key="MySQL">mysql</prop> <!-- 取别名,名称可任意,并不是上面配置的mysql数据源的bean的id--> </props> </property> </bean> <bean id="databaseIdProvider" class="org.apache.ibatis.mapping.VendorDatabaseIdProvider"> <property name="properties" ref="vendorProperties" /> </bean>
<!-- //添加对数据库类型的判断 -->
<bean name="myBatisSQLInterceptor" class="com.shr.dao.MyBatisSQLInterceptor"></bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="${dataSource}" /> <property name="typeAliasesPackage" value="com.shr.dao.pojo,com.shr.dao.model" /> <!-- 给MyBatis框架提供查询数据库类型的功能,这个千万不能少 --> <property name="databaseIdProvider" ref="databaseIdProvider" /> <property name="mapperLocations"> <list> <value>classpath:com/shr/dao/resources/mappers/*_mapper.xml</value> </list> </property> <!-- <property name="configLocation" value="/WEB-INF/mybatis-config.xml"/> --> <property name="typeHandlersPackage" value="com.shr.dao" /> <property name="plugins"> <list> <ref bean="myBatisSQLInterceptor"/> </list> </property> </bean> <!-- 配置事务管理器 --> <tx:annotation-driven/> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="${dataSource}"/> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.shr.dao.mapper"/> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> <!-- <property name="markerInterface" value="com.shr.dao.mapper.ITemplateMapper"/> --> </bean> /beans> 说明:重点看用加粗标记的配置,其他的配置不用管,因为跟此次要在Mapper.mxl文件中获取数据库类型的目的没有直接关系。
Mapper.xml配置如下:
<resultMap id="userResultMap" type="com.shr.dao.model.userManage.UserInfo"> <result property="user_id" column="user_id"/> <result property="user_name" column="user_name"/> <result property="user_password" column="user_password"/> <result property="user_privilege" column="user_privilege"/> <result property="user_alias" column="user_alias"/> <result property="create_date" column="create_date" javaType="java.util.Date" jdbcType="TIMESTAMP"/> <result property="invalid_date" column="invalid_date" javaType="java.util.Date" jdbcType="TIMESTAMP"/> </resultMap> <select id="selectUserInfo" resultMap="userResultMap" databaseId="mysql"> <!--对应在spring配置文件中的别名
--> select user_id, user_name, user_password, user_privilege, user_alias, create_date, invalid_date from user_define order by user_id asc </select> <select id="selectUserInfo" resultMap="userResultMap"databaseId="oracle"> <!--对应在spring配置文件中的别名<prop key="Oracle">oracle</prop> <!-- 取别名,名称可任意,并不是上面配置的mysql数据源bean的id -->
--> select user_id, user_name, user_password, user_privilege, user_alias, create_date, invalid_date from user_define order by user_id desc </select><prop key="Oracle">oracle</prop> <!-- 取别名,名称可任意,并不是上面配置的oracle数据源bean的id -->
说明:重点关注Mapper.xml文件用加粗标识的配置,主要是通过databaseId="xxx" 实现对数据库类型的判断,然后针对不同的数据库类型编写不同的sql即可。上面在spring配置文件中做的那些配置就是为了在Mapper.xml中使用databaseId="xxx",如果没有在spring配置文件中做上面的配置的话,就会报错,报错信息如下:
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.shr.dao.mapper.IuserManageMapper.selectUserInfo at org.apache.ibatis.binding.MapperMethod$SqlCommand.<init>(MapperMethod.java:189) at org.apache.ibatis.binding.MapperMethod.<init>(MapperMethod.java:43) at org.apache.ibatis.binding.MapperProxy.cachedMapperMethod(MapperProxy.java:58) at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:51) at com.sun.proxy.$Proxy29.selectUserInfo(Unknown Source) at com.shr.service.userManage.UserManageService.getUserListInfo(UserManageService.java:98)