MyBatis(3.2.3)

The key component of MyBatis is SqlSessionFactory from which we get SqlSession and execute the mapped SQL statements. The SqlSessionFactory object can be created using XML-based configuration or Java API.

The most commonly used approach for building SqlSessionFactory is XML-based configuration. The following mybatis-config.xml file shows how a typical MyBatis configuration file looks: 

<?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="application.properties">
        <property name="username" value="db_user"/>
        <property name="password" value="verysecurepwd"/>
    </properties>
    <settings>
        <setting name="cacheEnabled" value="true"/>
    </settings>
    
    <typeAliases>
        <typeAlias alias="Tutor" type="com.mybatis3.domain.Tutor"/>
        <package name="com.mybatis3.domain"/>
    </typeAliases>
    <typeHandlers>
        <typeHandler handler="com.mybatis3.typehandlers.PhoneTypeHandler"/>
        <package name="com.mybatis3.typehandlers"/>
    </typeHandlers>
    
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="${jdbc.driverClassName}"/>
                <property name="url" value="${jdbc.url}"/>
                <property name="username" value="${jdbc.username}"/>
                <property name="password" value="${jdbc.password}"/>
            </dataSource>
        </environment>
        <environment id="production">
            <transactionManager type="MANAGED"/>
            <dataSource type="JNDI">
                <property name="data_source" value="java:comp/jdbc/MyBatisDemoDS"/>
            </dataSource>
        </environment>
    </environments>
    
    <mappers>
        <mapper resource="com/mybatis3/mappers/StudentMapper.xml"/>
        <mapper url="file:///D:/mybatisdemo/mappers/TutorMapper.xml"/>
        <mapper class="com.mybatis3.mappers.TutorMapper"/>
    </mappers>
    
</configuration>
View Code

Environment

MyBatis supports configuring multiple dataSource environments so that deploying the application in various environments, such as DEV, TEST, QA, UAT, and PRODUCTION, can be easily achieved by changing the default environment value to the desired environment id value. In the preceding configuration, the default environment has been set to development. When deploying the application on to production servers, you don't need to change the configuration much; just set the default environment to the production environment id attribute.

Sometimes, we may need to work with multiple databases within the same application. For example, we may have the SHOPPINGCART database to store all the order details and the REPORTS database to store the aggregates of the order details for reporting purposes.

If your application needs to connect to multiple databases, you'll need to configure each database as a separate environment and create a separate SqlSessionFactory object for each database.

<environments default="shoppingcart">
    <environment id="shoppingcart">
        <transactionManager type="MANAGED"/>
        <dataSource type="JNDI">
            <property name="data_source" value="java:comp/jdbc/ShoppingcartDS"/>
        </dataSource>
    </environment>
    <environment id="reports">
        <transactionManager type="MANAGED"/>
        <dataSource type="JNDI">
            <property name="data_source" value="java:comp/jdbc/ReportsDS"/>
        </dataSource>
    </environment>
</environments>

We can create SqlSessionFactory for a given environment as follows:

inputStream = Resources.getResourceAsStream("mybatis-config.xml");
defaultSqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
cartSqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream,"shoppingcart");
reportSqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream,"reports");

When we create SqlSessionFactory without explicitly defining environment id, SqlSessionFactory will be created using the default environment. In the preceding code, defaultSqlSessionFactory was created using the shoppingcart environment settings.

For each environment, we need to configure the dataSource and transactionManager elements.

 

DataSource

The dataSource element is used to configure the database connection properties.

<dataSource type="POOLED">
    <property name="driver" value="${jdbc.driverClassName}"/>
    <property name="url" value="${jdbc.url}"/>
    <property name="username" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>
</dataSource>

The dataSource type can be one of the built-in types such as UNPOOLED, POOLED, or JNDI.

  • If you set the type to UNPOOLED, MyBatis will open a new connection and close that connection for every database operation. This method can be used for simple applications that have a small number of concurrent users.
  • If you set the type to POOLED, MyBatis will create a pool of database connections, and one of these connections will be used for the database operation. Once this is complete, MyBatis will return the connection to the pool. This is a commonly used method for developing/testing environments.
  • If you set the type to JNDI, MyBatis will get the connection from the JNDI dataSource that has typically been configured in the application server. This is a preferred method in production environments.

TransactionManager

MyBatis supports two types of transaction managers: JDBC and MANAGED.

The JDBC transaction manager is used where the application is responsible for managing the connection life cycle, that is, commit, rollback, and so on. When you set the TransactionManager property to JDBC, behind the scenes MyBatis uses the JdbcTransactionFactory class to create TransactionManager. For example, an application deployed on Apache Tomcat should manage the transactions by itself.

The MANAGED transaction manager is used where the application server is responsible for managing the connection life cycle. When you set the TransactionManager property to MANAGED, behind the scenes MyBatis uses the ManagedTransactionFactory class to create TransactionManager. For example, a JavaEE application deployed on an application server, such as JBoss, WebLogic, or GlassFish, can leverage the application server's transaction management capabilities using EJB. In these managed environments, you can use the MANAGED transaction manager.

原文地址:https://www.cnblogs.com/huey/p/5227328.html