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作了很大改动,看来不假,呵呵,废话少说,见下。

  首先是初始化的改变:

  1. Reader reader = Resources.getResourceAsReader(CONFIG_FILE_PATH);  
  2. SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader, "development_oracle");  
  3. 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文件。

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE configuration   
  3.   PUBLIC "-//ibatis.apache.org//DTD Config 3.0//EN" "http://ibatis.apache.org/dtd/ibatis-3-config.dtd">  
  4.   
  5. <configuration>  
  6.     <properties resource="conf/database.properties" />  
  7.     <environments default="development">  
  8.         <environment id="development">  
  9.             <transactionManager type="JDBC" />  
  10.             <dataSource type="POOLED">  
  11.                 <property name="driver" value="${database.driver}"/>  
  12.                 <property name="url" value="${database.url}"/>  
  13.                 <property name="username" value="${database.user}"/>a  
  14.                 <property name="password" value="${database.password}"/>  
  15.             </dataSource>  
  16.         </environment>  
  17.           
  18.         <environment id="development_oracle">  
  19.             <transactionManager type="JDBC" />  
  20.             <dataSource type="POOLED">  
  21.                 <property name="driver" value="${oracle.database.driver}"/>  
  22.                 <property name="url" value="${oracle.database.url}"/>  
  23.                 <property name="username" value="${oracle.database.user}"/>  
  24.                 <property name="password" value="${oracle.database.password}"/>  
  25.             </dataSource>  
  26.         </environment>      
  27.     </environments>  
  28.       
  29.     <mappers>  
  30.         <mapper resource="conf/NewsNotice.xml"/>  
  31.     </mappers>  
  32. </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内容:

  1. database.driver = org.gjt.mm.mysql.Driver  
  2. database.url = jdbc:mysql://localhost:3306/test  
  3. database.user = root  
  4. database.password = root  
  5.   
  6. oracle.database.driver = oracle.jdbc.driver.OracleDriver  
  7. oracle.database.url = jdbc:oracle:thin:@locahost:1521:ORCL2  
  8. oracle.database.user = Tester  
  9. 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改动最多的地方。

 

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE mapper    
  3.     PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN" "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">  
  4.   
  5. <mapper namespace="NewsNotice">  
  6.     <resultMap type="org.newsnotice.domain.NewsNoticeModel" id="resultMap-getNewsNotice1">  
  7.         <id column="NN_ID" property="id" />  
  8.         <result column="CATEGORY" property="category" />  
  9.         <result column="SUBJECT" property="subject" />  
  10.         <result column="POSTED_DATE" property="postedDate" />  
  11.         <result column="EXPIRY_DATE" property="expiryDate" />  
  12.         <result column="ALERT" property="alert" />  
  13.         <result column="EMAIL_ALERT" property="emailAlert" />  
  14.         <result column="AUDIENCE" property="audience" />  
  15.         <result column="FILTER" property="filter" />  
  16.         <result column="FILTER_VALUE" property="filterValue" />  
  17.         <result column="SUB_FILTER_VALUE" property="subFilterValue" />  
  18.         <result column="EXCLUDE_USER_ID" property="excludeUserId" />  
  19.         <result column="WF_DEPARTMENT" property="department" />  
  20.         <result column="WF_STATUS" property="status" />  
  21.         <result column="WF_NOTES" property="notes" />  
  22.         <result column="DEFUNCT_IND" property="defunctInd" />  
  23.         <result column="APPROVER" property="approver" />  
  24.         <association property="newsNoticeContent" column="CONTENT_ID" javaType="org.newsnotice.domain.NewsNoticeContentModel">  
  25.             <id column="CONTENT_ID" property="id" />  
  26.             <result column="PARENT_NN_ID" property="parentId" />  
  27.             <result column="CONTENT" property="content" />  
  28.         </association>  
  29.         <collection property="newsNoticeMsgBoxList" ofType="org.newsnotice.domain.NewsNoticeMsgBoxModel" >  
  30.             <id column="MSG_BOX_ID" property="id"/>  
  31.             <result column="USER_ID" property="userId" />  
  32.             <result column="MSG_BOX_NN_ID" property="nnId" />  
  33.             <result column="FOLDER" property="folder" />  
  34.             <result column="READ" property="read" />            
  35.             <result column="READ_ON" property="readOn" />  
  36.             <result column="MSG_BOX_DEFUNCT_IND" property="defunctInd" />  
  37.             <result column="MSG_BOX_PI_NO" property="piNo" />  
  38.         </collection>  
  39.     </resultMap>  
  40.       
  41.     <select id="getNewsNotice" parameterType="org.newsnotice.domain.NewsNoticeModel" resultMap="resultMap-getNewsNotice1" >  
  42.         SELECT A.NN_ID, A.CATEGORY, A.SUBJECT, A.POSTED_DATE, A.EXPIRY_DATE, A.ALERT, A.EMAIL_ALERT, A.AUDIENCE,  
  43.             A.FILTER, A.FILTER_VALUE, A.SUB_FILTER_VALUE, A.EXCLUDE_USER_ID, A.WF_DEPARTMENT, A.WF_STATUS, A.WF_NOTES,  
  44.             A.DEFUNCT_IND, A.APPROVER, B.ID CONTENT_ID, B.PARENT_NN_ID, B.CONTENT, C.ID MSG_BOX_ID, C.USER_ID,   
  45.             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  
  46.         FROM NN_MSTR A, NN_CONTENT B, NN_MSG_BOX C  
  47.         WHERE A.NN_ID = B.PARENT_NN_ID  
  48.         AND A.NN_ID = C.NN_ID  
  49.         <if test="id != null">  
  50.             AND A.NN_ID = #{id}  
  51.         </if>  
  52.         <if test="category != null">  
  53.             AND A.CATEGORY = #{category}  
  54.         </if>  
  55.         <if test="status != null">  
  56.             AND A.WF_STATUS = #{status}  
  57.         </if>  
  58.     </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技术网站的更多相关文章

  1. Mysql 官方Memcached 插件初步试用感受 - schweigen - ITeye技术网站

    Mysql 官方Memcached 插件初步试用感受 - schweigen - ITeye技术网站 Mysql 官方Memcached 插件初步试用感受

  2. ibatis参数传递小技巧 - 疯狂的菠菜 - ITeye技术网站

    body { font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI ...

  3. TCL/Expect交互式自动化测试概要 - - ITeye技术网站

    TCL/Expect交互式自动化测试概要 - - ITeye技术网站 expect是一种基于TCL,能与交互式程序进行"可程序化"会话的脚本语言,是一种可以提供"分支和嵌 ...

  4. 总结2015搭建日志,监控,ci,前端路由,数据平台,画的图与界面 - hugo - ITeye技术网站

    总结2015搭建日志,监控,ci,前端路由,数据平台,画的图与界面 - hugo - ITeye技术网站 极分享:高质分享+专业互助=没有难做的软件+没有不得已的加班 极分享:高质分享+专业互助=没有 ...

  5. 阿里巴巴开源项目:分布式数据库同步系统otter(解决中美异地机房) - agapple - ITeye技术网站

    阿里巴巴开源项目:分布式数据库同步系统otter(解决中美异地机房) - agapple - ITeye技术网站 阿里巴巴开源项目:分布式数据库同步系统otter(解决中美异地机房)

  6. python 内存泄露的诊断 - 独立思考 - ITeye技术网站

    python 内存泄露的诊断 - 独立思考 - ITeye技术网站 python 内存泄露的诊断 博客分类: 编程语言: Python Python多线程Blog.net  对于一个用 python ...

  7. http_load安装与测试参数分析 - 追求自由自在的编程 - ITeye技术网站

    http_load安装与测试参数分析 - 追求自由自在的编程 - ITeye技术网站 http_load -p 50 -s 120 urls

  8. JAVA中浅复制与深复制 - coolmist - ITeye技术网站

    body{ font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI& ...

  9. Apache Shiro 使用手册(一)Shiro架构介绍 - kdboy - ITeye技术网站

    转载 原文地址 http://kdboy.iteye.com/blog/1154644 一.什么是Shiro Apache Shiro是一个强大易用的Java安全框架,提供了认证.授权.加密和会话管理 ...

随机推荐

  1. iOS真机测试中出现dyld`dyld_fatal_error错误

    最近进入一家新公司,接手了一个之前由外包公司承接的项目.首先吐槽一下项目质量,哎毕竟也憋了很久了. 1.上手项目是打不开的,所有framework静态库全体飘红,一编译七八十错误.最终是偷懒还是什么就 ...

  2. Tree Cutting

    Tree Cutting Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/131072 K (Java/Others) Prob ...

  3. Hibernate 系列教程1-枚举单例类

    你还在为不知道怎样正确使用Hibernate而纠结吗 你还在为不知道怎样配置映射文件而郁闷吗 枚举单例(Enum Singleton) 是实现单例模式的一种方式而已,不过写法简单,创建枚举默认也是线程 ...

  4. CodeForces 719A Vitya in the Countryside 思维题

    题目大意:月亮从0到15,15下面是0.循环往复.给出n个数字,如果下一个数字大于第n个数字输出UP,小于输出DOWN,无法确定输出-1. 题目思路:给出0则一定是UP,给出15一定是DOWN,给出其 ...

  5. linux 进程命令

    Linux中的ps命令是Process Status的缩写.ps命令用来列出系统中当前运行的那些进程.ps命令列出的是当前那些进程的快照,就是执行ps命令的那个时刻的那些进程,如果想要动态的显示进程信 ...

  6. 【Python初学】深copy&浅copy

    在python中,对象赋值实际上是对象的引用.当创建一个对象,然后把它赋给另一个变量的时候,python并没有拷贝这个对象,而只是拷贝了这个对象的引用. 1. copy.copy 浅拷贝 只拷贝父对象 ...

  7. java数据结构之有序表查找

    这篇文章是关于有序表的查找,主要包括了顺序查找的优化用法.折半查找.插值查找.斐波那契查找: 顺序优化查找:效率极为底下,但是算法简单,适用于小型数据查找: 折半查找:又称为二分查找,它是从查找表的中 ...

  8. 手游 ui布局

    最近突然有做手游的冲动,其实也是酝酿好久了. 之前的demo 让我想做一款 策略类的 战争游戏,有点像 部落战争: 那么,让我最最头疼的就是 ui设计了. 国内大部分 游戏的 ui  都是 用各种各样 ...

  9. 使用U盘在Mac机上装win8.1系统

    1.首先要准备一个8G的U盘,用苹果机格式化为FAT格式.注意:U盘格式化之前要对U盘里的文件备份,U盘格式化后,里边的内容会清空. 2.下载原版win8.1系统,不要下载ghost版,http:// ...

  10. sphinx query multiple indexes in php

    http://stackoverflow.com/questions/17494784/searching-a-particular-index-using-sphinx-from-multiple- ...