MyBatis(3.2.3) - Configuring MyBatis using XML, Environment
2016-02-29 14:51
232 查看
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:
View Code
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.
We can create SqlSessionFactory for a given environment as follows:
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.
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.
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.
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.
相关文章推荐
- 移动端H5页面的设计稿尺寸大小规范-转载自http://www.chinaz.com/design/2015/1103/465670.shtml
- 终止线程的方法
- go语言值得学习的开源项目推荐
- MySQL分库分表基础表介绍(1st)
- 谈谈使用 promise 时候的一些反模式
- 集成bagging 算法 理论
- 【影评】:卧虎藏龙中的人物性格的分析
- (三)JavaMail 发送邮件之正文 && 附件
- RGB与YUV图像视频格式的相互转换
- 微信JSSDK接口,previewImage
- 源码分析—ArrayList
- Android 项目及编码的规范
- SharePoint2016 New Feature(Preview) - 企业合规管理
- Auto property synthesis will not synthesizeproterty;it will be implemented by its superclass, use@dy
- 老是糊涂 数据格式
- 回流和重绘
- 页面渲染流程
- Red Hat 6 换源
- Android 4.4前后版本读取图库图片和拍照完美解决方案
- MySQL中/*!40100注释