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安全框架,提供了认证.授权.加密和会话管理 ...
随机推荐
- ant 配置expdp and impap
+ 执行步骤: ant -f 1_exp_prod.xml copy file from prod to uat (maunule) ant -f 3_imp_uat.xml 附件: 1.1_exp ...
- oracle sqlplus @@用法
本文章已收录于: .embody { padding: 10px 10px 10px; margin: 0 -20px; border-bottom: solid 1px #ededed } .e ...
- win8.1下安装ubuntu 14.0 4LTS
1.前奏 电脑上已经安装了win8.1系统 2.准备工作 关闭win8.1的快速启动 步骤: 控制面板->电源选项->选择电源按钮的功能->更改不可用的设置,然后把"启用快 ...
- MSG 结构
MSG 消息结构 在 Windows 程序中,消息是由 MSG 结构体来表示的. 结构原型: typedef struct tagMSG { HWND hwnd; UINT message; ...
- MyBatis中Like语句使用方式
oracle数据库: SELECT * FROM user WHERE name like CONCAT('%',#{name},'%') 或 SELECT * FROM user WHERE nam ...
- 美团,点评,澎湃等APP的启示
事先声明,因个人能力尚浅,文章若有不足之处,望留言指出,也欢迎成为好朋友. 本来想打算写团购类APP的竞品分析,但是发现不管是天天果园这样生鲜APP,还是澎湃这样的新闻资讯APP,思路差不多,都是: ...
- Element type "bean" must be followed by either attribute specifications, ">" or "/>".
在这里其他内容就省了,错在,<bean id="bpcsU1gblDAO"class="dao.jk.bpcs.impl.BpcsU1gblDaoImpl" ...
- isinstance使用方法
#!/usr/bin/python2.7 def displayNumType(num): print num, 'is', if isinstance(num,(int, long ...
- 用python计算md5,sha1,crc32
Linux下计算md5sum,sha1sum,crc: 命令 输出 $md5sum hello f19dd746bc6ab0f0155808c388be8ff0 hello $sha1sum hel ...
- 快速部署Python应用:Nginx+uWSGI配置详解
在PHP里,最方便的就是deployment了,只要把php文件丢到支持PHP的路径里面,然后访问那个路径就能使用了:无论给主机添加多少PHP应用,只要把目录改好就没你的事了,完全不用关心php-cg ...