iBATIS 3 试用手记 - The FUTURE - ITeye技术网站
body
{
font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI",Tahoma,Helvetica,Sans-Serif,"Microsoft YaHei", Georgia,Helvetica,Arial,sans-serif,宋体, PMingLiU,serif;
font-size: 10.5pt;
line-height: 1.5;
}
html, body
{
}
h1 {
font-size:1.5em;
font-weight:bold;
}
h2 {
font-size:1.4em;
font-weight:bold;
}
h3 {
font-size:1.3em;
font-weight:bold;
}
h4 {
font-size:1.2em;
font-weight:bold;
}
h5 {
font-size:1.1em;
font-weight:bold;
}
h6 {
font-size:1.0em;
font-weight:bold;
}
img {
border:0;
max-width: 100%;
}
blockquote {
margin-top:0px;
margin-bottom:0px;
}
table {
border-collapse:collapse;
border:1px solid #bbbbbb;
}
td {
border-collapse:collapse;
border:1px solid #bbbbbb;
}
iBATIS 3 试用手记 - The FUTURE - ITeye技术网站
前记:本来打算去CSDN写这篇文章的,结果CSDN的服务器又出问题了,登录了N次都进不去,郁闷,干脆换个Blog来写。
iBATIS以其对SQL控制的灵活性而受到许多大型项目的青睐,它不像Hibernate那样是完全面向对象的,iBATIS是一个半自动化的O/R Mapping框架。今晚散逛到iBATIS的官网(http://ibatis.apache.org/),发现iBATIS 3已经到Beta 5阶段,应该说已经比较稳定了,于是Download了一个下来研究,早就听说iBATIS 3在相比iBATIS 2作了很大改动,看来不假,呵呵,废话少说,见下。
首先是初始化的改变:
- Reader reader = Resources.getResourceAsReader(CONFIG_FILE_PATH);
- SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader, "development_oracle");
- SqlSession session = sqlSessionFactory.openSession();
Reader reader = Resources.getResourceAsReader(CONFIG_FILE_PATH);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader, "development_oracle");
SqlSession session = sqlSessionFactory.openSession();
iBATIS 2中的SqlMapClient被SqlSession所替代, 而iBATIS
2中的静态类SqlMapClientBuilder也被SqlSessionFactoryBuilder所替代,变为了非静态的,此外最重要的是
iBATIS
3中需要使用openSession()方法来返回SqlSession的实例,至于上述代码中build方法的第二参数
“development_oracle”是环境配置ID,稍候会讲到。
下面来看Configuration文件。
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE configuration
- PUBLIC "-//ibatis.apache.org//DTD Config 3.0//EN" "http://ibatis.apache.org/dtd/ibatis-3-config.dtd">
- <configuration>
- <properties resource="conf/database.properties" />
- <environments default="development">
- <environment id="development">
- <transactionManager type="JDBC" />
- <dataSource type="POOLED">
- <property name="driver" value="${database.driver}"/>
- <property name="url" value="${database.url}"/>
- <property name="username" value="${database.user}"/>a
- <property name="password" value="${database.password}"/>
- </dataSource>
- </environment>
- <environment id="development_oracle">
- <transactionManager type="JDBC" />
- <dataSource type="POOLED">
- <property name="driver" value="${oracle.database.driver}"/>
- <property name="url" value="${oracle.database.url}"/>
- <property name="username" value="${oracle.database.user}"/>
- <property name="password" value="${oracle.database.password}"/>
- </dataSource>
- </environment>
- </environments>
- <mappers>
- <mapper resource="conf/NewsNotice.xml"/>
- </mappers>
- </configuration>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//ibatis.apache.org//DTD Config 3.0//EN" "http://ibatis.apache.org/dtd/ibatis-3-config.dtd"> <configuration>
<properties resource="conf/database.properties" />
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${database.driver}"/>
<property name="url" value="${database.url}"/>
<property name="username" value="${database.user}"/>a
<property name="password" value="${database.password}"/>
</dataSource>
</environment> <environment id="development_oracle">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${oracle.database.driver}"/>
<property name="url" value="${oracle.database.url}"/>
<property name="username" value="${oracle.database.user}"/>
<property name="password" value="${oracle.database.password}"/>
</dataSource>
</environment>
</environments> <mappers>
<mapper resource="conf/NewsNotice.xml"/>
</mappers>
</configuration>
database.properties内容:
- database.driver = org.gjt.mm.mysql.Driver
- database.url = jdbc:mysql://localhost:3306/test
- database.user = root
- database.password = root
- oracle.database.driver = oracle.jdbc.driver.OracleDriver
- oracle.database.url = jdbc:oracle:thin:@locahost:1521:ORCL2
- oracle.database.user = Tester
- oracle.database.password = password
database.driver = org.gjt.mm.mysql.Driver
database.url = jdbc:mysql://localhost:3306/test
database.user = root
database.password = root oracle.database.driver = oracle.jdbc.driver.OracleDriver
oracle.database.url = jdbc:oracle:thin:@locahost:1521:ORCL2
oracle.database.user = Tester
oracle.database.password = password
本人觉得iBATIS 3配置文件最大的变化是增加了<environment/>标签,这样对于不同的环境可以配置不同的属性,无论从开发还是布署都显得非常方便。
iBATIS
3提供了2种transactionManager类型,分别为JDBC和MANAGED,如果设定为MANAGED,则将整个Transaction的
生命周期交由J2EE Container管理;至于JDBC就不用说了吧,这个地球人都晓得的:-D
至于Datasource的类型,iBATIS 3提供了UNPOOLED、POOLED和JNDI三种方式,
- UNPOOLED:不使用连接池连接Database,每次请求时Open Connection,结束时Close Connection;
- POOLED:以池化方式连接Database,它有许多属性可供设置,像poolMaximumActiveConnections、
poolMaximumIdleConnections、poolMaximumCheckoutTime、poolPingQuery等等; - JNDI:在J2EE Container中配置Datasource。
<Mapper>标签指定要使用的Mapping文件。
下面来看Mapping文件内容,这是iBATIS 3改动最多的地方。
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE mapper
- PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN" "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
- <mapper namespace="NewsNotice">
- <resultMap type="org.newsnotice.domain.NewsNoticeModel" id="resultMap-getNewsNotice1">
- <id column="NN_ID" property="id" />
- <result column="CATEGORY" property="category" />
- <result column="SUBJECT" property="subject" />
- <result column="POSTED_DATE" property="postedDate" />
- <result column="EXPIRY_DATE" property="expiryDate" />
- <result column="ALERT" property="alert" />
- <result column="EMAIL_ALERT" property="emailAlert" />
- <result column="AUDIENCE" property="audience" />
- <result column="FILTER" property="filter" />
- <result column="FILTER_VALUE" property="filterValue" />
- <result column="SUB_FILTER_VALUE" property="subFilterValue" />
- <result column="EXCLUDE_USER_ID" property="excludeUserId" />
- <result column="WF_DEPARTMENT" property="department" />
- <result column="WF_STATUS" property="status" />
- <result column="WF_NOTES" property="notes" />
- <result column="DEFUNCT_IND" property="defunctInd" />
- <result column="APPROVER" property="approver" />
- <association property="newsNoticeContent" column="CONTENT_ID" javaType="org.newsnotice.domain.NewsNoticeContentModel">
- <id column="CONTENT_ID" property="id" />
- <result column="PARENT_NN_ID" property="parentId" />
- <result column="CONTENT" property="content" />
- </association>
- <collection property="newsNoticeMsgBoxList" ofType="org.newsnotice.domain.NewsNoticeMsgBoxModel" >
- <id column="MSG_BOX_ID" property="id"/>
- <result column="USER_ID" property="userId" />
- <result column="MSG_BOX_NN_ID" property="nnId" />
- <result column="FOLDER" property="folder" />
- <result column="READ" property="read" />
- <result column="READ_ON" property="readOn" />
- <result column="MSG_BOX_DEFUNCT_IND" property="defunctInd" />
- <result column="MSG_BOX_PI_NO" property="piNo" />
- </collection>
- </resultMap>
- <select id="getNewsNotice" parameterType="org.newsnotice.domain.NewsNoticeModel" resultMap="resultMap-getNewsNotice1" >
- SELECT A.NN_ID, A.CATEGORY, A.SUBJECT, A.POSTED_DATE, A.EXPIRY_DATE, A.ALERT, A.EMAIL_ALERT, A.AUDIENCE,
- A.FILTER, A.FILTER_VALUE, A.SUB_FILTER_VALUE, A.EXCLUDE_USER_ID, A.WF_DEPARTMENT, A.WF_STATUS, A.WF_NOTES,
- A.DEFUNCT_IND, A.APPROVER, B.ID CONTENT_ID, B.PARENT_NN_ID, B.CONTENT, C.ID MSG_BOX_ID, C.USER_ID,
- C.NN_ID MSG_BOX_NN_ID, C.FOLDER, C.READ, C.READ_ON, C.DEFUNCT_IND MSG_BOX_DEFUNCT_IND, C.PI_NO MSG_BOX_PI_NO
- FROM NN_MSTR A, NN_CONTENT B, NN_MSG_BOX C
- WHERE A.NN_ID = B.PARENT_NN_ID
- AND A.NN_ID = C.NN_ID
- <if test="id != null">
- AND A.NN_ID = #{id}
- </if>
- <if test="category != null">
- AND A.CATEGORY = #{category}
- </if>
- <if test="status != null">
- AND A.WF_STATUS = #{status}
- </if>
- </select>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN" "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd"> <mapper namespace="NewsNotice">
<resultMap type="org.newsnotice.domain.NewsNoticeModel" id="resultMap-getNewsNotice1">
<id column="NN_ID" property="id" />
<result column="CATEGORY" property="category" />
<result column="SUBJECT" property="subject" />
<result column="POSTED_DATE" property="postedDate" />
<result column="EXPIRY_DATE" property="expiryDate" />
<result column="ALERT" property="alert" />
<result column="EMAIL_ALERT" property="emailAlert" />
<result column="AUDIENCE" property="audience" />
<result column="FILTER" property="filter" />
<result column="FILTER_VALUE" property="filterValue" />
<result column="SUB_FILTER_VALUE" property="subFilterValue" />
<result column="EXCLUDE_USER_ID" property="excludeUserId" />
<result column="WF_DEPARTMENT" property="department" />
<result column="WF_STATUS" property="status" />
<result column="WF_NOTES" property="notes" />
<result column="DEFUNCT_IND" property="defunctInd" />
<result column="APPROVER" property="approver" />
<association property="newsNoticeContent" column="CONTENT_ID" javaType="org.newsnotice.domain.NewsNoticeContentModel">
<id column="CONTENT_ID" property="id" />
<result column="PARENT_NN_ID" property="parentId" />
<result column="CONTENT" property="content" />
</association>
<collection property="newsNoticeMsgBoxList" ofType="org.newsnotice.domain.NewsNoticeMsgBoxModel" >
<id column="MSG_BOX_ID" property="id"/>
<result column="USER_ID" property="userId" />
<result column="MSG_BOX_NN_ID" property="nnId" />
<result column="FOLDER" property="folder" />
<result column="READ" property="read" />
<result column="READ_ON" property="readOn" />
<result column="MSG_BOX_DEFUNCT_IND" property="defunctInd" />
<result column="MSG_BOX_PI_NO" property="piNo" />
</collection>
</resultMap> <select id="getNewsNotice" parameterType="org.newsnotice.domain.NewsNoticeModel" resultMap="resultMap-getNewsNotice1" >
SELECT A.NN_ID, A.CATEGORY, A.SUBJECT, A.POSTED_DATE, A.EXPIRY_DATE, A.ALERT, A.EMAIL_ALERT, A.AUDIENCE,
A.FILTER, A.FILTER_VALUE, A.SUB_FILTER_VALUE, A.EXCLUDE_USER_ID, A.WF_DEPARTMENT, A.WF_STATUS, A.WF_NOTES,
A.DEFUNCT_IND, A.APPROVER, B.ID CONTENT_ID, B.PARENT_NN_ID, B.CONTENT, C.ID MSG_BOX_ID, C.USER_ID,
C.NN_ID MSG_BOX_NN_ID, C.FOLDER, C.READ, C.READ_ON, C.DEFUNCT_IND MSG_BOX_DEFUNCT_IND, C.PI_NO MSG_BOX_PI_NO
FROM NN_MSTR A, NN_CONTENT B, NN_MSG_BOX C
WHERE A.NN_ID = B.PARENT_NN_ID
AND A.NN_ID = C.NN_ID
<if test="id != null">
AND A.NN_ID = #{id}
</if>
<if test="category != null">
AND A.CATEGORY = #{category}
</if>
<if test="status != null">
AND A.WF_STATUS = #{status}
</if>
</select>
先说动态SQL,这是iBATIS最强大的地方,如果熟悉iBATIS
2的话,一眼可以看出没有了<isNotNull>、<isNotEmpty>、<isLessThan>等熟悉的
标签,不错,iBATIS使用了类似JSTL的标
签<if>、<choose>、<when>、<otherwise>、<foreach>
等来代替原来的标签,并且传值方式由#property name#, $property name$变为了#{property
name}和${property name},就我个人而言,这些标签感觉没有iBATIS2用上去爽。
此外根元素<mapper>的属性namespace在iBATIS 3中是required,而不像iBATIS 2中是可选的,可要可不要。
下面来说说新增元素<association>和<collection>,<association>
对应于Java中的Has
A模型,也可以理解为数据库中一对一关系,拿上述例子来说,每条消息的概要信息与消息内容是分别存放在两张Table中的,可以通过上述方法一次性将其取
出来,而不需要执行多次查询。而<collection>有点类型主从表关系,即one-to-many模型。
查询标签<select>也有所改变,首先是属性名称,由原来的parameterClass改为了
parameterType,resultClass与变为了resultType,此外需要注意的是如果传入的参数类型为复杂对象,如Bean,则需要
在参数后面加上jdbcType属性来指定对应数据库表列的类型,如#{userName,
jdbcType=VARCHAR},如果传入的是基本类型,像int,long之类的,则不需要指定。
另外的变化就是执行方法上的变化,使用select, selectOne, selectList等替代了原来的方法,大家可以参考其API,我在些就不多说了。
iBATIS 3 试用手记 - The FUTURE - ITeye技术网站的更多相关文章
- Mysql 官方Memcached 插件初步试用感受 - schweigen - ITeye技术网站
Mysql 官方Memcached 插件初步试用感受 - schweigen - ITeye技术网站 Mysql 官方Memcached 插件初步试用感受
- ibatis参数传递小技巧 - 疯狂的菠菜 - ITeye技术网站
body { font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI ...
- TCL/Expect交互式自动化测试概要 - - ITeye技术网站
TCL/Expect交互式自动化测试概要 - - ITeye技术网站 expect是一种基于TCL,能与交互式程序进行"可程序化"会话的脚本语言,是一种可以提供"分支和嵌 ...
- 总结2015搭建日志,监控,ci,前端路由,数据平台,画的图与界面 - hugo - ITeye技术网站
总结2015搭建日志,监控,ci,前端路由,数据平台,画的图与界面 - hugo - ITeye技术网站 极分享:高质分享+专业互助=没有难做的软件+没有不得已的加班 极分享:高质分享+专业互助=没有 ...
- 阿里巴巴开源项目:分布式数据库同步系统otter(解决中美异地机房) - agapple - ITeye技术网站
阿里巴巴开源项目:分布式数据库同步系统otter(解决中美异地机房) - agapple - ITeye技术网站 阿里巴巴开源项目:分布式数据库同步系统otter(解决中美异地机房)
- python 内存泄露的诊断 - 独立思考 - ITeye技术网站
python 内存泄露的诊断 - 独立思考 - ITeye技术网站 python 内存泄露的诊断 博客分类: 编程语言: Python Python多线程Blog.net 对于一个用 python ...
- http_load安装与测试参数分析 - 追求自由自在的编程 - ITeye技术网站
http_load安装与测试参数分析 - 追求自由自在的编程 - ITeye技术网站 http_load -p 50 -s 120 urls
- JAVA中浅复制与深复制 - coolmist - ITeye技术网站
body{ font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI& ...
- Apache Shiro 使用手册(一)Shiro架构介绍 - kdboy - ITeye技术网站
转载 原文地址 http://kdboy.iteye.com/blog/1154644 一.什么是Shiro Apache Shiro是一个强大易用的Java安全框架,提供了认证.授权.加密和会话管理 ...
随机推荐
- WPF从我炫系列4---装饰控件的用法
这一节的讲解中,我将为大家介绍WPF装饰控件的用法,主要为大家讲解一下几个控件的用法. ScrollViewer滚动条控件 Border边框控件 ViewBox自由缩放控件 1. ScrollView ...
- PHP5.4 for Apache, php 5.4.0安装过程、方法、配置 ; Apache2.2支持php5.4的配置方法
那我们如何选择下载哪个版本的PHP呢?如果你是在windows下使用Apache+PHP的,请选择VC6版本:如果你是在windows下使用IIS+PHP的,请选择VC9版本. 二.如何选择PHP5. ...
- PHP生成静态页面详解
PHP生成静态页面详解 看到很多朋友在各个地方发帖问PHP生成静态文章系统的方法,以前曾做过这样一个系统,遂谈些看法,以供各位参考.好了,我们先回顾一些基本的概念. 一,PHP脚本与动态页面. PHP ...
- Loadrunner性能测试分类详(二)
一.基准测试 有基础的标准,这样能通过对比发现系统的不同点与变化. 1.可以再指定的标准下通过基准测试建立一个性能基准,这样以后当系统的环境.参数发生变化后,再进行一次相同标准下的测试,即可看出变化对 ...
- 快学Scala-第六章 对象
知识点: 1.单例对象 使用object语法结构达到静态方法和静态字段的目的,如下例,对象定义某个类的单个实例,包含想要的特性,对象的构造器在该对象第一次被使用时调用. object Account{ ...
- Job 逻辑执行图
General logical plan 典型的 Job 逻辑执行图如上所示,经过下面四个步骤可以得到最终执行结果: 从数据源(可以是本地 file,内存数据结构, HDFS,HBase 等)读取数据 ...
- 移动web开发常用属性
<!-- html5 doctype --> <!doctype html> <!-- lang 属性设置,中文页面尽量不要设置为 "en",这会开启 ...
- VirtualBox 复制vdi文件和修改vdi的uuid
1.复制vdi文件:VBoxManage clonehd 因为VirtualBox不允许注册重复的uuid,而每个vdi文件都有一个唯一的uuid.所以要想拷贝一份vdi文件再次在VBOX中注册,简单 ...
- System.Text.RegularExpressions.Regex
System.Text.RegularExpressions.Regex.IsMatch(string) 是否在指定字符串中找到制定项 System.Text.RegularExpressions.R ...
- 集合问题 离线+并查集 HDU 3938
题目大意:给你n个点,m条边,q个询问,每条边有一个val,每次询问也询问一个val,定义:这样条件的两个点(u,v),使得u->v的的价值就是所有的通路中的的最长的边最短.问满足这样的点对有几 ...