Your Ad Here
首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 编程语言 > Java > ibatis 开始之旅
【标  题】:ibatis 开始之旅
【关键字】:ibatis
【来  源】:http://www.blogjava.net/zyl/archive/2007/01/10/93001.html

ibatis 开始之旅

Your Ad Here ??? 用了很久hibernate ,突然想换个别的orm 工具,当然在orm领域中,hibernate是老大。看了一下ibatis,发现如果对于crud操作不是很多的系统来说,是个不错的选择,尤其是适合那些对sql和性能热衷的开发者。综合来说ibatis不能算orm工具,只能算个半成品。不过比起直接用jdbc写,那还是方便多了。主要的好处是分离了sql和代码,如果你想追求性能,那么sql是你很好的利器,当然ibatis的缓存也不错。比起hibernate,ibatis就简单多了,估计也就3天能够基本掌握了,这大大减少了学习成本。
??? 说了那么多废话,下面开始正题,通过一个简单的实例开始ibatis之旅,文章大部分参考网上的ibatis 开发指南一文。
??? 主要的jar:ibatis 2.3.0,spring 2.0.1,log4j 1.2.9,commons-logging 1.0.4,hsqldb 1.8.0
??? ibatis实例配置:
?
<sqlMapConfig>
<!-- 事务采用spring 管理 -->
? <!--
? <transactionManager type="JDBC" commitRequired="false">
??? <dataSource type="SIMPLE">
????? <property name="JDBC.Driver" value="org.hsqldb.jdbcDriver"/>
????? <property name="JDBC.ConnectionURL" value="jdbc:hsqldb:hsql://localhost/xdb"/>
????? <property name="JDBC.Username" value="sa"/>
????? <property name="JDBC.Password" value=""/>
??? </dataSource>
? </transactionManager>
-->
? <sqlMap resource="org/esoft/bo/xml/Account.xml"/>
</sqlMapConfig>?? ?

创建POJO对象:

?
package com.esoft.bo;

public class Account {

??? private String emailAddress;

??? private String firstName;

??? private int id;

??? private String lastName;

??? public String getEmailAddress() {
??????? return emailAddress;
??? }

??? public String getFirstName() {
??????? return firstName;
??? }

??? public int getId() {
??????? return id;
??? }

??? public String getLastName() {
??????? return lastName;
??? }

??? public void setEmailAddress(String emailAddress) {
??????? this.emailAddress = emailAddress;
??? }

??? public void setFirstName(String firstName) {
??????? this.firstName = firstName;
??? }

??? public void setId(int id) {
??????? this.id = id;
??? }

??? public void setLastName(String lastName) {
??????? this.lastName = lastName;
??? }

}?? ?

