码迷,mamicode.com
首页 > 数据库 > 详细

Mybatis 动态切换数据库

时间:2016-07-12 14:02:57      阅读:229      评论:0      收藏:0      [点我收藏+]

标签:

mybatis介绍:


每一个Mybatis的应用程序都以一个SqlSessionFactory对象的实例为核心。SqlSessionFactory对象实例可以通过SqlSessionFactoryBuilder对象获得。SqlSessionFactoryBuilder对象可以从XML配置文件或从Configuration类的习惯准备的实例中构建SqlSessionFactory对象。


从XML文件中构建SqlSessionFactory的实例非常简单。这里建议使用类的路径的资源文件来配置,这样我们就可以使用任意的Reader实例,这个实例包括有文字形式的文件路径或URL形式的文件路径file://来创建。Mybatis包含了一些工具类,称作为资源,这些工具类包含一些方法,这些方法使得从类路径或其他位置加载资源文件更加简单。


<span style="font-size:18px;">String resource = "org/mybatis/example/mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);</span>

XML的配置文件包含对MyBatis系统的核心设置,包含获取数据库链接实例的数据源和决定事物范围和控制的事物管理器。


<span style="font-size:18px;"><?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="development">
    <environment id="development">
      <transactionManager type="JDBC"/>
      <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>
  <mappers>
    <mapper resource="org/mybatis/example/BlogMapper.xml"/>
  </mappers>
</configuration></span>

大家我们今天就可以从这里进行入手,大家可以看到,


sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

但是事实上,SQLSessionFactoryBuilder类负责构建SqlSessionFactory,并且提供了多个build的重载方法,丹其实很多都是在调用同意签名的方法,例如public SqlSessionFactory build(InputStream inputStream,String environment,Properties properties),只是由于方法参数environment和properties都可以为null,所以为了提供调用的便利性,才提供了下面的三个方法。


<span style="font-size:18px;">public SqlSessionFactory build(InputStream inputStream)
public SqlSessionFactory build(InputStream inputStream, String environment) 
public SqlSessionFactory build(InputStream inputStream, Properties properties)</span>

真正加载的方法只有三种:


<span style="font-size:18px;">public SqlSessionFactory build(InputStream inputStream, String environment, Properties properties) 
public SqlSessionFactory build(Reader reader, String environment, Properties properties)
public SqlSessionFactory build(Configuration config)</span>


建造者模式:


可以看出,配置信息可以以三种形式提供给SqlSessionFactory的build方法,分别是InputStream(字节流)、Reader(字符流)、Configuration(类),由于字节流与字符流都属于读取配置文件的方式,所以从配置信息的来源就很容易想到创建一个SqlSessionFactory方式:


(1)读取XML文件构造方式


<span style="font-size:18px;">String resource = "org/mybatis/example/mybatis-config.xml";
  InputStream inputStream = Resources.getResourceAsStream(resource);
  SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream) ;</span>


(2)编程构造方式


<span style="font-size:18px;">DataSource dataSource = BlogDataSourceFactory.getBlogDataSource();
TransactionFactory transactionFactory = new JdbcTransactionFactory();
Environment environment = new Environment("development", transactionFactory, dataSource);
Configuration configuration = new Configuration(environment);
configuration.addMapper(BlogMapper.class);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration) ;</span>

下面先来分析XML文件构建方式的build方法的源码:


<span style="font-size:18px;">public SqlSessionFactory build(InputStream inputStream, String environment, Properties properties) {
    try {
      XMLConfigBuilder parser = new XMLConfigBuilder(inputStream, environment, properties);
      return build(parser.parse());
    } catch (Exception e) {
      throw ExceptionFactory.wrapException("Error building SqlSession.", e);
    } finally {
      ErrorContext.instance().reset();
      try {
        inputStream.close();
      } catch (IOException e) {
        // Intentionally ignore. Prefer previous error.
      }
    }
  }</span>


通过上面的几行代码,就能看出基于XML文件的这种构造方式,通过XML读取信息的工作之后,也是构造出Configuration对象之后,再继续进行SqlSessionFactory的构建工作的,只是多了些XML的解析工作,所以我们只需要单钩植入,直接分析变成构造方式的代码就可以了,或者是直接分析build(Parser.parse())这句代码


<span style="font-size:18px;"> public SqlSessionFactory build(Configuration config) {
                return new DefaultSqlSessionFactory(config);
    }</span>

