新闻正文

ibatis教程入门

来源:JAVA天堂  JAVA学习者  2007-7-13 00:26:29 网友评论 1 条 字体:[ ] ~我要投稿!

1.  总体描述

以Eclipse为例说明ibatis用法,数据库为MSSQL2000,ibatis版本为2.0, jDK1.5, 以对一个用户信息表 user_info的插入、查询(单条记录),多记录查询为例说明itatis的用法。
    
说明:
     本文适合初次接触ibatis的读者。
     文章中如有不妥之处,欢迎指正。
     如国引用,请说明出处,谢谢。

2.  准备工作

1.      安装Eclipse 3.0.1

2.      安装jdk1.5

3.      下载 ibatis 2.0 开发包  www.ibatis.com

4.      下载MS SQL 的JDBC驱动包, MSJDBC3.0SP3

5.      下载日志操作包 commons-logging.jar

3.  开发步骤

1.      新建J2EE工程 test

2.      在test下建立包 cjs

3.      建立文件夹 sqlmap

4.      建立文件夹 sqlmap.map

5.      在sqlmap.map 下建立三个文件:

SqlMapConfigExample.properties

sql-map-config.xml

user.xml

 

文件内容如下:

 

SqlMapConfigExample.properties

-----------------------------------------------------------------------------------------------------------

 

driver=com.microsoft.jdbc.sqlserver.SQLServerDriver

url=jdbc:microsoft:sqlserver://localhost:1433;

DatabaseName=java

username=sa

password=tn

-----------------------------------------------------------------------------------------------------------

sql-map-config.xml

-----------------------------------------------------------------------------------------------------------

 

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE sqlMapConfig

 PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"

 "http://www.ibatis.com/dtd/sql-map-config-2.dtd">

 

  <sqlMapConfig>

 

   <properties

    resource="sqlmap/map/SqlMapConfigExample.properties" />

 

   <settings

    cacheModelsEnabled="true"

    enhancementEnabled="true"

    lazyLoadingEnabled="true"

    maxRequests="32"

    maxSessions="10"

    maxTransactions="5"

    useStatementNamespaces="false" />

 

   <transactionManager type="JDBC" >

    <dataSource type="SIMPLE">

     <property name="JDBC.Driver" value="${driver}"/>

     <property name="JDBC.ConnectionURL" value="${url}"/>

     <property name="JDBC.Username" value="${username}"/>

     <property name="JDBC.Password" value="${password}"/>

    </dataSource>

   </transactionManager>

 

    

   <sqlMap resource="sqlmap/map/User.xml" />

</sqlMapConfig>

-----------------------------------------------------------------------------------------------------------

 

 

user.xml

-----------------------------------------------------------------------------------------------------------

 

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE sqlMap

  PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN"

  "http://www.ibatis.com/dtd/sql-map-2.dtd">

 

 <sqlMap namespace="User">

    <select id="getUser" parameterClass="java.lang.Integer"

        resultClass="cjs.User">

        SELECT * FROM user_info WHERE uid=#uid#

    </select>

    <select id="getAllUser" resultClass="cjs.User">

    select

            *from user_info

    wherename = #value#

    </select>

 

    <insertid="insertUser" parameterClass="cjs.User">

         INSERTINTO

             user_info(name, sex, age, addr,zipcode)

        VALUES (#name#, #sex#, #age#,#addr#, #zipcode#)

   </insert>

</sqlMap>

-----------------------------------------------------------------------------------------------------

 

6.      在test上右键,点击导入,将

commonslogging.jar

ibatis-common-2.jar

ibatis-dao-2.jar

ibatis-sqlmap-2.jar

msbase.jar

mssqlserver.jar

msutil.jar

导入到Lib文件夹中

7.      在项目-》属性-》构建路径中添加外部 jar的引用, 也就是上面几个 jar

8.      建立一个数据库 java,并执行下面脚本

Table.sql

-----------------------------------------------------------------------------------------------------

CREATE TABLE user_info

(

  uid int identity(1,1) primary key,

  name varchar(20),

  sex int,

  age int,

  addr varchar(50),

  zipcode varchar(6),

);

-----------------------------------------------------------------------------------------------------

 

9.      在cjs包下面编写类

Myapp.java

Operates.java

User.java

 

内容如下:

Myapp.java

-----------------------------------------------------------------------------------------------------------

/*

 * 创建日期 2005-10-26

 *

 * TODO 要更改此生成的文件的模板,请转至

 * 窗口- 首选项 - Java - 代码样式 - 代码模板

 */

 

/**

 * @author Administrator

 *

 * TODO 要更改此生成的类型注释的模板,请转至

 * 窗口- 首选项 - Java - 代码样式 - 代码模板

 */

package cjs;

importjava.io.Reader;

 

importcom.ibatis.common.resources.Resources;

importcom.ibatis.sqlmap.client.SqlMapClient;

importcom.ibatis.sqlmap.client.SqlMapClientBuilder;

 

 

public classMyapp {

    private static final SqlMapClient sqlMap;

    static {

        try {

            String resource ="sqlmap/map/sql-map-config.xml";

            Reader reader =Resources.getResourceAsReader (resource);

           sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);

        }

        catch (Exception e) {

         e.printStackTrace();   

        throw new RuntimeException ("Errorinitializing MyApp class. Cause:"+e);

        }

    }

    public static SqlMapClientgetSqlMapInstance () {

        return sqlMap;

    }

}

-----------------------------------------------------------------------------------------------------------



收藏到ViVi   收藏此页到365Key
上一篇:ibatis学习后的一些心得
下一篇:一个Servlet生命周期(Tomcat处理请求中)
用户名:新注册) 密码: 匿名评论 [所有评论]
评论内容:不能超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规。
本栏搜索
  • Google
   网站首页 -  网站地图 -  技术学习 -  网站投稿 -  帮助中心
Copyright 2003-2008 www.javah.net All Rights Reserved
2008 如果你喜欢本站 请收藏本站 并推荐给你的朋友一起分享