修改了一个网上的示例,测试通过!
所需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());
}
}
}
==============================================
分享到:
相关推荐
在iBatis中,结果集的Map封装是通过`<resultMap>`标签来定义的,它可以指定列名或别名作为键,Java字段名作为值。例如: ```xml ``` 在这个例子中,`UserMap`结果集映射将数据库中的`user_id`、`user_name...
Spring框架是Java开发中最常用的轻量级开源框架之一,它为构建企业级应用程序提供...这些基础jar包是构建高效、可维护的Java应用的基础,它们通过Spring的 IoC 容器和AOP特性,极大地提高了代码的可测试性和可重用性。
在Spring+iabtis框架中,增加hessian接口,要实现以下几点: 1.绕过系统本身的认证,拦截器直接将该实现方法的servlet直接调用,需要重写拦截器。 2.由于绕过了spring和Struts,请求没有经过封装的事物管理器实例化,...
其中`#value#`表示传入的实际参数值,通过这种方式实现了SQL语句的参数化,避免了SQL注入等安全问题。 ### 二、插入操作 接下来是第二条SQL语句: ```xml parameterClass=...
ibatis调用oracle存储过程