ibatis教程入门示例

http://www.javah.net/ibatis/20070713/4087.html

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; } }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值