1、Ibatis.xml文件代码
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMapConfig
PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<settings
cacheModelsEnabled="true"
errorTracingEnabled="true"
enhancementEnabled="true"
classInfoCacheEnabled="true"
lazyLoadingEnabled="true"
maxRequests="32"
maxSessions="15"
maxTransactions="5"
useStatementNamespaces="false"
/>
<transactionManager type="JDBC">
<dataSource type="SIMPLE">
<property name="JDBC.Driver" value="oracle.jdbc.driver.OracleDriver"/>
<property name="JDBC.ConnectionURL" value="jdbc:oracle:thin:@localhost:1521:xe"/>
<property name="JDBC.Username" value="zzx"/>
<property name="JDBC.Password" value="zzx"/>
</dataSource>
</transactionManager>
<sqlMap resource="com/yiduedu/po/Customer.xml"/>
</sqlMapConfig>
2、Customer实体类代码
package com.yiduedu.po;
public class Customer {
private int cid;
private String name;
private String password;
public Customer(){}
public Customer(String name,String password){
this.password=password;
this.name=name;
}
public int getCid() {
return cid;
}
public void setCid(int cid) {
this.cid = cid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
3、实体类对应的XML映射文件
<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE sqlMap
PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-2.dtd">
<sqlMap>
<resultMap class="com.yiduedu.po.Customer" id="map">
<result property="cid" column="c_id"/>
<result property="name" column="c_name"/>
<result property="password" column="c_pwd"/>
</resultMap>
<statement id="insertObject" parameterClass="com.yiduedu.po.Customer">
<!-- insert into customer values(#cid#,#name#,#password#)-->
insert into customer values(seq_id.nextval,#name#,#password#)
</statement>
<delete id="deleteObject" parameterClass="int">
delete from customer where c_id=#cid#
</delete>
<select id="selectObject" parameterClass="int" resultClass="com.yiduedu.po.Customer">
select c_id as cid,c_name as name,c_pwd as password from customer where c_id=#x#
</select>
<update id="updateObject" parameterClass="com.yiduedu.po.Customer">
update customer set c_name=#name#,c_pwd=#password# where c_id=#cid#;
</update>
<select id="getList" resultClass="com.yiduedu.po.Customer" resultMap="map">
select * from customer
</select>
<!-- 在ibatis中调用存储过程 -->
<parameterMap class="java.util.HashMap" id="pro">
<parameter property="name" jdbcType="VARCHAR" javaType="java.lang.String" mode="IN"/>
<parameter property="password" jdbcType="VARCHAR" javaType="java.lang.String" mode="IN"/>
<parameter property="msg" jdbcType="INTEGER" javaType="java.lang.Integer" mode="OUT"/>
</parameterMap>
<procedure id="getProcedure" parameterMap="pro">
{call pk_test.pro_test1(?,?,?)}
</procedure>
<!-- 调用存储过程返回游标(如果是其他的数据库jdbcType="OTHER",切记:OTHER一定要大写) -->
<parameterMap class="java.util.HashMap" id="pro1">
<parameter property="result" jdbcType="ORACLECURSOR" javaType="java.sql.ResultSet" resultMap="map" mode="OUT"/>
</parameterMap>
<procedure id="getCur" parameterMap="pro1">
{call pk_test.pro_get_cur(?)}
</procedure>
<!-- 模糊查询方法一 -->
<select id="getLike" parameterClass="java.lang.String"
resultClass="com.yiduedu.po.Customer" resultMap="map">
select * from customer where c_name like '%$name$%'
</select>
<!-- 模糊查询方法二 -->
<select id="getLike2" parameterClass="String"
resultClass="com.yiduedu.po.Customer" resultMap="map">
select * from customer where c_name like '$name$'
</select>
</sqlMap>
4、读取映射文件
package com.yiduedu.test;
import java.io.IOException;
import java.io.Reader;
import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
public class TestUtil {
private SqlMapClient client=null;
public SqlMapClient getClient(){
Reader reader=null;
try {
reader=Resources.getResourceAsReader("Ibatis.xml");
client=SqlMapClientBuilder.buildSqlMapClient(reader);
return client;
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
if(reader!=null)
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
}
5、测试代码
package com.yiduedu.test;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.yiduedu.po.Customer;
public class Test1 {
private TestUtil t=new TestUtil();
public void getInsert(Customer cus) throws SQLException{
SqlMapClient client=t.getClient();
client.insert("insertObject", cus);
}
public int getDel(int id) throws SQLException{
SqlMapClient client=t.getClient();
return client.delete("deleteObject", new Integer(id));
}
public Object getSelect(int id) throws SQLException{
SqlMapClient client=t.getClient();
return client.queryForObject("selectObject",new Integer(id));
}
public int getUpdate(Customer cus) throws SQLException{
SqlMapClient client=t.getClient();
return client.update("updateObject", new Integer(cus.getCid()));
}
public List getList() throws SQLException{
SqlMapClient client=t.getClient();
return client.queryForList("getList");
}
public Map getMap() throws SQLException{
SqlMapClient client=t.getClient();
return client.queryForMap("getList", null, "cid");//在此处cid来自Customer类中的属性名
}
public Object getProcedure(Map map) throws SQLException{
SqlMapClient client=t.getClient();
client.insert("getProcedure", map);
return map.get("msg");
}
public Object getListCur(Map map) throws SQLException{
SqlMapClient client=t.getClient();
//client.queryForObject("getCur", map);//若用这个方法就会报错,所以一定要用insert()方法
client.insert("getCur", map);
return map.get("result");
}
public List getLike(String like) throws SQLException{
SqlMapClient client=t.getClient();
return client.queryForList("getLike", like);
}
public List getLike2(String like) throws SQLException{
SqlMapClient client=t.getClient();
return client.queryForList("getLike2", like);
}
public static void main(String[] args) {
Test1 t1=new Test1();
Customer cus=new Customer("zzx6","zz623456x6");
//cus.setCid(2);
try {
//t1.getInsert(cus);
// int a=t1.getDel(1);
// System.out.println();
// Customer cu=(Customer) t1.getSelect(2);
// System.out.println(cu.getName()+"---"+cu.getPassword());
//t1.getUpdate(cus);
// List list=t1.getList();
// Iterator it=list.iterator();
// while(it.hasNext()){
// Customer c=(Customer) it.next();
// System.out.println(c.getCid()+"---"+c.getName()+"---"+c.getPassword());
// }
// Map map=t1.getMap();
// System.out.println(map);
// Set set=map.keySet();
// for (Iterator iterator = set.iterator(); iterator.hasNext();) {
// Integer str = (Integer) iterator.next();
// Customer cu=(Customer) map.get(str);
// System.out.println(cu.getCid()+"----"+cu.getName());
// }
// Map map=new HashMap();
// map.put("name", cus.getName());
// map.put("password", cus.getPassword());
// map.put("msg", null);
// Integer in=(Integer) t1.getProcedure(map);
// System.out.println(in);
Map map=new HashMap();
map.put("result", null);
List list=(List) t1.getListCur(map);
Iterator it=list.iterator();
while(it.hasNext()){
Customer cust=(Customer) it.next();
System.out.println(cust.getCid()+"-----"+cust.getName());
}
//System.out.println(t1.getListCur(map));
// List list=t1.getLike("x");
//
// Iterator it=list.iterator();
// while(it.hasNext()){
// Customer cust=(Customer) it.next();
// System.out.println(cust.getCid()+"----"+cust.getPassword());
// }
//
// List list=t1.getLike2("%z%");
//
// Iterator it=list.iterator();
// while(it.hasNext()){
// Customer cust=(Customer) it.next();
// System.out.println(cust.getCid()+"----"+cust.getPassword());
// }
} catch (SQLException e) {
e.printStackTrace();
}
}
}