测试ibatis类的具体写法
package com.wyq.test;

import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;

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



public class TestIbatis {
    
public void readSqlMapConfig(){
        
    }

    
public static void main(String args[]){
        Reader reader
=null;
        
/*SqlMapClient是ibatis的核心组件,提供数据操作的基础平台,可以通过sqlMapClientBuilder创建
         * 
         * 
*/

        SqlMapClient sqlMap
=null;
        String resource
="sqlMapConfig.xml";
        
try{
         reader
=Resources.getResourceAsReader(resource);
         sqlMap
=SqlMapClientBuilder.buildSqlMapClient(reader);
         
         sqlMap.startTransaction();
         
        
/* //数据写入操作(insert,update,delete)
         User user=new User();
         user.setName("Erica");
         user.setSex(new Integer(1));
         sqlMap.insert("User.insertUser",user);
*/

         
         
/*//数据查询操作(select)
         Integer key=new Integer(1);
         User users=(User)sqlMap.queryForObject("User.getUsers", key);
         System.out.println("id==="+users.getId());
         System.out.println("name==="+users.getName());
         System.out.println("sex==="+users.getSex());
*/

         
         
/*//在指定对象中放查询结果
         Integer key=new Integer(1);
         User user=new User();
         sqlMap.queryForObject("User.getUsers", key,user);
         System.out.println("id==="+user.getId());
         System.out.println("name==="+user.getName());
         System.out.println("sex==="+user.getSex());
*/

         
         
/*//执行批量查询
         List list=sqlMap.queryForList("User.getUsers",null);
         Iterator it=list.iterator();
         while(it.hasNext()){
             User user=(User)it.next();
             System.out.println("id==="+user.getId());
             System.out.println("name==="+user.getName());
             System.out.println("sex==="+user.getSex());
         }
*/

         
         
/*//查询指定范围内的数据
         List list=sqlMap.queryForList("User.getUsers",null,0,3);
         Iterator it=list.iterator();
         while(it.hasNext()){
             User user=(User)it.next();
             System.out.println("id==="+user.getId());
             System.out.println("name==="+user.getName());
             System.out.println("sex==="+user.getSex());
         }
*/

         
         sqlMap.commitTransaction();
         System.out.print(
"操作成功!");
         
        }
catch(IOException e){
            System.out.print(
"读取配置文件失败!");
        }
catch(SQLException ex){
            System.out.print(
"操作数据出现异常!");
        }

        
    }


}

posted on 2007-11-25 20:37  王永庆  阅读(797)  评论(0编辑  收藏  举报