iBATIS 3 试用手记 - The FUTURE - ITeye技术网站

时间:2023-03-08 17:26:07

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技术网站

前记:本来打算去****写这篇文章的,结果****的服务器又出问题了,登录了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,我在些就不多说了。