How to configure multiple database connections in MyBatis
Introduction:
In modern application development, it is often necessary to connect multiple databases to meet different business needs . As a popular persistence layer framework, MyBatis can easily interact with multiple databases. This article will introduce how to configure multiple database connections in MyBatis and provide specific code examples.
Step 1: Introduce relevant dependencies
In the pom.xml file of the project, add the dependencies of MyBatis and database driver. For example, if you use Oracle database, you can add the following dependencies:
<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.5</version> </dependency> <dependency> <groupId>com.oracle.database.jdbc</groupId> <artifactId>ojdbc8</artifactId> <version>19.8.0.0</version> </dependency>
Step 2: Configure the data source
In MyBatis, we can configure the data source in a variety of ways, such as using JNDI, Spring, or Configure the data source directly in the MyBatis configuration file.
The following is a common way to configure multiple database connections in MyBatis configuration:
<!-- 数据源一 --> <dataSource type="com.zaxxer.hikari.HikariDataSource"> <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/> <property name="jdbcUrl" value="jdbc:oracle:thin:@localhost:1521:ORCL"/> <property name="username" value="user1"/> <property name="password" value="password1"/> </dataSource> <!-- 数据源二 --> <dataSource type="com.zaxxer.hikari.HikariDataSource"> <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/db2?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC"/> <property name="username" value="user2"/> <property name="password" value="password2"/> </dataSource>
In the above example, we configured two data sources, one to connect to the Oracle database and one to connect to MySQL database.
Step 3: Configure MyBatis session factory
In the MyBatis configuration file, we need to configure the session factory (SqlSessionFactory) to manage database connections and sessions. Multiple database connections can be achieved by using multiple data sources and multiple database connection factories.
The following is a simple example configuration:
<!-- 数据源一的数据库连接工厂 --> <bean id="sqlSessionFactory1" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource1"/> </bean> <!-- 数据源二的数据库连接工厂 --> <bean id="sqlSessionFactory2" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource2"/> </bean> <!-- MyBatis会话工厂 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="mapperLocations" value="classpath*:mapper/*.xml"/> <property name="configLocation" value="classpath:mybatis-config.xml"/> </bean>
In the above example, we configured two database connection factories (sqlSessionFactory1 and sqlSessionFactory2) respectively, and in the final MyBatis session factory (sqlSessionFactory ).
Step 4: Use multiple database connections in the Mapper interface
When using multiple database connections, we need to specify which database connection to use in the Mapper interface.
The following is the definition of an example Mapper interface:
public interface UserMapper { // 数据源一的查询 @Mapper @DataSource("dataSource1") List<User> getUsersFromDataSource1(); // 数据源二的查询 @Mapper @DataSource("dataSource2") List<User> getUsersFromDataSource2(); }
In the above example, we use the @DataSource annotation to specify a specific data source, for example, @DataSource("dataSource1") indicates the use of data Source one to query.
Conclusion:
Through the above steps, we can easily configure multiple database connections in MyBatis. In actual applications, multiple data sources and database connection factories can be configured according to specific needs to meet different business needs.
In actual development, the configuration of multiple database connections may be more complex, for example involving transaction management, dynamic data source switching, etc. It can be further configured and adjusted according to the specific situation.
The above is the detailed content of How to configure MyBatis to connect to multiple databases. For more information, please follow other related articles on the PHP Chinese website!