How to configure multiple data sources for ibatis

Source: Internet
Author: User
How to configure multiple data sources for ibatis

Document category: Database

1. applicationcontext. xml Java code
  1. <! -- Define the data source datasource -->
  2. <Bean id = "datasource"
  3. Class = "org. Apache. commons. DBCP. basicdatasource">
  4. <Property name = "driverclassname"
  5. Value = "ianywhere. ml. jdbcodbc. jdbc3.idriver">
  6. </Property>
  7. <Property name = "url"
  8. Value = "JDBC: ODBC: Driver = SQL Anywhere 11; Eng = ZYX; links = TCPIP {Host = 10.144.99.46; Port = 2998};">
  9. </Property>
  10. <Property name = "username" value = "ZYX"> </property>
  11. <Property name = "password" value = "ZYX"> </property>
  12. <Property name = "initialsize" value = "1"> </property>
  13. <Property name = "maxactive" value = "500"> </property>
  14. <Property name = "maxidle" value = "5"> </property>
  15. <Property name = "minidle" value = "1"> </property>
  16. </Bean>
  17. <! -- Define the data source Oracle -->
  18. <Bean id = "ceceoracle" class = "org. Apache. commons. DBCP. basicdatasource">
  19. <Property name = "driverclassname" value = "oracle. JDBC. Driver. oracledriver"> </property>
  20. <Property name = "url" value = "JDBC: oracle: thin: @ 10.144.99.114: 1521/sanexcfg"> </property>
  21. <Property name = "username" value = "sanex"> </property>
  22. <Property name = "password" value = "sanex"> </property>
  23. <Property name = "initialsize" value = "1"> </property>
  24. <Property name = "maxactive" value = "500"> </property>
  25. <Property name = "maxidle" value = "5"> </property>
  26. <Property name = "minidle" value = "1"> </property>
  27. </Bean>
  28. <! -- Define the data source sqlserver datasource -->
  29. <Bean id = "datasourcesqlserver"
  30. Class = "org. Apache. commons. DBCP. basicdatasource">
  31. <Property name = "driverclassname"
  32. Value = "com. Microsoft. JDBC. sqlserver. sqlserverdriver">
  33. </Property>
  34. <Property name = "url"
  35. Value = "JDBC: Microsoft: sqlserver: // 10.71.158.50: 1433; databasename = mon;">
  36. </Property>
  37. <Property name = "username" value = "sa"> </property>
  38. <Property name = "password" value = "sa"> </property>
  39. <Property name = "initialsize" value = "1"> </property>
  40. <Property name = "maxactive" value = "500"> </property>
  41. <Property name = "maxidle" value = "5"> </property>
  42. <Property name = "minidle" value = "1"> </property>
  43. </Bean>
  44. <! -- Configure sqlmapclient of ibatis -->
  45. <Bean id = "sqlmapclient"
  46. Class = "org. springframework. Orm. ibatis. sqlmapclientfactorybean">
  47. <Property name = "configlocation"
  48. Value = "/WEB-INF/sqlmapconfig. xml"/>
  49. <Property name = "datasource" ref = "datasource"/>
  50. </Bean>
  51. <Bean id = "sqlmapclientorac"
  52. Class = "org. springframework. Orm. ibatis. sqlmapclientfactorybean">
  53. <Property name = "configlocation"
  54. Value = "/WEB-INF/sqlmapconfig. xml"/>
  55. <Property name = "datasource" ref = "ceceoracle"/>
  56. </Bean>
<! -- Define the data source datasource --> <bean id = "datasource" class = "org. apache. commons. DBCP. basicdatasource "> <property name =" driverclassname "value =" ianywhere. ML. jdbcodbc. jdbc3.idriver "> </property> <property name =" url "value =" JDBC: ODBC: Driver = SQL Anywhere 11; Eng = ZYX; links = TCPIP {Host = 10.144.99.46; port = 2998 }; "> </property> <property name =" username "value =" ZYX "> </property> <property name =" password "value =" ZYX "> </property> <PR Operty name = "initialsize" value = "1" type = "codeph" text = "codeph"> </property> <property name = "maxactive" value = "500" type = "codeph" text = "/codeph"> <property name = "maxidle" value = "5"> </property> <property name = "minidle" value = "1"> </property> </bean> <! -- Define the data source Oracle --> <bean id = "ceceoracle" class = "org. apache. commons. DBCP. basicdatasource "> <property name =" driverclassname "value =" oracle. JDBC. driver. oracledriver "> </property> <property name =" url "value =" JDBC: oracle: thin: @ 10.144.99.114: 1521/sanexcfg "> </property> <property name =" username "value =" sanex "> </property> <property name =" password "value =" sanex "> </property> <property name = "initialsize" value =" 1 "> </property> <property name =" maxactive "value =" 500 "> </property> <property name =" maxidle "value =" 5 "> </Property> <property name = "minidle" value = "1"> </property> </bean> <! -- Define the data source sqlserver datasource --> <bean id = "datasourcesqlserver" class = "org. apache. commons. DBCP. basicdatasource "> <property name =" driverclassname "value =" com. microsoft. JDBC. sqlserver. sqlserverdriver "> </property> <property name =" url "value =" JDBC: Microsoft: sqlserver: // 10.71.158.50: 1433; databasename = mon; "> </property> <property name =" username "value =" sa "> </property> <property name =" password "value =" sa "> </proper Ty> <property name = "initialsize" value = "1"> </property> <property name = "maxactive" value = "500"> </property> <property name = "maxidle" value = "5"> </property> <property name = "minidle" value = "1"> </property> </bean> <! -- Configure sqlmapclient of ibatis --> <bean id = "sqlmapclient" class = "org. springframework. orm. ibatis. sqlmapclientfactorybean "> <property name =" configlocation "value ="/WEB-INF/sqlmapconfig. XML "/> <property name =" datasource "ref =" datasource "/> </bean> <bean id =" sqlmapclientorac "class =" org. springframework. orm. ibatis. sqlmapclientfactorybean "> <property name =" configlocation "value ="/WEB-INF/sqlmapconfig. XML "/> <property name =" datasource "ref =" ceceoracle "/> </bean>

2. Nothing is set in the sqlmapconfig. xml file. Only the custom query XML file is placed.

3. spring annotations are used in the Code. The name in the resource must correspond to applicationcontext. the bean ID in XML, which can find the corresponding bean and reference the Java code of the data source you want to operate on.

  1. @ Resource (name = "sqlmapclient ")
  2. Private sqlmapclient;
  3. @ Resource (name = "sqlmapclientorac ")
  4. Private sqlmapclient sqlmapclientorac;
  5. Public void executesp (MAP map) throws exception {
  6. Sqlmapclient. queryforlist ("userpercept. useranalyse. addusefulprocedure", MAP );
  7. }
  8. Public list <cell> getcelllist () throws exception {
  9. Return sqlmapclientorac. queryforlist ("userpercept. useranalyse. getcelllist ");
  10. }
  11. Public placecell getcell (long ID) throws exception {
  12. Placecell cell = (placecell) sqlmapclient
  13. . Queryforobject ("userpercept. useranalyse. selectcell", ID );
  14. Return cell;
  15. }

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.