其实这么看来SqlSessionFactory在mybatis的默认实现类为org.apache.ibatis.session.defaults.DefaultSqlSessionFactory , 其构造过程主要是注入了Configuration的实例对象,Configuration的实例对象即可通过解析xml配置文件产生,也可能通过代码直接构造。


以上代码使用了一个设计模式:建设者模式(Builder),SqlSessionFactoryBuilder扮演具体的建造者,Configuration类则负责建造的细节工作,SqlSession则是建造出来的产品。


看一下类图和创建者模式的基本形态图:


技术分享


技术分享


代码实现Mybatis动态切换数据库:


目录结构:


技术分享


Goods:


<span style="font-size:18px;">package com.csdn.kane.domain;

import java.sql.Timestamp;

public class Goods {
	 private int id;  
	    private int categoryId;  
	    private String name;  
	    private float price;  
	    private String description;  
	    private int acount;  
	    private Timestamp updateTime;  
	      
	    public int getId() {  
	        return id;  
	    }  
	    public void setId(int id) {  
	        this.id = id;  
	    }  
	    public int getCategoryId() {  
	        return categoryId;  
	    }  
	    public void setCategoryId(int categoryId) {  
	        this.categoryId = categoryId;  
	    }  
	    public String getName() {  
	        return name;  
	    }  
	    public void setName(String name) {  
	        this.name = name;  
	    }  
	    public float getPrice() {  
	        return price;  
	    }  
	    public void setPrice(float price) {  
	        this.price = price;  
	    }  
	    public String getDescription() {  
	        return description;  
	    }  
	    public void setDescription(String description) {  
	        this.description = description;  
	    }  
	    public int getAcount() {  
	        return acount;  
	    }  
	    public void setAcount(int acount) {  
	        this.acount = acount;  
	    }  
	    public Timestamp getUpdateTime() {  
	        return updateTime;  
	    }  
	    public void setUpdateTime(Timestamp updateTime) {  
	        this.updateTime = updateTime;  
	    }  
}
</span>


GoodsMapper:


<span style="font-size:18px;">package com.csdn.kane.dao;

import org.apache.ibatis.annotations.Select;

import com.csdn.kane.domain.Goods;

public interface GoodsMapper {
	 @Select("SELECT * FROM Goods WHERE id=#{id}")  
	 public Goods selectGoods(int id); 
}
</span>

applicationContext.xml:


<span style="font-size:18px;"><?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>  
    <typeAliases>  
        <typeAlias alias="Goods" type="com.csdn.kane.domain.Goods"/>  
    </typeAliases>  
  
    <environments default="development">  
        <environment id="development">  
            <transactionManager type="JDBC" />  
            <dataSource type="POOLED">  
   		<property name="driver" value="${jdbc.driver}" />   
                <property name="url" value="${jdbc.url}" />  
                <property name="username" value="${jdbc.username}" />  
                <property name="password" value="${jdbc.password}" />
            </dataSource>  
        </environment>  
    </environments>  
    <mappers>  
        <mapper class="com.csdn.kane.dao.GoodsMapper"/>  
    </mappers>  
</configuration> </span>

TestMybatis:


<span style="font-size:18px;">package com.csdn.kane.test;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.csdn.kane.dao.GoodsMapper;
import com.csdn.kane.domain.Goods;

public class TestMybitas {  
    
    public static void main(String[] args) throws IOException {  
        //最基本的mybitas示例方法  
        TestMybitas.testMethod();  
    }  
      
    public static void testMethod() throws IOException{  
        String resource = "applicationContext.xml";  
        InputStream inputStream = Resources.getResourceAsStream(resource);  
        
        Properties properties = new Properties();
		properties.setProperty("jdbc.driver", "com.mysql.jdbc.Driver");
		properties.setProperty(
				"jdbc.url",
				"jdbc:mysql://localhost:3306/qmxtest1?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true");		
		properties.setProperty("jdbc.username", "root");
		properties.setProperty("jdbc.password", "root");

       
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream,properties);  
          
        SqlSession session = sqlSessionFactory.openSession();  
        try {  
            GoodsMapper mapper = session.getMapper(GoodsMapper.class);  
            Goods goods = mapper.selectGoods(1);  
            System.out.println("good description:"+goods.getDescription());  
        } finally {  
            session.close();  
        }  
    }  
  
}  
</span>

这里传入的Property是咱们需要访问的数据库,当然,后面的参数是可以作为变量进行传递的。













Mybatis 动态切换数据库

标签:

原文地址:http://blog.csdn.net/qiumuxia0921/article/details/51886346

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!