引自友人blog: [url]http://blog.csdn.net/senton[/url]
 
数据库用的是mysql,开发环境是Eclipse3.2:
1.首先把用到的包导入进来,此例用的是spring-framework-1.2.7,iBATIS_DBL-2.1.7.597,mysql的数
  据库连接包用的是mysql-connector-java-5.0.3-bin.jar.
2.建POJO类,在此我们用的是一个Student类.
 package cn.itcast;
 
 public class Student {
  private Integer id;
 
  private String firstname;
 
  private String lastname;
 
  public String getFirstname() {
   return firstname;
  }
 
  public void setFirstname(String firstname) {
   this.firstname = firstname;
  }
 
  public Integer getId() {
   return id;
  }
 
  public void setId(Integer id) {
   this.id = id;
  }
 
  public String getLastname() {
   return lastname;
  }
 
  public void setLastname(String lastname) {
   this.lastname = lastname;
  }
 }
3.POJO的映射文件Student.xml,在这里面只有两个功能,即根据student的id检索出一个Student对象,另一
  个就是向数据库插入一条记录(一个Student对象),注意:此应用程序中所有的配置文件(xml文件和
  properties文件都放在configfile包下面).
 
 
 
 
  
  
  
  
   insert into student(firstname,lastname) values(#firstname#,#lastname#)
  

 
4.建一个SqlMap的配置文件sql-map-config.xml,sqlMap元素的resource属性告诉Spring去哪找POJO映射文件.
 
  PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"
 " [url]http://www.ibatis.com/dtd/sql-map-config-2.dtd[/url]">
 
  
 
5.jdbc.properties文件,存储数据库连接的driver,url,username,password等信息,
 jdbc.driver=com.mysql.jdbc.Driver
 jdbc.url=jdbc:mysql://localhost/itcast
 jdbc.username=root
 jdbc.password=

6.Spring配置文件applicationContext.xml
 
 
 
 
 
  
     class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
   
    configfile/jdbc.properties
   

  

 
  
     class="org.springframework.jdbc.datasource.DriverManagerDataSource">
   
    ${jdbc.driver}
   

   
    ${jdbc.url}
   

   
    ${jdbc.username}
   

   
    ${jdbc.password}
   

  

 
  
     class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
   
    configfile/sql-map-config.xml
   

   
    
   

  

 
   
     class="org.springframework.orm.ibatis.SqlMapClientTemplate">
   
    
   

  

  
  
  
   
    
   

  

 
 
7.StudentDaoSqlMap 是一个DAO,它负责和数据库的交互,在这里实现了查询单条记录和插入单条记录的功能.
 package cn.itcast;
 
 import org.springframework.orm.ibatis.SqlMapClientTemplate;
 
 public class StudentDaoSqlMap {
  private SqlMapClientTemplate sqlMapClientTemplate;
 
  public SqlMapClientTemplate getSqlMapClientTemplate() {
   return sqlMapClientTemplate;
  }
 
  public void setSqlMapClientTemplate(
    SqlMapClientTemplate sqlMapClientTemplate) {
   this.sqlMapClientTemplate = sqlMapClientTemplate;
  }
 
  //此方法的返回值与Student.xml的select元素的resultClass对应.
  public Student getStudent(Integer id) {
   return (Student) sqlMapClientTemplate.queryForObject("getStudentById",id);
   //注意:queryForObject方法返回一个Object,第一个参数与Student.xml的select元素
   //的id属性值对应,第二个参数的类型与Student.xml的select元素的parameterClass
   //属性值对应.
  }
 
  public Object insertStudent(Student student) {
   return sqlMapClientTemplate.insert("insertStudent", student);
  }
 }
8.下面写一个带main函数的类来测试上面的代码.代码非常简单就不再解释了.
 package cn.itcast;
 
 import org.springframework.context.ApplicationContext;
 import org.springframework.context.support.ClassPathXmlApplicationContext;
 
 public class Client {
 
  public static void main(String[] args) {
   ApplicationContext factory = new ClassPathXmlApplicationContext(
     "applicationContext.xml");
 
   StudentDaoSqlMap studentDao = (StudentDaoSqlMap) factory
     .getBean("studentDao");
   
   //插入一个student
   Student student = new Student();
   student.setFirstname("tian");
   student.setLastname("xiangdong");
   studentDao.insertStudent(student);
 
   //查询出id是1的Student对象.
   //Student student = studentDao.getStudent(1);
   //System.out.println(student.getId());
   //System.out.println(student.getFirstname());
   //System.out.println(student.getLastname());
  }
 
 }