How to configure multiple data sources in MyBatis

Source: Internet
Author: User

<? Xml version = "1.0" encoding = "UTF-8"?>
<! DOCTYPE configuration
PUBLIC "-// mybatis.org//DTD Config 3.0 // EN"
Http://mybatis.org/dtd/mybatis-3-config.dtd>
<Configuration>
<Environments default = "DB1">
<Environment id = "DB1">
<TransactionManager type = "JDBC"/>
<DataSource type = "POOLED">
<Property name = "driver"
Value = "com. ibm. db2.jcc. DB2Driver"/>
<Property name = "url"
Value = "jdbc: db2: // localhost: 50000/DB1"/>
<Property name = "username" value = "root"/>
<Property name = "password" value = "root"/>
</DataSource>
</Environment>

<Environment id = "DB2">
<TransactionManager type = "JDBC"/>
<DataSource type = "POOLED">
<Property name = "driver"
Value = "com. ibm. db2.jcc. DB2Driver"/>
<Property name = "url"
Value = "jdbc: db2: // localhost: 50000/DB2"/>
<Property name = "username" value = "root"/>
<Property name = "password" value = "root"/>
</DataSource>
</Environment>

</Environments>
</Configuration>

 

Public class cescesqlsessionfactory {

Private Logger logger = LoggerFactory. getLogger (getClass ());

Private final static String MYBATIS_CONFIG = "mybatis-config-datasource.xml ";

Public final static String AMDB_ENVIRONMENT_ID = "DB1 ";

Public final static String AODB_ENVIRONMENT_ID = "DB2 ";

Public SqlSessionFactory getSqlSessionFactory (String environment ){
InputStream inputStream = null;
SqlSessionFactory sqlSessionFactory = null;
Try {
InputStream = Resources. getResourceAsStream (MYBATIS_CONFIG );
SqlSessionFactory = new SqlSessionFactoryBuilder (). build (inputStream, environment );
InputStream. close ();
Logger.info ("Get [" + environment + "] data source connection successful ");
} Catch (IOException e ){
Logger. error ("getting [" + environment + "] failed to connect to the data source, error message:" + e );
}
Return sqlSessionFactory;
}

}

 

Public void contextInitialized (ServletContextEvent event ){
Try {

// Annotation: instantiate the DataSourceSqlSessionFactory class
DataSourceSqlSessionFactory dataSourceSqlSessionFactory = new DataSourceSqlSessionFactory ();

// Note: save multiple data sources in the ServletContext container.
Event. getServletContext (). setAttribute (
DataSourceSqlSessionFactory. AMDB_ENVIRONMENT_ID,
DataSourceSqlSessionFactory. getSqlSessionFactory (DataSourceSqlSessionFactory. AMDB_ENVIRONMENT_ID ));

Event. getServletContext (). setAttribute (
DataSourceSqlSessionFactory. AODB_ENVIRONMENT_ID,
DataSourceSqlSessionFactory. getSqlSessionFactory (DataSourceSqlSessionFactory. AODB_ENVIRONMENT_ID ));

Logger.info ("multi-data source connection succeeded ");

} Catch (Exception e ){
Logger. error ("failed to get multi-data source connection, error message:" + e );
}
}

 

The above is the specific implementation of configuring multiple data sources for MyBatis. For everyone to learn.

Related Article

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.