<SelectID= "Selectperson"ParameterType= "int"Parametermap= "Deprecated"Resulttype= "HashMap"Resultmap= "Personresultmap"Flushcache= "false"UseCache= "true"Timeout= "10000"fetchsize= " the"StatementType= "PREPARED"ResultsetType= "Forward_only"databaseId= "MySQL">
The following explanation has a DatabaseId property: If Databaseidprovider,mybatis is configured, all statements without databaseId or matching the current databaseId are loaded; The non-band will be ignored. New, modified, and deleted have this property
First, in the Configuration.xml configuration
<type= "Db_vendor"><name= " MySQL " value=" MySQL "/> <name = "Oracle" value = "Oracle" /> </ Databaseidprovider >
Second, new mapper
Public Interface databaseidprovidermapper { String selecttime ();}
Third, new Databaseidprovider.xml
<Mappernamespace= "Com.yihaomen.mybatis.dao.DatabaseIdProviderMapper"> <SelectID= "Selecttime"Resulttype= "String" databaseId= "MySQL" >SELECT now () from dual</Select> <SelectID= "Selecttime"Resulttype= "String" databaseId= "Oracle" >SELECT ' Oralce ' | | To_char (sysdate, ' Yyyy-mm-dd hh24:mi:ss ') from dual</Select></Mapper>
Iv. Adding a mapper to the Configuration.xml
< mappers > < resource= "Com/yihaomen/mybatis/model/databaseidprovider.xml"/></ mappers>
V.. jdbc.properties file
jdbc.driver=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://127.0.0.1:3306/mybatis-learn? Characterencoding=utf8jdbc.username=rootjdbc.password=tiger#jdbc.driver= Oracle.jdbc.driver.oracledriver#jdbc.url=jdbc:oracle:thin: @localhost: 1521: Mybatis#jdbc.username= Mybatis#jdbc.password=mybatismaxactive= 50
VI. Testing
ImportCom.yihaomen.mybatis.dao.DatabaseIdProviderMapper;Importorg.apache.ibatis.session.SqlSession;Importorg.apache.ibatis.session.SqlSessionFactory;Importuser. Basetest; Public classDatabaseidprovidertestextendsbasetest{ Public Static voidMain (string[] args) {Sqlsessionfactory factory=getsession (); Sqlsession Session=factory.opensession (); Databaseidprovidermapper Mapper= Session.getmapper (databaseidprovidermapper.class); System.out.println (Mapper.selecttime ()); }}
- Result Description
If Oracle is currently enabled, execute databaseid= "Oracle" statement if MySQL value executes databaseid= "MySQL" statement
Https://gitee.com/huayicompany/springmvc-mybatis Reference Documents:
[1] Yang Kaizhen, "MyBatis technical principles and actual combat", electronic industry Press, 2016.09
[2] Match box Zhang Blog, http://blog.csdn.net/likewindy/article/details/51396576
Mybatis-databaseidprovider Multi-database support