Ibatis2.0使用说明(一)——入门实例篇(转载)

来源:百度文库 编辑:神马文学网 时间:2024/04/29 05:13:10
本文章将从一个Ibatis的具体示例,帮助你快速了解IBatis框架。一个简单的IBatis应用包含以下基本步骤:一、 配置文件
1. 配置SqlMapConfig.properties文件2. 配置SqlMapConfig.xml文件3. 配置SqlMap.xml文件(可能有多个文件,一般情况下,可以一个表对应一个SqlMap.xml文件,文件名称可以与表名相同)注意:上面所述的SqlMapConfig.xml文件必须在类路径中,SqlMapConfig.properties和SqlMap.xml文件可以在类路径中,也可以不在类路径中。当SqlMapConfig.properties和SqlMap.xml文件不在类路径中的时候,配置也不同,在本文中,这三个文件都放在类路径中。二、 程序调用
1. 初始化SqlMapClient对象。2. 运行Sql语句:你可以调用SqlMapClient对象的queryfor...()、insert()、update()、delete()来分别执行select、insert、update和delete操作。好了,下面我们结合实例进行讲解:
三、实例:下面的例子是以mysql为例进行说明,建立了一个author表,为了方便调试代码,你可以将ibatis-common-2.jar、ibatis-dao-2.jar、ibatis-sqlmap-2.jar和lib目录下的所有的jar都加载到你的程序中,在后续的文章中,将会说明每个Jar的用途。(一) 创建数据库和表
创建一个名字为IBatisExample的数据库
CREATE TABLE author (
  auth_id int(8) NOT NULL auto_increment,
  auth_name varchar(100) NOT NULL default '',
  auth_age int(3) NOT NULL default '0',
  auth_tel varchar(100) NOT NULL default '',
  auth_address varchar(100) NOT NULL default '',
  PRIMARY KEY  (auth_id)
) TYPE=MyISAM;
INSERT INTO author VALUES (1, '作者一', 30, '025-12345678', '南京');
INSERT INTO author VALUES (2, '作者二', 30, '025-12345678', '南京');(二) 配置文件1. 配置SqlMapConfig.properties文件
文件内容:
driver=org.gjt.mm.mysql.Driver
url=jdbc:mysql://192.168.0.26:3306/IBatisExample?useUnicode=true&characterEncoding=GB2312
username=root
password=1234562. 配置SqlMapConfig.xml文件
文件内容:

PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"
"http://www.ibatis.com/dtd/sql-map-config-2.dtd">





cacheModelsEnabled="true"
enhancementEnabled="true"
lazyLoadingEnabled="true"
maxRequests="32"
maxSessions="10"
maxTransactions="5"
useStatementNamespaces="false"
/>



















3. 配置SqlMap.xml文件
这里我们命名为author.xml
PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN"
"http://www.ibatis.com/dtd/sql-map-2.dtd">


 SELECT * FROM author

 INSERT INTO author (auth_name,auth_age,auth_tel,auth_address) VALUES (?,?,?,?)

 UPDATE author set auth_name=#name# WHERE auth_id = #id#

 delete from author WHERE auth_id = #id#
(三) 程序调用
由于源代码很长,所以这里我只给出一些简单的程序调用方法,所以如果有人想要源代码的话,可以留下你的邮箱。
1. 初始化一个SqlMapClient对象,代码如下:
public class SqlMapConf
{
    private static SqlMapClient sqlMapClient;
    static
 {
  try
  {
      System.out.println("sqlMapClient initing.....");
   String resource = "SqlMapConfig.xml";
   Reader reader = Resources.getResourceAsReader (resource);
   sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader);
  }
  catch (Exception e)
  {
   e.printStackTrace();
   throw new RuntimeException ("Error initializing MyAppSqlConfig class. Cause: " +e);
  }
 }
    public static SqlMapClient getInstance()
    {
        return sqlMapClient;
    } 
}2. 然后要为Author表写一个bean,代码如下:
public class Author
{
    private int id;
    private int age;
    private String name;  
    private String address;
    private String telephone;
   
    public int getId()
    {
        return id;
    }
    public void setId(int id)
    {
        this.id=id;
    }
    public int getAge()
    {
        return age;
    }
    public void setAge(int age)
    {
        this.age=age;
    }
    public String getName()
    {
        return name;
    }
    public void setName(String name)
    {
        this.name=name;
    }
   
    public String getAddress()
    {
        return address;
    }
    public void setAddress(String address)
    {
        this.address=address;
    }
    public String getTelephone()
    {
        return telephone;
    }
    public void setTelephone(String telephone)
    {
        this.telephone=telephone;
    }
}3. 程序调用:
这里将只示范一下getAuthor、insertAuthor1、updateAuthor和deleteAuthor的方法。
首先应该得到一个SqlMapClient实例:
SqlMapClient sqlMapClient = SqlMapConf.getInstance();(1) getAuthor:
 Author author = (Author)sqlMapClient.queryForObject("getAuthor", new Integer(1));
(2) getAllAuthor
 List authorList = (List)sqlMapClient.queryForList("getAllAuthor", null);
(3) insertAuthor:
 Author author = new Author();
 author.setName("作者三");
 author.setAge(31);
 author.setAddress("南京");
 author.setTelephone("025-987654321");
 sqlMapClient.insert(operaName, author);
(4) updateAuthor
 Author author = new Author();
 author.setName("Updated");
 author.setId(authorID);
 sqlMapClient.update(operaName, author);       
(5) deleteAuthor
 sqlMapClient.delete("deleteAuthor", new Integer(authorID)); 这里只是做一个简单的例子,希望能够帮助快速的入门,而并没有对IBatis的原理进行剖析,不过通过这几个调用,我想你可能能够猜到IBatis的一部分运作原理了,关于IBatis的原理以及高级应用,请关注后续文章本篇文章来源于 新技术天空 原文链接:http://www.ntsky.com/tech/java/opensource/ibatis/2007-06-29/4e9790ff0a9181cf.html