映射文件,感觉比较的麻烦。以后有机会的话一定自动生成此文件,尤其现在jpa当道。
?
<?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="Account">
?? ?
?? ?<!-- Use type aliases to avoid typing the full classname every time. -->
?? ?<typeAlias alias="Account" type="com.esoft.bo.Account"/>
?? ?
?? ?<!-- Result maps describe the mapping between the columns returned
?? ?from a query, and the class properties.? A result map isn't
?? ?necessary if the columns (or aliases) match to the properties
?? ?exactly. -->
?? ?<resultMap id="AccountResult" class="Account">
?? ??? ?<result property="id" column="ACC_ID"/>
?? ??? ?<result property="firstName" column="ACC_FIRST_NAME"/>
?? ??? ?<result property="lastName" column="ACC_LAST_NAME"/>
?? ??? ?<result property="emailAddress" column="ACC_EMAIL"/>
?? ?</resultMap>
?? ?
?? ?<!-- Select with no parameters using the result map for Account class. -->
?? ?<select id="selectAllAccounts" resultMap="AccountResult">
?? ??? ?select * from ACCOUNT
?? ??? ?</select>
?? ?
?? ?<select id="selectByName" resultMap="AccountResult" parameterClass="String">
?? ??? ?select * from Account where ACC_FIRST_NAME like #name#
?? ?</select>
?? ?
?? ?<!-- A simpler select example without the result map.? Note the
?? ?aliases to match the properties of the target result class. -->
?? ?<select id="selectAccountById" parameterClass="int" resultClass="Account">
?? ??? ?select ACC_ID as id, ACC_FIRST_NAME as firstName, ACC_LAST_NAME as lastName,
?? ??? ?ACC_EMAIL as emailAddress from ACCOUNT where ACC_ID = #id# </select>
?? ?
?? ?<!-- Insert example, using the Account parameter class -->
?? ?<insert id="insertAccount" parameterClass="Account"> insert into ACCOUNT ( ACC_ID,
?? ??? ?ACC_FIRST_NAME, ACC_LAST_NAME, ACC_EMAIL) values ( #id#, #firstName#,
?? ??? ?#lastName#, #emailAddress# ) </insert>
?? ?
?? ?<!-- Update example, using the Account parameter class -->
?? ?<update id="updateAccount" parameterClass="Account"> update ACCOUNT set
?? ??? ?ACC_FIRST_NAME = #firstName#, ACC_LAST_NAME = #lastName#, ACC_EMAIL =
?? ??? ?#emailAddress# where ACC_ID = #id# </update>
?? ?
?? ?<!-- Delete example, using an integer as the parameter class -->
?? ?<delete id="deleteAccountById" parameterClass="int"> delete from ACCOUNT where
?? ??? ?ACC_ID = #id# </delete>
?? ?
?? ?<delete id="clearAccount"> delete from ACCOUNT </delete>
?? ?
</sqlMap>?? ?

spring 配置:
?
...
<bean id="dataSource"
?? ??? ?class="org.springframework.jdbc.datasource.DriverManagerDataSource">
?? ??? ?<property name="driverClassName">
?? ??? ??? ?<value>${jdbc.driverClassName}</value>
?? ??? ?</property>
?? ??? ?<property name="url">
?? ??? ??? ?<value>${jdbc.url}</value>
?? ??? ?</property>
?? ??? ?<property name="username">
?? ??? ??? ?<value>${jdbc.username}</value>
?? ??? ?</property>
?? ??? ?<property name="password">
?? ??? ??? ?<value>${jdbc.password}</value>
?? ??? ?</property>
?? ?</bean>
???????? <bean id="transactionManager"
?? ??? ?class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
?? ??? ?<property name="dataSource" ref="dataSource"/>
?? ? </bean>
?? ?
?????? <bean id="sqlMapClient"
?? ??? ?class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
?? ??? ?<property name="dataSource" ref="dataSource"/>
?? ??? ?<property name="configLocation">
?? ??? ??? ?<value>SqlMapConfig.xml</value>
?? ??? ?</property>
????? </bean>
????? <bean id="accountDao" class="org.esoft.dao.AccountDaoImpl">
???????????? <property name="sqlMapClient" ref="sqlMapClient"/>
????? </bean>?? ?? ?
?? ?

主要的代码:
public class AccountDaoImpl
??????? extends SqlMapClientDaoSupport {
??? public PK save(Account obj) {
??????? return (PK) getSqlMapClientTemplate().insert("insertAccount", obj);
??? }
??? public void update(Accountobj) {
??????? getSqlMapClientTemplate().update("updateAccount", obj);
??? }
???? public void delete(Account obj) {
??????? getSqlMapClientTemplate().delete(
??????????????? "deleteAccountById",
??????????????? obj.getPk());
??? }
??? public Account get(PK primaryKey) {
??????? return (Account) getSqlMapClientTemplate().queryForObject(
??????????????? "selectAccountById",
??????????????? primaryKey);
??? }
}


今天被野马郁闷了:【上一篇】
JDK6.0的新特性:轻量级Http Server:【下一篇】
【相关文章】
  • iBatis的JpetStore示例中MVC机制实现的研究,BeanAction,BaseBean
  • webwork2.2.4+ibatis2.2+lucene+spring+velocity 编写的bt服务器(提供源代码)
  • ibatis如何支持clob 和blob
  • iBatis框架batch处理优化
  • iBATIS for Java 2.3.0 Released (译)
  • 错误代码:WLTC0032W ibatis作为持久层,websphere 报告连接没有提交错误,问题的解决以及产生的原因
  • 使ibatis支持hibernate式的物理分页
  • ibatisnet使用心得
  • ibatis + dbunit 应用实例
  • 基于Castle+IBatisNet+Castle.MVC的ASP.NET构架
  • 【随机文章】
  • 使用vs2005(vc8)编译log4cpp-0.3.5rc3
  • 所思
  • Linux 内核的编译
  • Photoshop无痕迹拼接美女图像(2)
  • 获取时间区间列表
  • eclipse安装之前有个JDK——我晕!
  • 第一个Qt程序
  • oracle学习笔记(二)------函数
  • [算法题]约瑟夫(josephus)环的php玩法
  • 企业网络防范Serv-U的漏洞
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 bbb软讯网络 All Rigths Reserved.