first, the use of the scene
Different database SQL syntax differs, in order to ensure that it can be executed in a different database, we need to write SQL statements in the MyBatis mapper.xml file to determine the type of database currently connected, and then write SQL statements that adapt to different databases. Now we are going to work out how to differentiate between the types of connected databases in Mapper.xml. Second, the solution
MyBatis provides databaseidprovider for database type acquisition, which enables the determination of database types in mapper.xml files. Nonsense not much to say, directly on the configuration.
The spring configuration file is as follows:
<?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/con Text "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 Clas
Spath:/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 configuration-- <!--Scan class package, the spring note will be annotatedThe class of the solution automatically transforms the bean, while completing the bean injection--<context:component-scan base-package= "Com.shr.dao"/> <context:compone Nt-scan base-package= "Com.shr.service"/> <!--DAO Configuration--<context:property-placeholder Loca tion= "Classpath:config.properties"/> <bean id= "mysql" class= "Org.apache.commons.dbcp.BasicDataSource" destroy-method= "Close" > <property name= "driverclassname" value= "${mysql_driver}"/> <pro
Perty name= "url" value= "${mysql_url}"/> <property name= "username" value= "${mysql_username}"/> <property name= "Password" value= "${mysql_password}"/> </bean> <bean id= "Oracle" CLA ss= "Org.apache.commons.dbcp.BasicDataSource" destroy-method= "Close" > <property name= "driverclassname" V Alue= "${ora_driver}"/> <property name= "url" value= "${ora_url}"/> <property name= "us Ername "Value=" ${ora_username}"/> <property name=" password "value=" ${ora_password "/> </bean>
<!--Add a judgment on the database type-<bean id= "vendorproperties" class= "org.springframework.beans.factory.conf Ig. Propertiesfactorybean "> <property name=" Properties "> <props> < Prop key= "Oracle" >oracle</prop> <!--alias name is arbitrary, not the ID of the ORALCE data source bean configured above--<prop key= " MySQL ">mysql</prop> <!--alias, name can be arbitrary,not the id--> of the MySQL data source configured above </props> </property> </bean> <bean id= "Databaseidprovider" class= "Org.apache.ibatis.mapping.VendorDatabaseIdProvider" > <property na Me= "Properties" ref= "Vendorproperties"/> </bean>
<!--//Add a judgment on the database type--
<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 "/>
<!--provide the MyBatis framework with the ability to query the database type, which must not be less----<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> <!--configuration transaction Manager-<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>
Description: Focus on the configuration with bold markings, other configurations are not necessary, because it is not directly related to the purpose of getting the database type in the Mapper.mxl file.
The Mapper.xml configuration is as follows:
<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>
databaseid=" MySQL> <!--aliases in the spring configuration file
<prop key= "Oracle" >oracle</prop> <!--alias name can be arbitrary, not the above configuration of the MySQL data source Bean ID--
--Select user_id, user_name, User_password, User_privilege, User_alias, Create_date, invalid_date from User_define o Rder by user_id ASC </select> <select id= "Selectuserinfo" resultmap= "Userresultmap" databaseid= "Oracle" > <!--aliases that correspond to the spring configuration file
<prop key= "Oracle" >oracle</prop> <!--alias name is arbitrary, not the ID of the Oracle data source bean configured above
-
Select user_id, user_name, User_password, User_privilege, User_alias, Create_date, invalid_date from User_define order by user_id desc </select>
Note: Focus on the Mapper.xml file with bold identification configuration, mainly through the databaseid= "xxx" to achieve the database type of judgment, and then for different database types to write different SQL. The above configuration in the spring configuration file is to use databaseid= "xxx" in the mapper.xml, if not in the spring configuration file to do the above configuration, will be error, the error message is as follows:
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)