码迷,mamicode.com
首页 > 其他好文 > 详细

Mybatis源码分析:环境设置Environment

时间:2019-07-26 19:07:59      阅读:94      评论:0      收藏:0      [点我收藏+]

标签:rop   resource   java类   设置   other   XML   静态类   size   private   

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

标签:rop   resource   java类   设置   other   XML   静态类   size   private   

原文地址:https://www.cnblogs.com/zhengzuozhanglina/p/11252135.html

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