Mybatis-databaseidprovider Multi-database support

Source: Internet
Author: User

<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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.