浏览 2468 次
锁定老帖子 主题:iabtis 测试通过的例子
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2008-10-14
所需jar包:mysql-connector-java-5.0.4-bin.jar ;ibatis-2.3.0.677.jar SqlMapConfig.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> <settings cacheModelsEnabled="true" enhancementEnabled="true" lazyLoadingEnabled="true" errorTracingEnabled="true" maxRequests="32" maxSessions="10" maxTransactions="5" useStatementNamespaces="false" /> <transactionManager type="JDBC"> <dataSource type="SIMPLE"> <property name="JDBC.Driver" value="com.mysql.jdbc.Driver" /> <property name="JDBC.ConnectionURL" value="jdbc:mysql://localhost/sample" /> <property name="JDBC.Username" value="root" /> <property name="JDBC.Password" value="root" /> <property name="Pool.MaximumActiveConnections" value="10" /> <property name="Pool.MaximumIdleConnections" value="5" /> <property name="Pool.MaximumCheckoutTime" value="120000" /> <property name="Pool.TimeToWait" value="500" /> <property name="Pool.PingQuery" value="select 1 from sample" /> <property name="Pool.PingEnabled" value="false" /> <property name="Pool.PingConnectionsOlderThan" value="1" /> <property name="Pool.PingConnectionsNotUsedFor" value="1" /> </dataSource> </transactionManager> <sqlMap resource="com/ctgusec/zhupan/model/User.xml" /> </sqlMapConfig> ============================================== 我用的是mysql数据库 建表语句: DROP TABLE IF EXISTS `sample`.`t_user`; CREATE TABLE `sample`.`t_user` ( `id` int(10) unsigned NOT NULL auto_increment, `name` varchar(50) NOT NULL, `sex` varchar(45) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=gbk; ============================================== 实体 package com.model; import java.io.Serializable; @SuppressWarnings("serial") public class User implements Serializable { private Integer id; private String name; private String sex; public User() { } public Integer getId() { return this.id; } public void setId(Integer id) { this.id = id; } public String getName() { return this.name; } public void setName(String name) { this.name = name; } public String getSex() { return this.sex; } public void setSex(String sex) { this.sex = sex; } } ============================================== 实体对应的mxl文件 <?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"> <typeAlias alias="user" type="com.ctgusec.zhupan.model.User" /> <select id="getUser" parameterClass="java.lang.String" resultClass="user"> select name, sex from t_user where name like '%$name$%' </select> <select id="getAllUser" resultClass="user"> select name, sex from t_user </select> <update id="updateUser" parameterClass="user"> UPDATE t_user SET name=#name#, sex=#sex# WHERE id = #id# </update> <insert id="insertUser" parameterClass="user"> INSERT INTO t_user ( name, sex) VALUES ( #name#, #sex# ) </insert> <delete id="deleteUser" parameterClass="java.lang.Integer"> delete from t_user where id=#value# </delete> </sqlMap> ============================================== 测试类 package com.test; import java.io.Reader; import java.sql.SQLException; import java.util.List; import com.ibatis.common.resources.Resources; import com.ibatis.sqlmap.client.SqlMapClient; import com.ibatis.sqlmap.client.SqlMapClientBuilder; import com.model.User; public class ExampleMain { /** * 初始化 iBatis 获得一个 SqlMapClient 对象 * * @return sqlMap; */ public static SqlMapClient getSqlMap() { SqlMapClient sqlMap = null; String RESOURCE = "com/maps/SqlMapConfig.xml"; try { Reader reader = Resources.getResourceAsReader(RESOURCE); sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader); } catch (Exception e) { e.printStackTrace(); } return sqlMap; } /** * 更新 * */ public static void update() { // getSqlMap(); sqlMap 系统初始化完毕,开始执行 update 操作 SqlMapClient sqlMap = getSqlMap(); try { sqlMap.startTransaction(); User user = new User(); user.setId(new Integer(6)); user.setName("zhupan11"); user.setSex("女"); sqlMap.update("updateUser", user); sqlMap.commitTransaction(); } catch (SQLException e) { System.out.println(e.getMessage()); } finally { try { sqlMap.endTransaction(); } catch (SQLException e) { e.printStackTrace(); } } } /** * 查询 * * @return */ public static List getAllUser() { // getSqlMap();首先初始化 iBatis 获得一个 SqlMapClient 对象 List userList = null; SqlMapClient sqlMap = getSqlMap(); // sqlMap 系统初始化完毕,开始执行 getAllUser 操作 try { sqlMap.startTransaction(); userList = sqlMap.queryForList("getUser", "s");// 模糊查询 // userList = sqlMap.queryForList("getAllUser", null);//查询 sqlMap.commitTransaction(); } catch (SQLException e) { System.out.println(e.getMessage()); } finally { try { sqlMap.endTransaction(); } catch (SQLException e) { e.printStackTrace(); } } return userList; } /** * 删除 * */ public static void deleteUser() { // getSqlMap();首先初始化 iBatis 获得一个 SqlMapClient 对象 // List userList = null; SqlMapClient sqlMap = getSqlMap(); // sqlMap 系统初始化完毕,开始执行 getAllUser 操作 try { sqlMap.startTransaction(); for (int i = 7; i < 12; i++) { sqlMap.delete("deleteUser", new Integer(i));// 参数对应user.xml中的parameterClass所定义的类型 sqlMap.commitTransaction(); } } catch (SQLException e) { System.out.println(e.getMessage()); } finally { try { sqlMap.endTransaction(); } catch (SQLException e) { e.printStackTrace(); } } } /** * 插入数据 * */ public static void insertUser() { // getSqlMap();初始化一个SqlMapClient对象 SqlMapClient sqlMap = getSqlMap(); double tem = Math.random(); User user = new User(); user.setName("sfd" + tem); user.setSex("女"); try { sqlMap.startTransaction(); sqlMap.insert("insertUser", user); sqlMap.commitTransaction(); } catch (SQLException e) { System.out.println(e.getMessage()); } finally { try { sqlMap.endTransaction(); } catch (SQLException e) { e.printStackTrace(); } } } public static void main(String[] args) { update(); // deleteUser(); // insertUser(); List user = getAllUser(); for (int i = 0; i < user.size(); i++) { System.out.println(i + " :: " + ((User) user.get(i)).getName() + "=======" + ((User) user.get(i)).getSex()); } } } ============================================== 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |