打印本文 打印本文 关闭窗口 关闭窗口
iBATIS SQL Maps(二)
作者:武汉SEO闵涛  文章来源:敏韬网  点击数5616  更新时间:2007/11/14 12:57:50  文章录入:mintao  责任编辑:mintao
iBATIS SQL Maps 核心组件,可以说我们的编程工作都是围绕着它展开。

 

形成的 one-to-many 保存如下:

 

package test;

import java.io.Reader;

import com.ibatis.sqlmap.client.*;
import com.ibatis.common.resources.*;

import bo.*;

public class AutoMag {

 private Reader reader;
 private SqlMapClient sqlMap;
 private String resource = "SqlMapConfig.xml";
 
 public void insertPeople() throws Exception{
  try{
   reader = Resources.getResourceAsReader(resource);
      sqlMap=SqlMapClientBuilder.buildSqlMapClient(reader);
      sqlMap.startTransaction();
     
      People people=new People();
      people.setName("张三");
      people.setAddress("中国");
            sqlMap.insert("insertPeople",people);

            AutoInfo autoInfo=new AutoInfo();
            autoInfo.setLicensePlate("A00001");
            autoInfo.setOwnerNo(people);           
      sqlMap.insert("insertAutoInfo",autoInfo);
     
      sqlMap.commitTransaction();
  }finally{
   sqlMap.endTransaction();
  }
 }
}

 

程序和 Hibernate 写法差不多,我感觉甚至比 Hibernate 更简单。我可以显示的进行 insert 操作,符合传统 JDBC 编程习惯。iBATIS SQL Maps 支持自动事务处理,可以不用写明 startTransactioncommitTransaction。但如果 People insert 操作成功,而 AutoInfo insert 操作失败,就破坏了两次 insert 操作的原子性。最后 endTransaction

上一页  [1] [2] [3] [4] [5] [6] [7]  下一页

打印本文 打印本文 关闭窗口 关闭窗口