---
title: "ibatis"
author: "vtyi"
date: "2009-06-05T17:10:33Z"
tags: []
slug: "ibatis"
draft: false
lastmod: "2009-06-05T17:10:33Z"
id: 402940
url: "https://vtyi.iteye.com/blog/402940"
---

sql-map-config.xml

 

&lt;?xml version="1.0" encoding="UTF-8"?&gt;  
&lt;!DOCTYPE sqlMapConfig  
  PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"  
  "[http://www.ibatis.com/dtd/sql-map-config-2.dtd](http://www.ibatis.com/dtd/sql-map-config-2.dtd)"&gt;  
&lt;sqlMapConfig&gt;  
 &lt;settings cacheModelsEnabled="true"  
     enhancementEnabled="true"  
     lazyLoadingEnabled="true"  
     maxRequests="32"  
     maxSessions="10"  
     maxTransactions="5"  
     useStatementNamespaces="false" /&gt;  
 &lt;transactionManager type="JDBC" &gt;  
  &lt;dataSource type="SIMPLE"&gt;  
  &lt;property name="JDBC.Driver" value="oracle.jdbc.driver.OracleDriver"/&gt;  
  &lt;property  name="JDBC.ConnectionURL" value="jdbc:oracle:thin:@127.0.0.1:1521:orcl" /&gt;  
  &lt;property name="JDBC.Username" value="vtyi" /&gt;  
  &lt;property name="JDBC.Password" value="vtyi" /&gt;  
  &lt;/dataSource&gt;  
 &lt;/transactionManager&gt;  
 &lt;sqlMap resource="Account.xml" /&gt;  
 &lt;sqlMap resource="Product.xml" /&gt;  
&lt;/sqlMapConfig&gt;  
 

 

Account.xml

 

&lt;?xml version="1.0" encoding="UTF-8"?&gt;  
&lt;!DOCTYPE sqlMap  
  PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN"  
  "[http://www.ibatis.com/dtd/sql-map-2.dtd](http://www.ibatis.com/dtd/sql-map-2.dtd)"&gt;  
&lt;sqlMap namespace="Account"&gt;  
&lt;select id="getAccount" parameterClass="java.lang.Integer" resultClass="test.Account"&gt;  
 select * from account where id= #id#  
&lt;/select&gt;

