Mybatis源码分析:环境设置Environment

Posted zhengzuozhanglina

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mybatis源码分析:环境设置Environment相关的知识,希望对你有一定的参考价值。

Mybatis环境配置类Environment

在初始化Mybatis框架时,需要配置environment元素,该元素允许有多个,方便我们在不同环境下切换后端的存储。拥有一个id属性用于该环境,此id必须唯一,否则后续配置会覆盖前面的配置,该元素拥有两个子元素(事务管理器),(数据源),这两个元素后续会进行讲解,目前只需要记住名称即可。如下便是的相关配置,该配置定义了dev,test,prod三种环境

 

 <configuration>
      <!--引入外部资源  -->
      <properties resource="./mybatis-mysql.properties"> 
      </properties>
      <settings>
          <setting name="cacheEnabled" value="true"/>
      </settings>
      <objectFactory type="com.zzz.mybatis.object.MyObjectFacotry">
          <property name="env" value="test"/>
      </objectFactory>
      <!--设置默认的环境为开发环境  -->
        <environments default="development">
    <environment id="development">
      <transactionManager type="JDBC"/>
      <dataSource type="UNPOOLED">
        <property name="driver" value="$dev.driver"/>
        <property name="url" value="$dev.url"/>
        <property name="username" value="$dev.username"/>
        <property name="password" value="$dev.password"/>
      </dataSource>
    </environment>
    <!--测试环境用  -->
    <environment id="test">
        <transactionManager type="JDBC"/>
      <dataSource type="POOLED">
        <property name="driver" value="$test.driver"/>
        <property name="url" value="$test.url"/>
        <property name="username" value="$test.username"/>
        <property name="password" value="$test.password"/>
      </dataSource>
    </environment>
    <!--生产环境用  -->
    <environment id="prod">
        <transactionManager type="MANAGED"/>
      <dataSource type="JDBC">
        <property name="driver" value="$prod.driver"/>
        <property name="url" value="$prod.url"/>
        <property name="username" value="$prod.username"/>
        <property name="password" value="$prod.password"/>
      </dataSource>
    </environment>
  </environments>
  </configuration>

 

mybatis-config.xml

dev.driver=com.mysql.jdbc.Driver
dev.url=jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC
dev.username=root
dev.password=root

test.driver=com.mysql.jdbc.Driver
test.url=jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC
test.username=root
test.password=root

prod.driver=com.mysql.jdbc.Driver
prod.url=jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC
prod.username=root
prod.password=root

 

与之对应的Java类为org.apache.ibatis.mapping.Environment,该类同样拥有三个属性与xml元素对应,如下代码

private final String id;
  private final TransactionFactory transactionFactory;//事务工厂
  private final DataSource dataSource;//数据源

 

创建一个Environment

Environment的创建可以通过构造器方式和建造器方式,Environment类中有一个内在的静态类Builder,Builder类提供了一个builder方法对Environment进行构建。代码如下

方式一

 1  @Test
 2     public void TestgetInstanceEnviroment() throws SQLException 
 3         //设置mysql数据源
 4         MysqlConnectionPoolDataSource dataSource=new MysqlConnectionPoolDataSource();
 5         dataSource.setUser("root");
 6         dataSource.setPassword("root");
 7         dataSource.setUrl("jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC");
 8         //设置事务工厂
 9         TransactionFactory transactionFactory= new  JdbcTransactionFactory();
10         //实例化id
11         Environment environment = new Environment("test", transactionFactory, dataSource);
12         System.out.println(environment.getId()+environment.getDataSource().toString()+environment.getTransactionFactory().toString());
13 
14     

 

方式二

 1     @Test
 2     public void TestgetInstanceEnviromentByBuilder() throws SQLException 
 3         Environment environment = new Environment.Builder("prod")
 4                                         .transactionFactory(new JdbcTransactionFactory())
 5                                         .dataSource(new MysqlConnectionPoolDataSource() 
 6                                             
 7                                                 setUser("root");
 8                                                 setPassword("root");
 9                                                 setUrl("jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC");
10                                                 
11                                             )
12                                         .build();
13         System.out.println(environment.getId()+environment.getDataSource().toString()+environment.getTransactionFactory().toString());
14 
15 
16 
17     

 

应用Enviroment

 1 @Test
 2     public void TestSqlsessionFactoryBuilder() throws SQLException 
 3 
 4         SqlSessionFactoryBuilder factoryBuilder = new SqlSessionFactoryBuilder();
 5         InputStream inputStream = getClass().getResourceAsStream("/mybatis-config.xml");
 6         SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
 7         SqlSessionFactory sessionFactory=builder.build(inputStream);
 8         SqlSession session=sessionFactory.openSession();
 9         Environment environment=session.getConfiguration().getEnvironment();
10         System.out.println(environment.getId()+environment.getDataSource().toString()+environment.getTransactionFactory().toString());
11          environment = new Environment.Builder("other")
12                 .transactionFactory(new JdbcTransactionFactory())
13                 .dataSource(new MysqlConnectionPoolDataSource() 
14                     
15                         setUser("mytest");
16                         setPassword("mytest");
17                         setUrl("jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC");
18                         
19                     )
20                 .build();
21         session.getConfiguration().setEnvironment(environment);
22         System.out.println(environment.getId()+environment.getDataSource().toString()+environment.getTransactionFactory().toString());
23 
24 

 

development  org.apache.ibatis.datasource.unpooled.UnpooledDataSource@7a5d012corg.apache.ibatis.transaction.jdbc.JdbcTransactionFactory@3fb6a447
other        com.zzz.mybatis.service.EnviromentTest$2@6be46e8forg.apache.ibatis.transaction.jdbc.JdbcTransactionFactory@3567135c

 

以上是关于Mybatis源码分析:环境设置Environment的主要内容,如果未能解决你的问题,请参考以下文章

MyBatis源码分析 : 环境准备

MyBatis简单源码分析1 - 环境搭建

mybatis源码分析之01环境搭建

Mybatis源码分析

MyBatis源码解析-搭建调试环境

Mybatis源码分析