mybatis多数据源在项目开发中经常用到,有时候可能是为了主从读写分离,有时候可能是需要调用不同的业务库数据,因此需要使用到Mybatis多数据源配置
1、步骤一:配置多数据源sit1、dev2
<environments default="development">
<environment id="sit1">
<transactionManager type="JDBC" />
<!-- 配置数据库连接信息 -->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/test" />
<property name="username" value="root" />
<property name="password" value="123" />
</dataSource>
</environment>
<environment id="dev2">
<transactionManager type="JDBC" />
<!-- 配置数据库连接信息 -->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/mysql" />
<property name="username" value="root" />
<property name="password" value="123" />
</dataSource>
</environment>
</environments>
<!-- mapping 文件路径配置 -->
<mappers>
<mapper resource="user.xml" />
</mappers>
2、部署二:准备不同的数据库表,test -->tb_test , Mysql -->db
C:\Documents and Settings\Administrator>mysql -uroot -p
Enter password: ***
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.0.51b-community-nt-log MySQL Community Edition (GPL)
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| phpmyadmin |
| test |
+--------------------+
4 rows in set (0.03 sec)
mysql> use test;
Database changed
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| tb_file |
| tb_test |
+----------------+
2 rows in set (0.02 sec)
mysql> use mysql;
Database changed
mysql> show tables;
+---------------------------+
| Tables_in_mysql |
+---------------------------+
| columns_priv |
| db |
| func |
| help_category |
| help_keyword |
| help_relation |
| help_topic |
| host |
| proc |
| procs_priv |
| tables_priv |
| time_zone |
| time_zone_leap_second |
| time_zone_name |
| time_zone_transition |
| time_zone_transition_type |
| user |
+---------------------------+
17 rows in set (0.00 sec)
3、步骤三:编写SQL配置文件
<select id="getDb1" resultType="java.lang.Integer">
select count(*) from tb_test
</select>
<select id="getDb2" resultType="java.lang.Integer">
select count(*) from db
</select>
4、步骤四:编写java测试程序
package mybatistest.mybatisdemo;
import java.io.InputStream;
import mybatistest.mybatisdemo.vo.User;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class MultiDataSource {
/**
* Mybatis多数据源的配置实验
* <properties>
* <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
* </properties>
* <dependencies>
* <dependency>
* <groupId>org.mybatis</groupId>
* <artifactId>mybatis</artifactId>
* <version>3.3.0</version>
* </dependency>
* <dependency>
* <groupId>mysql</groupId>
* <artifactId>mysql-connector-java</artifactId>
* <version>5.1.18</version>
* </dependency>
* </dependencies>
*
* @param args
*/
public static void main(String[] args) {
// 创建能执行映射文件中sql的sqlSession
SqlSession session = getSessionFactory("sit1").openSession();
String namespace = "cn.com.test.userinfo.";// 映射sql的标识字符串
// 执行查询返回一个唯一user对象的sql
int count = session.selectOne(namespace + "getDb1");
System.out.println("=====total count tb_test====" + count);
// 创建能执行映射文件中sql的sqlSession
SqlSession session2 = getSessionFactory("dev2").openSession();
count = session2.selectOne(namespace + "getDb2");
System.out.println("=====total count=db====" + count);
}
public static SqlSessionFactory getSessionFactory(String environmentId) {
// mybatis的配置文件
String resource = "conf.xml";
// 使用类加载器加载mybatis的配置文件(它也加载关联的映射文件)
InputStream is = MultiDataSource.class.getClassLoader().getResourceAsStream(resource);
// 构建sqlSession的工厂
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is, environmentId);
return sessionFactory;
}
}
5、步骤五:实验验证
1)准备数据库表
2)实验结果