&lt;insert id="insertAccount" parameterClass="test.Account"&gt;  
   
 &lt;selectKey resultClass="int" keyProperty="id"&gt;  
  select accountSequence.nextval as id from dual  
 &lt;/selectKey&gt;  
   
 insert into Account values(#id#,#name#,#subid#,#address#)  
&lt;/insert&gt;

&lt;!--   
&lt;parameterMap id="insert\_param" class="test.Account"&gt;  
 &lt;parameter property="id" jdbcType="NUMBER" javaType="java.lang.Integer" nullValue="0" /&gt;  
 &lt;parameter property="name" jdbcType="VARCHAR" javaType="java.lang.String" nullValue="aa" /&gt;  
&lt;/parameterMap&gt;  
&lt;statement id="insert\_statement" parameterMap="insert\_param"&gt;  
 insert into Account(id,name) values(?,?);  
&lt;/statement&gt;  
&lt;parameterMap id="parameterMap" class="map"&gt;  
 &lt;parameter property="name" jdbcType="VARCHAR" javaType="java.lang.String" mode="INOUT" nullValue="aa"&gt;  
 &lt;/parameter&gt;  
&lt;/parameterMap&gt;  
&lt;procedure id="procedureMap" parameterMap="parameterMap"&gt;  
 {call swap(?)}  
&lt;/procedure&gt;  
\--&gt;  
&lt;resultMap id="one\_many" class="test.Account"&gt;  
 &lt;result property="id" column="id" /&gt;  
 &lt;result property="name" column="name" /&gt;  
 &lt;result property="subid" column="subid" /&gt;  
 &lt;result property="address" column="address" /&gt;  
 &lt;!-- n+1  
 &lt;result property="address" column="address" /&gt;  
 &lt;result property="subid" column="subid" select="getProduct" /&gt;  
  --&gt;  
 &lt;result property="product.id" column="id" /&gt;  
 &lt;result property="product.name" column="name" /&gt;  
&lt;/resultMap&gt;  
&lt;!-- n+1  
&lt;resultMap id="product\_result" class="test.Product" &gt;  
 &lt;result property="id" column="id" /&gt;  
 &lt;result property="name" column="name" /&gt;  
&lt;/resultMap&gt;  
\--&gt;  
&lt;select id="getAccountAndProduct" parameterClass="java.lang.Integer" resultMap="one\_many"&gt;  
 select * from Account ,Product  where Account.id= Product.id and Account.id=#id#  
&lt;/select&gt;  
&lt;!--   
&lt;select id="getProduct" parameterClass="java.lang.Integer" resultMap="product\_result"&gt;  
 select * from Product where id= #id#  
&lt;/select&gt;  
\--&gt;

&lt;cacheModel id="cache\_one" type="MEMORY"&gt;&lt;!-- FIFO  OSCACHE--&gt;  
 &lt;flushInterval hours="24" /&gt;  
 &lt;flushOnExecute statement="insertAccount" /&gt;  
   
 &lt;property name="reference-type" value="WEAK" /&gt;&lt;!-- STRONG,SOFT --&gt;  
&lt;/cacheModel&gt;  
&lt;cacheModel id="cache\_two" type="LRU" readOnly="true" serialize="true"&gt;  
 &lt;flushInterval hours="24" /&gt;  
 &lt;flushOnExecute statement="insertAccount" /&gt;

 &lt;property name="cache-size" value="1000" /&gt;  
&lt;/cacheModel&gt;

&lt;!-- dynamic --&gt;  
&lt;select id="dynamic\_sql" cacheModel="cache\_two" resultMap="one\_many"&gt;  
 select * from Account  
 &lt;isGreaterThan prepend="and" property="id" compareValue="0" &gt;  
  where id=#id#  
 &lt;/isGreaterThan&gt;  
 order by id  
&lt;/select&gt;  
&lt;statement id="dynamicGetAccountList" resultMap="one\_many" &gt;  
 select * from ACCOUNT  
 &lt;dynamic prepend="WHERE"&gt;  
 &lt;isNotNull prepend="AND" property="firstName"&gt;  
  ( ACC\_FIRST\_NAME = #firstName#  
 &lt;isNotNull prepend="OR" property="lastName"&gt;  
  ACC\_LAST\_NAME = #lastName#  
 &lt;/isNotNull&gt;  
  )  
 &lt;/isNotNull&gt;  
 &lt;isNotNull prepend="AND" property="emailAddress"&gt;  
  ACC\_EMAIL like #emailAddress#  
 &lt;/isNotNull&gt;  
 &lt;isGreaterThan prepend="AND" property="id" compareValue="0"&gt;  
  ACC\_ID = #id#  
 &lt;/isGreaterThan&gt;  
 &lt;/dynamic&gt;  
 order by ACC\_LAST\_NAME  
&lt;/statement&gt;

&lt;statement id="someName" resultMap="one\_many" &gt;  
 select * from ACCOUNT  
 &lt;dynamic prepend="where"&gt;  
 &lt;isGreaterThan prepend="and" property="id" compareValue="0"&gt;  
 ACC\_ID = #id#  
 &lt;/isGreaterThan&gt;  
 &lt;isNotNull prepend="and" property="lastName"&gt;  
 ACC\_LAST\_NAME = #lastName#  
 &lt;/isNotNull&gt;  
 &lt;/dynamic&gt;  
 order by ACC\_LAST\_NAME  
&lt;/statement&gt;

&lt;!-- xml --&gt;  
&lt;select id="getByIdValueXml" resultClass="xml" xmlResultName="account" &gt;  
 select * from Account where id= #id#  
&lt;/select&gt;  
&lt;/sqlMap&gt;

 

Account.java

 

package test;

public class Account {

 private Integer id;  
 private String name;  
 private Integer subid;  
 private String address;  
 private Product product;  
 public Integer getId() {  
  return id;  
 }  
 public void setId(Integer id) {  
  this.id = id;  
 }  
 public String getName() {  
  return name;  
 }  
 public void setName(String name) {  
  this.name = name;  
 }  
 public Integer getSubid() {  
  return subid;  
 }  
 public void setSubid(Integer subid) {  
  this.subid = subid;  
 }  
 public String getAddress() {  
  return address;  
 }  
 public void setAddress(String address) {  
  this.address = address;  
 }  
   
 public Product getProduct() {  
  return product;  
 }  
 public void setProduct(Product product) {  
  this.product = product;  
 }  
 @Override  
 public String toString(){  
  return "id= "+id+" name= "+name+" subid= "+subid+" address= "+address+  
  " product={ "+product+" }";  
 }  
   
}

Product.java

 

package test;

public class Product {

 private Integer id;  
 private String name;  
 public Integer getId() {  
  return id;  
 }  
 public void setId(Integer id) {  
  this.id = id;  
 }  
 public String getName() {  
  return name;  
 }  
 public void setName(String name) {  
  this.name = name;  
 }  
 @Override  
 public String toString(){  
  return "id= "+id+" name= "+name;  
 }  
}

 

Ibatis.java

 

package test;

import java.beans.Introspector;  
import java.beans.PropertyDescriptor;  
import java.io.Reader;  
import java.util.List;

import com.ibatis.common.resources.Resources;  
import com.ibatis.sqlmap.client.SqlMapClient;  
import com.ibatis.sqlmap.client.SqlMapClientBuilder;

public class Ibatis {

 /\**  
  * @param args  
  \*/  
 public static void main(String\[] args) throws Exception {  
    
  String resource="sql-map-config.xml";  
  System.out.println(Ibatis.class.getClassLoader().getResource("E:\\\\workspace\\\\test\\\\src\\\\sql-map-config.xml"));  
  Reader reader=Resources.getResourceAsReader(resource);  
  SqlMapClient sqlMap=SqlMapClientBuilder.buildSqlMapClient(reader);  
//  List list=sqlMap.queryForList("getAccount",1);  
  List list=sqlMap.queryForList("getAccountAndProduct",1);  
  if(list!=null && !list.isEmpty()){  
   for(int i=0;i&lt;list.size();i++){  
    Account acc=(Account)list.get(i);  
    System.out.println(acc);  
   }  
  }  
  Account acc=new Account();  
  acc.setAddress("深圳3");  
//  acc.setId(new Integer(2));  
  acc.setSubid(new Integer(3));  
  acc.setName("cccca");  
  sqlMap.insert("insertAccount", acc);  
    
  String xml=(String)sqlMap.queryForObject("getByIdValueXml", new Integer(1));  
  System.out.println(xml);  
 }

 public String convertToXml(Object o,Class c,String name) throws Exception{  
  StringBuffer sb=new StringBuffer("");  
  if(o.getClass().isAssignableFrom(c)){  
   PropertyDescriptor\[] pd=Introspector.getBeanInfo(c).getPropertyDescriptors();  
   if(pd.length&gt;0){  
     sb.append("&lt;"+name+"&gt;");  
     for(int i=0;i&lt;pd.length;i++){  
       
     }  
   }  
  }  
  return null;  
 }  
}