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. boost ASIO实例

    client端代码 #include <iostream> #include <boost/asio.hpp> #include <boost/bind.hpp> ...

  2. ASP.NET中使用Server.Transfer()方法在页间传值 实例

    以下代码在VS2008中测试通过 <%@ Page Language="C#" AutoEventWireup="true" CodeFile=" ...

  3. access的保留关键字

    access的保留关键字  -A     ADD     ALL     Alphanumeric     ALTER     AND     ANY     Application     AS   ...

  4. UML类图图示样例

    下图来自<大话设计模式>一书:

  5. zzuli 1919 数列划分

    题面: Description 晴天想把一个包含n个整数的序列a分成连续的若干段,且和最大的一段的值最小,但他有强迫症,分的段数不能超过m段,然后他就不会分了...他想问你这个分出来的和最大的一段的和 ...

  6. 【语法】修饰符 static extern const

    转载自:http://my.oschina.net/u/2560887/blog/552683 一.C语言中的static的作用 在C语言中,static的字面意思很容易把我们导入歧途,其实它的作用有 ...

  7. AI 人工智能 探索 (十)

    呼叫事件图形结构如下 蓝色代表 警察局 红色代表警察 黄色代表 死亡人 蓝色球代表呼救人 黑色代表 敌人 警察目标是 攻击 黑色人,但 路中 会碰到 黄色人,如果警察有 救人功能 则会先救人去医院再看 ...

  8. angularJS 判断

    判断语句: ng-switch on ng-switch-when ng-switch-when ng-if=”person.sex==1“ <ul> <li ng-repeat=” ...

  9. Git学习 -- 新建版本库

    创建目录 mkdir git cd git 2 初始化git init #会在目录下自动创建一个.git目录,用于跟踪管理版本库,不要修改 3 向版本库中添加文件git add readme.txt ...

  10. zendstudio的安装与配置

    <微信公众平台应用开发实战>第1章搭建开发环境和相关技术介绍,本章会先介绍微信公众平台的一些基本概念和公众平台的开发模式:然后讲解如何搭建开发环境—AppServ和zendstudio:然 ...