`

[顶]iBATIS框架-转载

阅读更多
iBATIS框架理论学习篇
 
2008-02-22 作者:kimsoft 来源:csdn
 
1、什么是iBATIS
1.1作者
    Clinton Begin,很牛X的名字
1.2背景故事
    Clinton Begin于2001年启动iBATIS项目,最先的焦点是发展密码软件解决方案。第一个iBATIS产品是Secrets,是一个象PGP的个人数据加密器和签名工具。Secrets完全用Java编写并在一个开源许可下发布。
    在发布Secrets Java版后不久,iBATIS项目陷入困境转而关注Web和其它internet相关的技术,在接下来的一年中,两个有趣的软件开发完成,包括Axle web 框架,一个JSP可选工具。Lookout邮件客户端差不多完成90%在 being set on the back burner之前,同时更多有趣的挑战接踵而来…
1.3加入Pet Store“基准”
    在2002年早些时候,Microsoft发布一个纸面文档声称.NET有10倍于J2EE的速度和4倍多的生产效率。事实是这只是简单的而不是典型的。iBATIS项目在2002年7月1日快速回应,JPetStore 1.0发布了。基于相同的Pet Store需求,JPetStore证明了Java不仅比.NET更有生产效率,同时比Microsoft的实现拥有更好的架构
    JPetStore利用有趣的持久层迅速地引起了开源社区的关注。在JPetStore发布不久,对SQL Maps和DAO框架的问题和要求使大家知道的iBATIS Database Layer产生了。iBATIS Database Layer包含两个被打包在一起的框架:SQL Maps和DAO。
       如今iBATIS项目重点关注持久层框架并以SQL Maps 和 Data Access Objects (DAO)著称。JPetStore继续作为这些框架典型应用的官方示例程序。
1. 4“iBATIS”是什么意思
    现在,单词“ibatis”和SQL Maps 与 DAO 框架同义――就象“xerox”『施乐复印机(商标名称),译者著』和“photocopy” 『影印,译者著』同义。
    回想起iBATIS来源于密码系统软件。iBATIS实际上用两个单词的组合来命名:“internet” 和 “abatis”。
Reference.com 如此定义 abatis:
ab-a-tis
n. pl. ab-a-tis (-tz) or ab-a-tis-es (-t-sz)
将伐倒的树木树枝相互交叉,放置,有时削尖,面对敌人一种防御障碍物。
将“Internet”中象征性的“i”和abatis中的“batis”组合所以暗示了抵御Internet的意思。-in the case of iBATIS它是Secrets提供的关于密码防御的东西。
所以iBATIS正确的写法应该是:iBATIS
1.5 iBATIS怎么念?
    因为我们发明了这个单词,我们以自己喜欢的读音念它
    我们这样读:eye-BAT-iss
    但是按照传统,它可能应该被读作:eye-BATE-iss,但就象我们说的,我们创造了它。
1.6许可(License)
    iBATIS的源码和文档在Apache License 2.0许可下发布。
    以上部分文字翻译自http://ibatis.apache.org,不妥之处,请指正。
1.7版本情况
iBATIS有for Java,for .NET,for Ruby三个语言的版本,以下主要讨论for Java版,也是最重要的版本。
      
       关于版本,看下面一段英文:
iBATIS 3.0 Whiteboard
January 11th, 2007 marks the 3rd Anniversary(周年纪念) of the iBATIS 2.0. It has served the community well for three years, but times change. The year 2006 was full of innovation(改革、创新) and shifts in technology and mindset. The impact(冲击,碰撞) of frameworks like Ruby on Rails cannot be ignored. The industry has noticed and finally invested in lightweight frameworks, agile principles and simple solutions first.
最新版本:iBATIS2.3.0 build667,非常稳定。
1.8 典型用户
    MySpace.com - A place for friends
    …
2、当前Java平台下的一些DAO框架
2.1Hibernate
    应该是最好的ORM框架,ORM:Object Relation Mapping,即对象关系映射。
    了解不是很多,不多作评价。不过应该是很好的框架,EJB3里的JPA所说差不多就是简化版的Hibernate,侧重于对象模型设计,系统设计中数据库设计让位于对象模型设计。
2.2 Apache JDO
    Apache组织的一个比较早的开源框架,影响力不大。
2.3 Spring DAO(Template)
    Spring DAO对JDBC进行了封装
    用得最多的还是各种Template:
JdbcTemplate
       HibernateTemplate
       SqlMapTemplate
       JdoTemplate
       JpaTemplate
2.4 JDBC
    对数据库操作最原始的办法,也是根本,无所不能的。
2.5 iBATIS
    又称为SQL Map或 Data Map(2.0以后)
    SQL Mapping framework 更准确地说是 SQL Mapping tool,是一个SQL语句映射的框架(工具)
2.5.1介绍(Introduction)
    The iBATIS Data Mapper framework will help you to significantly(意味深长地,值得注目地) reduce the amount of Java code that you normally need to access a relational database. iBATIS simply maps(此处应译作映射) JavaBeans to SQL statements using a very simple XML descriptor. Simplicity(简单) is the key advantage(优势,有利条件) of iBATIS over other frameworks and object relational mapping tools. To use the iBATIS Data Mapper you need only be familiar with JavaBeans, XML and SQL. There is very little else to learn. There is no complex scheme required to join tables or execute complex queries. Using Data Mapper you have the full power of real SQL at your fingertips.
2.5.2 概念(Concept)
    The iBATIS Data Mapper API allows programmers to easily map JavaBeans objects to PreparedStatement parameters and ResultSets. The philosophy behind Data Mapper is simple: provide a simple framework to provide 80% of JDBC functionality using only 20% of the code.
3、学习iBATIS
3.1 一个简单的iBATIS原型
    SELECT COUNT(*) FORM USER
    讨论:
一、用JDBC直接在页面中如何写程序
二、多个JSP文件同时出现此功能时
三、出现不同的版本,SELECT COUNT(*) FORM USER WHERE USER_TYPE=1 AND IS_DEL=0…
四、讨论各个版本的输入输出参数
3.2 iBATIS 藏宝图
    讨论:iBATIS各种输入输出参数
3.3 iBATIS API(Spring SqlMapClientTemplate API)
    第一个参数statementName都是必须的,其它参数都是可选的
    增、删、改
    public Object insert(String statementName, Object parameterObject)
    public int update(String statementName, Object parameterObject)
    public int delete(String statementName, Object parameterObject)
    查询
    public Object queryForObject(String statementName, Object parameterObject)
    public List queryForList(String statementName, Object parameterObject)
    void queryWithRowHandler (String statementName, Object parameterObject, RowHandler rowHandler) 不常用
    public PaginatedList queryForPaginatedList(String statementName, Object parameterObject, int pageSize) 性能问题,已经被废弃(deprecated)
    public Map queryForMap (String statementName, Object parameterObject, String keyProperty, String valueProperty)
    没有了…
4.使用iBATIS的好处
    4.1简单,学习曲线低
    4.2使用公司投资回报率提高,一般是5到8倍
    4.3喝着Java(咖啡)写Java代码,节省大量编码维护时间
    4.4提高员工土气
    4.5性能好,灵活性强
    4.6集中管理后,DAO的实现简单,使用开发人员更专心于业务逻辑的处理。修改时不用修改Java代码。
    4.7使用preparedStatement,性能,安全性提高,代码简洁。
    4.8配置log4j在调试时输出SQL语句和返回记录集,漫天飞舞的System.out.print不见了。
    4.9不用担心,连接、记录集的关闭
    4.10缓存,讨论:很小变动的基础数据。
    4.11写程序时很自然地优化SQL,减少数据库的连接次数和操作次数
    讨论:
       一级分类1
              二级分类1.1 二级分类1.2
       一级分类2
              二级分类2.1 二级分类2.2
       一级分类3
              二级分类3.1 二级分类3.2
    讨论遗漏的好处。
    讨论缺点
5.iBATIS的配置文件
有两种:
5.1 sqlMapConfig文件
    只有一个,集中管理配置文件的“中央文件”
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMapConfig
       PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
       "http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
 
<sqlMapConfig>
    <settings cacheModelsEnabled="true"
              enhancementEnabled="true"
              lazyLoadingEnabled="true"
useStatementNamespaces="false"
statementCachingEnabled="true"
             classInfoCacheEnabled="true" />
    <sqlMap resource="com/ahtec/ssi/dao/ibatis/maps/SysModule.xml" />
    <sqlMap resource="com/ahtec/ssi/dao/ibatis/maps/EudProject.xml" />
</sqlMapConfig>
5.2 各个SQL Map 文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd">
 
<sqlMap namespace="dv_bbslink">
<typeAlias alias="link" type="com.ahtec.ssi.domain.Link" />
 
<cacheModel id="oneDayCache" type="OSCACHE">
<flushInterval hours="24" />
<flushOnExecute statement="insertLink" />
<flushOnExecute statement="updateLinkByPrimaryKey" />
<flushOnExecute statement="updateLinkByPrimaryKeySelective" />
<flushOnExecute statement="deleteLinkByPrimaryKey" />
<flushOnExecute statement="deleteLink" />
</cacheModel>
 
<resultMap class="com.ahtec.ssi.domain.Link" id="LinkResult">
<result column="id" jdbcType="INTEGER" property="id" />
<result column="boardname" jdbcType="VARCHAR" property="boardname" />
<result column="readme" jdbcType="VARCHAR" property="readme" />
<result column="url" jdbcType="VARCHAR" property="url" />
<result column="logo" jdbcType="VARCHAR" property="logo" />
<result column="islogo" jdbcType="INTEGER" property="islogo" />
</resultMap>
 
<sql id="whereClause">
<isNotNull property="map.boardname"> and boardname like #map.boardname#</isNotNull>
<isNotNull property="islogo"> and islogo=#islogo#</isNotNull>
</sql>
 
<select id="selectLinkByPrimaryKey" parameterClass="link" resultMap="LinkResult" cacheModel="oneDayCache">
select id, boardname, readme, url, logo, islogo from dv_bbslink where id = #id:INTEGER#
</select>
 
<select id="selectLinkList" parameterClass="link" resultMap="LinkResult">
select id, boardname, readme, url, logo, islogo from dv_bbslink where 1=1
<isParameterPresent>
<include refid="whereClause" />
</isParameterPresent>
<isNotNull property="row.count">limit 0, #row.count#</isNotNull>
</select>
 
<select id="selectLinkCount" parameterClass="link" resultClass="int">
select count(*) from dv_bbslink where 1=1
<isParameterPresent>
<include refid="whereClause" />
</isParameterPresent>
</select>
 
<select id="selectLinkPaginatedList" parameterClass="link" resultMap="LinkResult">
select id, boardname, readme, url, logo, islogo from dv_bbslink where 1=1
<isParameterPresent>
<include refid="whereClause" />
</isParameterPresent>
<isNotNull property="row.count">limit #row.first#, #row.count#</isNotNull>
</select>
 
<insert id="insertLink" parameterClass="link">
insert into dv_bbslink (boardname, readme, url, logo, islogo) values (
#boardname#,
#readme#,
#url#,
#logo#,
#islogo#)
<selectKey keyProperty="id" resultClass="java.lang.Integer">
SELECT LAST_INSERT_ID()
</selectKey>
</insert>
 
<update id="updateLinkByPrimaryKey" parameterClass="link">
update dv_bbslink set
boardname = #boardname#,
readme = #readme#,
url = #url#,
logo =#logo#,
islogo = #islogo#
where id = #id:INTEGER#
</update>
 
<update id="updateLinkByPrimaryKeySelective" parameterClass="link">
</update>
 
<delete id="deleteLinkByPrimaryKey" parameterClass="link">
delete from dv_bbslink where id = #id#
</delete>
 
<delete id="deleteLink" parameterClass="link">
delete from dv_bbslink
<include refid="whereClause" />
</delete>
 
</sqlMap>
6、实例(MySQL)
    一个论坛链接的表,分为LOGO链接,文字链接,有如下字段
6.1 数据库表(dv_bbslink
Id                  自增ID(INTEGER
Boardname     论坛名称(VARCHAR
Readme          说明(VARCHAR
url                 链接地址(VARCHAR
logo               LOGO图片地址(VARCHAR
islogo             是否是LOGO链接(INTEGER
6.2 写一个Domain(POJO,Model)
Link.java
    private Integer id;
    private String boardname;
    private String readme;
    private String url;
    private String logo;
    private Integer islogo;
   //getters and setters
6.3 写接口(LinkDao)
    常用接口
    根据业务逻辑生成特定的接口
 
LinkDao.java
publicinterface LinkDao {
    Integer insertLink(Link link);
    int updateLinkByPrimaryKey(Link link);
    int updateLinkByPrimaryKeySelective(Link link);
    int deleteLink(Link link);
    int deleteLinkByPrimaryKey(Integer id);
    List selectLinkList(Link link);
    List selectLinkPaginatedList(Link link);
    Link selectLinkByPrimaryKey(Integer id);
    Integer selectLinkCount(Link link);
}
6.4 写实现(LinkDaoSqlMapImpl)
public class LinkDaoSqlMapImpl extends SqlMapClientDaoSupport implements LinkDao {
public int deleteLink(Link link) {
int rows = super.getSqlMapClientTemplate().delete("deleteLink", link);
return rows;
}
public Integer insertLink(Link link) {
Object newKey = super.getSqlMapClientTemplate().insert("insertLink", link);
return (Integer) newKey;
}
public Link selectLinkByPrimaryKey(Integer id) {
Link key = new Link();
key.setId(id);
Link record = (Link) super.getSqlMapClientTemplate().queryForObject("selectLinkByPrimaryKey", key);
return record;
}
public Integer selectLinkCount(Link link) {
return (Integer) super.getSqlMapClientTemplate().queryForObject("selectLinkCount", link);
}
public List selectLinkList(Link link) {
return (List) super.getSqlMapClientTemplate().queryForList("selectLinkList", link);
}
public List selectLinkPaginatedList(Link link) {
return (List) super.getSqlMapClientTemplate().queryForList("selectLinkPaginatedList", link);
}
public int updateLinkByPrimaryKey(Link link) {
int rows = getSqlMapClientTemplate().update("updateLinkByPrimaryKey", link);
return rows;
}
public int updateLinkByPrimaryKeySelective(Link link) {
int rows = getSqlMapClientTemplate().update("updateLinkByPrimaryKeySelective", link);
return rows;
}
public int deleteLinkByPrimaryKey(Integer id) {
int rows = super.getSqlMapClientTemplate().delete("deleteLinkByPrimaryKey", id);
return rows;
}
}
6.5配置SQL Map
    分析SQL Map 文件
6.7 配置 Spring Struts,写Action, View,OK。
    此讨论在本文范围外
7.其它
7.1 学习资源
    wiki
    faq
    maillist
    jpetstore 5.0
    Spring包里也有个JPetStore 建议学习
7.2 自动化工具
    Abator(http://ibatis.apache.org
    容易使用,三步:
    一、下载eclipse plug in
    二、新建并配置一个abatorConfig.xml
    三、生成model,sqlmap, dao和daoImpl
    最后一步,加工它生成的代码。
    再一步,喝咖啡
8 名人名言
if you are starting a new project and you're in full control of your object model and database design, Hibernate is a good choice of O/R tool.
Clinton Begin
 
if you are accessing any 3rd party databases (e.g. vendor supplied), or you're working with a legacy database, or even just a really poorly designed database, then an O/R mapper might not be capable of handling the situation. That's were  an SQL Mapper comes in handy
Clinton Begin
 
In the end the choice is yours and nobody can tell you what the right one is, Trust only yourself, draw your own conclusions and do lots of testing!
Clinton Begi
分享到:
评论

相关推荐

    ibatis-sqlmap-2.jar.zip

    iBatis-SQLMap 2是iBatis框架的一个重要组成部分,它提供了SQL映射文件和Java对象之间的映射机制,使得开发者可以更方便地处理数据库操作。`ibatis-sqlmap-2.jar`是该版本的核心库文件,包含了iBatis框架的所有功能...

    apache开源项目源码ibatis-3-core-src-3.0.0.227(ibatis框架java源程序)

    apache开源项目源码ibatis-3-core-src-3.0.0.227 ibatis框架java源程序 spring,struts,hibernate,ibatis,框架源码 各种ibatis框架应用源码,你会从中得到意想不到的效果! apache开源组织开发的开源项目源码,其...

    ibatis-sqlmap-2.3.4.726-sources.jar.zip_birth84v_cutting1v2_ibat

    《深入解析iBatis-SQLMap 2.3.4.726源码》 在Java开发领域,iBatis作为一个优秀的持久层框架,深受广大开发者喜爱。它将SQL语句与Java代码分离,提高了代码的可读性和可维护性。本篇将围绕iBatis-SQLMap 2.3.4.726...

    ibatis-3-core-3.0.0.242.jar.zip

    ibatis-3-core-3.0.0.242.jar.zipibatis-3-core-3.0.0.242.jar.zipibatis-3-core-3.0.0.242.jar.zipibatis-3-core-3.0.0.242.jar.zipibatis-3-core-3.0.0.242.jar.zip

    ibatis-2.3.4.726.jar,ibatis-2.3.0.677.jar,ibatis-2.3.3.720.jar下载

    这里提到的是iBATIS的三个不同版本的jar包:ibatis-2.3.4.726.jar、ibatis-2.3.0.677.jar以及ibatis-2.3.3.720.jar。 首先,让我们深入了解iBATIS的核心概念和功能: 1. **SQL Map配置**:iBATIS的核心是SQL Map...

    ibatis-core-3.0.jar.zip

    而`ibatis-core-3.0.jar`则是iBatis框架的核心库,包含了iBatis运行所需的全部类和资源。本文将深入探讨这个核心库的内部结构和主要功能,帮助开发者更好地理解和使用iBatis。 一、iBatis框架概述 iBatis源于...

    iBATIS-SqlMaps-2-Tutorial_cn.pdf

    iBATIS-SqlMaps-2-Tutorial_cniBATIS-SqlMaps-2-Tutorial_cn.pdf.pdfiBATIS-SqlMaps-2-Tutorial_cn.pdfiBATIS-SqlMaps-2-Tutorial_cn.pdf

    ibatis-3-core-3.0.0.200

    ibatis-3-core-3.0.0.200

    ibatis-3-core-3.0.0.242.zip

    ibatis-3-core-3.0.0.242.zip ibatis-3-core-3.0.0.242.zip ibatis-3-core-3.0.0.242.zip ibatis-3-core-3.0.0.242.zip

    ibatis-common.jar包

    而ibatis-common.jar包则是Ibatis框架的重要组成部分,它提供了诸多实用功能,帮助开发者更高效地进行数据访问操作。本文将详细探讨ibatis-common.jar包的核心功能及其实现原理。 首先,ibatis-common-1.3.1.jar与...

    ibatis 缓存 - 24小时学习网.mht

    ibatis 缓存 - 24小时学习网ibatis 缓存 - 24小时学习网ibatis 缓存 - 24小时学习网ibatis 缓存 - 24小时学习网ibatis 缓存 - 24小时学习网

    ibatis-common包

    ibatis-common包ibatis-common包ibatis-common包ibatis-common包ibatis-common包ibatis-common包ibatis-common包ibatis-common包ibatis-common包ibatis-common包ibatis-common包

    ibatis-sqlmap-2.jar

    ibatis-sqlmap-2.jar 对数据库进行操作的jar包 很方便使用

    ibatis2-dao-2.1.6.589.jar

    ibatis2-dao-2.1.6.589.jar文件,ibatis实现控制层

    ibatis--dao的应用

    标题 "ibatis--dao的应用" 暗示了本文将探讨如何在Java应用程序中使用iBatis框架进行数据访问对象(DAO)的操作。iBatis是一个轻量级的持久层框架,它允许开发者将SQL语句与Java代码分离,提高了代码的可维护性和...

    iBATIS-DAO-2.3.4.726.rar_com.ibatis.dao_iBATIS dao 2_iBatis DAO_

    iBATIS DAO是Java开发中的一个持久层框架,它提供了数据访问对象(DAO)的实现,使得数据库操作更加简单和灵活。在这个2.3.4.726版本的源码中,我们可以深入理解iBATIS DAO的工作原理,并通过添加注释来帮助我们更好...

    iBATIS-DAO-2

    例如,对于某个特定的DAO,可以有两个实现版本:一个使用iBATIS SQL Maps框架将对象持久化到数据库,另一个则使用Hibernate框架。或者,有一个DAO为另一个DAO提供缓存服务。根据具体场景(如数据库性能受限或内存...

    ibatis-sqlmap_2.3.4_2.jar两个版本的jar包

    Ibatis-SqlMap是早期非常流行的一款Java持久层框架,由Cody Aravena创建,后被Apache基金会接管并更名为MyBatis。本篇将详细阐述Ibatis-SqlMap 2.3.4和2两个版本的主要知识点。 一、Ibatis-SqlMap简介 Ibatis-...

    ibatis-3-core-3.0.0.227.z

    首先,"ibatis-3-core-3.0.0.227.jar"是iBatis的核心库文件,包含了iBatis框架的主要功能。这个JAR文件包含了所有必要的类和资源,使得开发者可以在项目中直接引用,实现数据库操作与业务逻辑的分离,提高代码的可...

    ibatis-common-2.jar

    ibatis-common-2.jar...........

Global site tag (gtag.js) - Google Analytics