可以接受环境配置的两个方法签名是:
SqlSessionFactory factory = sqlSessionFactoryBuilder.build(reader, environment);
SqlSessionFactory factory = sqlSessionFactoryBuilder.build(reader, environment,properties);
如果环境被忽略,那么默认环境将会被加载,如下进行:
SqlSessionFactory factory = sqlSessionFactoryBuilder.build(reader);
SqlSessionFactory factory = sqlSessionFactoryBuilder.build(reader,properties);
环境元素定义了如何配置环境。
<environments default="development"> <environment id="development"> <transactionManager type="JDBC"> <property name="..." value="..."/> </transactionManager> <dataSource type="POOLED"> <property name="driver" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </dataSource> </environment> </environments>
注意这里的键:
1 默认的环境 ID(比如:default=”development”)。
2 每个 environment 元素定义的环境 ID(比如:id=”development”)。
覆盖配置类除了用插件来修改 MyBatis 核心行为之外,你也可以完全覆盖配置类。简单扩展它,然后覆盖其中的任意方法,之后传递它sqlSessionFactoryBuilder.build(myConfig) 方法的调用。这可能会严重影响 MyBatis 的行为,所以要小心。
3 事务管理器的配置(比如:type=”JDBC”)。
4 数据源的配置(比如:type=”POOLED”)。
默认的环境和环境 ID 是自我解释的。你可以使用你喜欢的名称来命名,只要确定默认的要匹配其中之一
测试时我使用了MySql,Sqlserver,Oracle三个数据库,以MySql为默认数据库,如下:
mysql.driver=com.mysql.jdbc.Driver mysql.url=jdbc:mysql://127.0.0.1:3306/company mysql.username=root mysql.password=xiaohu sqlserver.driver=com.microsoft.sqlserver.jdbc.SQLServerDriver sqlserver.url=jdbc:sqlserver://127.0.0.1:1434;database=company sqlserver.username=sa sqlserver.password=xiaohu oracle.driver=oracle.jdbc.driver.OracleDriver oracle.url=jdbc:oracle:thin:@127.0.0.1:1521:orcl oracle.username=scott oracle.password=xiaohu
主配置文件:mybatis/mybatis-config.xml
<?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> <!-- 引入属性文件 --> <properties resource="jdbc.properties"/> <!-- 配置类别名 --> <typeAliases> <typeAlias alias="Emp" type="com.tenghu.mybatis.model.Emp"/> <typeAlias alias="Dept" type="com.tenghu.mybatis.model.Dept"/> </typeAliases> <environments default="development"> <!-- 默认MySq --> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${mysql.driver}"/> <property name="url" value="${mysql.url}"/> <property name="username" value="${mysql.username}"/> <property name="password" value="${mysql.password}"/> </dataSource> </environment> <!-- Sqlserver数据库 --> <environment id="sqlserver"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${sqlserver.driver}"/> <property name="url" value="${sqlserver.url}"/> <property name="username" value="${sqlserver.username}"/> <property name="password" value="${sqlserver.password}"/> </dataSource> </environment> <!-- Oracle --> <environment id="oracle"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${oracle.driver}"/> <property name="url" value="${oracle.url}"/> <property name="username" value="${oracle.username}"/> <property name="password" value="${oracle.password}"/> </dataSource> </environment> </environments> <!-- 配置映射文件 --> <mappers> <mapper resource="com/tenghu/mybatis/mapper/DeptMapper.xml"/> <mapper resource="com/tenghu/mybatis/mapper/EmpMapper.xml"/> </mappers> </configuration>
打开SqlSession工具类MyBatiUtil.java:
package com.tenghu.mybatis.util; import java.io.IOException; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; public class MyBatisUtil { private MyBatisUtil(){} //Mysql Session工厂 private static SqlSessionFactory mysqlSessionFactory; //Sqlserver Session工厂 private static SqlSessionFactory sqlserverSessionFactory; //Oracle Session工厂 private static SqlSessionFactory oracleSessionFactory; static{ try { //实例化Mysql Session工厂 mysqlSessionFactory=new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis/mybatis-config.xml")); //实例化SqlServer Session 工厂 sqlserverSessionFactory=new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis/mybatis-config.xml"), "sqlserver"); //实例化Oracle Session工厂 oracleSessionFactory=new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis/mybatis-config.xml"), "oracle"); } catch (IOException e) { e.printStackTrace(); } } /** * 打开MySql会话 * @return */ public static SqlSession openMySqlSession(){ return mysqlSessionFactory.openSession(); } /** * 打开SqlServer会话 * @return */ public static SqlSession openSqlserverSession(){ return sqlserverSessionFactory.openSession(); } /** * 打开Oracle会话 * @return */ public static SqlSession openOracleSession(){ return oracleSessionFactory.openSession(); } /** * 关闭会话 * @param sqlSession */ public static void closeSession(SqlSession sqlSession){ if(null!=sqlSession) sqlSession.close(); } }
实体类:
package com.tenghu.mybatis.model; public class Dept { private int deptno; private String dname; private String loc; }
package com.tenghu.mybatis.model; public class Emp { private int id; private int age; private String name; }省略get和set方法
映射文件:EmpMapper.xml和DeptMapper.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.tenghu.mybatis.mapper.EmpMapper"> <select id="queryForList" resultType="Emp"> select * from emp </select> </mapper>
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.tenghu.mybatis.mapper.DeptMapper"> <select id="queryForList" resultType="Dept"> select * from dept </select> </mapper>
测试类:
package com.tenghu.test; import java.util.List; import org.apache.ibatis.session.SqlSession; import org.junit.Test; import com.tenghu.mybatis.model.Dept; import com.tenghu.mybatis.model.Emp; import com.tenghu.mybatis.util.MyBatisUtil; public class EmpMapperTest { /** * 测试MySql数据库 */ @Test public void testMysqlQueryForList(){ SqlSession sqlSession=null; try { //获取sql会话 sqlSession=MyBatisUtil.openMySqlSession(); //获取集合 List<Emp> empList=sqlSession.selectList("com.tenghu.mybatis.mapper.EmpMapper.queryForList"); for (Emp emp : empList) { System.out.println(emp.getAge()+"\t"+emp.getId()+"\t"+emp.getName()); } } catch (Exception e) { e.printStackTrace(); }finally{ //关闭SqlSession MyBatisUtil.closeSession(sqlSession); } } /** * 测试Sqlserver数据库 */ @Test public void testSqlServerQueryForList(){ SqlSession sqlSession=null; try { //获取sql会话 sqlSession=MyBatisUtil.openSqlserverSession(); //获取集合 List<Emp> empList=sqlSession.selectList("com.tenghu.mybatis.mapper.EmpMapper.queryForList"); for (Emp emp : empList) { System.out.println(emp.getAge()+"\t"+emp.getId()+"\t"+emp.getName()); } } catch (Exception e) { e.printStackTrace(); }finally{ //关闭SqlSession MyBatisUtil.closeSession(sqlSession); } } /** * 测试Oracle数据库 */ @Test public void testOracleQueryForList(){ SqlSession sqlSession=null; try { //获取Sql会话 sqlSession=MyBatisUtil.openOracleSession(); List<Dept> deptList=sqlSession.selectList("com.tenghu.mybatis.mapper.DeptMapper.queryForList"); for (Dept dept : deptList) { System.out.println(dept.getDeptno()+"\t"+dept.getDname()+"\t"+dept.getLoc()); } } catch (Exception e) { e.printStackTrace(); }finally{ //关闭SqlSession MyBatisUtil.closeSession(sqlSession); } } }
这样就完成了同时连接多个数据库了