`
vtyi
  • 浏览: 82388 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

ibatis

阅读更多

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>
 <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="oracle.jdbc.driver.OracleDriver"/>
  <property  name="JDBC.ConnectionURL" value="jdbc:oracle:thin:@127.0.0.1:1521:orcl" />
  <property name="JDBC.Username" value="vtyi" />
  <property name="JDBC.Password" value="vtyi" />
  </dataSource>
 </transactionManager>
 <sqlMap resource="Account.xml" />
 <sqlMap resource="Product.xml" />
</sqlMapConfig>
 

 

Account.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="Account">
<select id="getAccount" parameterClass="java.lang.Integer" resultClass="test.Account">
 select * from account where id= #id#
</select>

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

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

<cacheModel id="cache_one" type="MEMORY"><!-- FIFO  OSCACHE-->
 <flushInterval hours="24" />
 <flushOnExecute statement="insertAccount" />
 
 <property name="reference-type" value="WEAK" /><!-- STRONG,SOFT -->
</cacheModel>
<cacheModel id="cache_two" type="LRU" readOnly="true" serialize="true">
 <flushInterval hours="24" />
 <flushOnExecute statement="insertAccount" />

 <property name="cache-size" value="1000" />
</cacheModel>

<!-- dynamic -->
<select id="dynamic_sql" cacheModel="cache_two" resultMap="one_many">
 select * from Account
 <isGreaterThan prepend="and" property="id" compareValue="0" >
  where id=#id#
 </isGreaterThan>
 order by id
</select>
<statement id="dynamicGetAccountList" resultMap="one_many" >
 select * from ACCOUNT
 <dynamic prepend="WHERE">
 <isNotNull prepend="AND" property="firstName">
  ( ACC_FIRST_NAME = #firstName#
 <isNotNull prepend="OR" property="lastName">
  ACC_LAST_NAME = #lastName#
 </isNotNull>
  )
 </isNotNull>
 <isNotNull prepend="AND" property="emailAddress">
  ACC_EMAIL like #emailAddress#
 </isNotNull>
 <isGreaterThan prepend="AND" property="id" compareValue="0">
  ACC_ID = #id#
 </isGreaterThan>
 </dynamic>
 order by ACC_LAST_NAME
</statement>

<statement id="someName" resultMap="one_many" >
 select * from ACCOUNT
 <dynamic prepend="where">
 <isGreaterThan prepend="and" property="id" compareValue="0">
 ACC_ID = #id#
 </isGreaterThan>
 <isNotNull prepend="and" property="lastName">
 ACC_LAST_NAME = #lastName#
 </isNotNull>
 </dynamic>
 order by ACC_LAST_NAME
</statement>

<!-- xml -->
<select id="getByIdValueXml" resultClass="xml" xmlResultName="account" >
 select * from Account where id= #id#
</select>
</sqlMap>

 

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<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>0){
     sb.append("<"+name+">");
     for(int i=0;i<pd.length;i++){
     
     }
   }
  }
  return null;
 }
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics