MyBatis(3.2.3) - Configuring MyBatis using XML, Environment
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>
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.
MyBatis(3.2.3) - Configuring MyBatis using XML, Environment的更多相关文章
- MyBatis(3.2.3) - Configuring MyBatis using XML, typeHandlers
As discussed in the previous chapter, MyBatis simplifies the persistent logic implementation by abst ...
- MyBatis(3.2.3) - Configuring MyBatis using XML, typeAliases
In the SQL Mapper configuration file, we need to give the fully qualified name of the JavaBeans for ...
- MyBatis(3.2.3) - Configuring MyBatis using XML, Settings
The default MyBatis global settings, which can be overridden to better suit application-specific nee ...
- MyBatis(3.2.3) - Configuring MyBatis using XML, Properties
The properties configuration element can be used to externalize the configuration values into a prop ...
- MyBatis(3.2.3) - Configuring MyBatis using XML, Mappers
Mapper XML files contain the mapped SQL statements that will be executed by the application using st ...
- Mybatis增加对象属性不增加mapper.xml的情况
Mybatis增加对象属性不增加mapper.xml的情况: 只增加Model 对象的属性,在查询语句中返回相同名称的字段,但是在mapper中的 resultMap上面不进行新增字段的增加,查询结果 ...
- Mybatis学习总结(三)——SqlMapConfig.xml全局配置文件解析
经过上两篇博文的总结,对mybatis中的dao开发方法和流程基本掌握了,这一节主要来总结一下mybatis中的全局配置文件SqlMapConfig.xml在开发中的一些常用配置,首先看一下该全局配置 ...
- Java Persistence with MyBatis 3(中文版) 第三章 使用XML配置SQL映射器
关系型数据库和SQL是经受时间考验和验证的数据存储机制.和其他的ORM 框架如Hibernate不同,MyBatis鼓励开发者可以直接使用数据库,而不是将其对开发者隐藏,因为这样可以充分发挥数据库服务 ...
- Mybatis手工写sql语句及Mapper.xml方法
首先在项目中 建一个mapper包,然后在spring集合mybatis的配置文件中设置扫描这个mapper包 然后,建 封装查询结果需要的 pojo 然后,在 mapper包中创建 Mapper接口 ...
随机推荐
- AVCaptureDevice
转载自:http://blog.csdn.net/andy_jiangbin/article/details/19820717 0.媒体采集的几个东西.这里所需要明白的是,在这个流程中,这里会存在 ...
- 初学Android 二 创建项目以及目录结构
命令行创建 android create project Usage: android [global options] create project [action options] Global ...
- 深度剖析WordPress主题结构(转)
利用强大的技术,可以把基于wordpress的网站做成各种各样的形式,这除了要求wordpress主题开发人员精通html,PHP,JS,CSS等技术,还需要开发者掌握WordPress主题的框架. ...
- ckeditor内容保存后显示问题
ckeditor保存到数据库中的数据是html格式,如果要在前台进行显示,需要采用${news.content },这样数据才能进行正常的显示,否则如果采用<s:property value=& ...
- npm package 装包匹配原则
经常看到package.json 里面有这样的devDependencies: "devDependencies": { "@angular/common": ...
- 理解Windows中的路由表和默认网关
每一个Windows系统中都具有IP路由表,它存储了本地计算机可以到达的网络目的地址范围和如何到达的路由信息.路由表是TCP/IP通信的基础,本地计算机上的任何TCP/IP通信都受到路由表的控制. 理 ...
- 浅谈IT员工管理
本人尽管还不是管理人员,但也管理过学弟们(不是同校.仅仅是工作中同事,为了好称呼叫学弟).也被管理着,工作也好多年了.今天又感而发.想来谈谈假设管理好员工(在此声明,我仅仅是发表个人意见哦.不要喷.哈 ...
- Perl多进程
perl作为一种解释性的语言,非常受广大系统管理员的欢迎,优点么就不多说了,坏处也有不少,比如对线程的支持,就一直不咋地,所以大多数情况下,我们都须要多个进程,来帮助我们完毕工作,闲话少说,上代码. ...
- 详解Android Handler的使用-别说你不懂handler
我们进行Android开发时,Handler可以说是使用非常频繁的一个概念,它的用处不言而喻.本文就详细介绍Handler的基本概念和用法. Handler的基本概念 Handler主 ...
- 发布站点到远程FTP根目录
第一步:修改 phpcms/modules/admin/templates/site_add.tpl.php 找到 <legend><?php echo L('release_poi ...