欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Eclipse Spring3.x集成ibatis2.x开发案例

程序员文章站 2022-05-25 11:55:05
...

开发环境如以下:

数据库 mysql 5.x

sql脚本

CREATE TABLE `ibatis` (
  `id` varchar(20) NOT NULL,
  `name` varchar(20) default NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gb2312;

insert into ibatis values("1","1");
insert into ibatis values("2","2");

 

jar包

 mysql-connector-java-5.1.5-bin.jar

commons-dbcp-1.4.jar

commons-logging-1.0.4.jar

commons-pool-1.6.jar

ibatis-2.3.0.677.jar

mysql-connector-java-5.1.5-bin.jar

spring-aop-3.2.0.RELEASE.jar

spring-aspects-3.2.0.RELEASE.jar

spring-beans-3.2.0.RELEASE.jar

spring-context-3.2.0.RELEASE.jar

spring-core-3.2.0.RELEASE.jar

spring-expression-3.2.0.RELEASE.jar

spring-jdbc-3.2.0.RELEASE.jar

spring-orm-3.2.0.RELEASE.jar

spring-tx-3.2.0.RELEASE.jar

spring-web-3.2.0.RELEASE.jar

spring-webmvc-3.2.0.RELEASE.jar

 

 所有的jar包我已经上传

在src下新建ch10.SpringAndIbatis包,所有的测试文件都在这个包下,废话不多说了,直接贴代码

1.Ibatis.java

package ch10.SpringAndIbatis;

public class Ibatis {
  

private String id;
   private String name;
   public String getId() {
        return id;
   }
   public void setId(String id) {
        this.id = id;
   }
   public String getName() {
        return name;
   }
   public void setName(String name) {
        this.name = name;
   }
   public Ibatis(){
       
   }
public Ibatis(String id, String name) {
    super();
    this.id = id;
    this.name = name;
}
}

 

2.IDAO.java

package ch10.SpringAndIbatis;

import java.util.List;

public interface IDAO {
  public List getList();
  public Ibatis getByName(String name);
  public Ibatis getById(String id);
  public void save(Ibatis ibatis);
  public void delete(String id);
  public void update(Ibatis ibatis);
}

 

3.TestDao.java

package ch10.SpringAndIbatis;

import java.util.List;

import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

public class TestDAO extends SqlMapClientDaoSupport implements IDAO {

    public void delete(String id) {
        getSqlMapClientTemplate().delete("deleteUsers", id);
    }

    public Ibatis getById(String id) {
        return (Ibatis)getSqlMapClientTemplate().queryForObject("getUsersById",id);
    }

    public Ibatis getByName(String name) {
        
        return (Ibatis)getSqlMapClientTemplate().queryForObject("getUsersByName",name);
    }

    public List getList() {
        return getSqlMapClientTemplate().queryForList("getAllUsers",null);
    }

    public void save(Ibatis ibatis) {
        getSqlMapClientTemplate().insert("insertUsers",ibatis);
    }

    public void update(Ibatis ibatis) {
        getSqlMapClientTemplate().update("updateUsers", ibatis);
    }

}

 

4.Ibatis.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd" >
<sqlMap >
  <typeAlias type="ch10.SpringAndIbatis.Ibatis" alias="user"/>
  <resultMap id="ibatisTest" class="user" >
    <result column="id" property="id" jdbcType="VARCHAR" />
    <result column="name" property="name" jdbcType="VARCHAR" />
  </resultMap>
  
  <!-- 获得全查询列表 -->
  <select id="getAllUsers" resultMap="ibatisTest">
    select * from Ibatis
  </select>
  
   <!-- 根据用户名获得用户对象 -->
   <select id="getUsersByName" resultMap="ibatisTest">
     select * from Ibatis where name=#value#
  </select> 
  
  <!-- 根据id获得用户对象 -->
    <select id="getUsersById" resultMap="ibatisTest">
     select * from Ibatis where id=#value#
  </select> 
  
   <!-- 新增用户对象 -->
   <insert id="insertUsers" parameterClass="user">
      insert into ibatis (id,name) values (#id#,#name#)
   </insert>
   
   <!-- 删除用户对象 -->
   <delete id="deleteUsers">
     delete from ibatis where id=#value#
   </delete>
   
   <!-- 更新用户对象 -->
   <delete id="updateUsers" parameterClass="user">
      update ibatis set name=#name# where id=#id#
   </delete>
</sqlMap>

 

5.sqlMapConfig.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> 

<sqlMap resource="ch10/SpringAndIbatis/Ibatis.xml" /> 

</sqlMapConfig>

 

6.applicationContext.xml,注意修改你数据库配置

<?xml version="1.0" encoding="UTF-8"?>
<beans
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">


<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
  <property name="driverClassName">
    <value>com.mysql.jdbc.Driver</value>
  </property>
  <property name="username">
    <value>root</value>
  </property>
  <property name="password">
    <value>admin</value>
  </property>
  <property name="url">
    <value>jdbc:mysql://localhost:3306/bosdbo</value>
  </property>
</bean>

<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
  <!-- 此处应注入ibatis配置文件,而非sqlMap文件,否则会出现“there is no statement.....异常” -->
  <property name="configLocation">
     <value>ch10/SpringAndIbatis/sqlMapConfig.xml</value>
  </property>

</bean>

<bean id="testDAO" class="ch10.SpringAndIbatis.TestDAO">
   <property name="dataSource">
   <ref bean="dataSource"/>
 </property>
  <property name="sqlMapClient">
    <ref bean="sqlMapClient"/>
  </property>
</bean>
 </beans>

 

7.Test.java

package ch10.SpringAndIbatis;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {

    /** *//**
     * @param args
     */																						
    public static void main(String[] args) {
        ApplicationContext context=new ClassPathXmlApplicationContext("ch10/SpringAndIbatis/applicationContext.xml");
        TestDAO testDAOImpl=(TestDAO)context.getBean("testDAO");

        //获得全查询列表
        System.out.println("获得全查询列表");
        List result=new ArrayList();
        result=testDAOImpl.getList();
        for (Iterator iter = result.iterator(); iter.hasNext();) {
            Ibatis element = (Ibatis) iter.next();
            System.out.println(element.getName());

        }    
        
       //根据用户名获得用户对象
        System.out.println("根据用户名获得用户对象");
        Ibatis ibatis=testDAOImpl.getByName("1");
        System.out.println(ibatis.getName());
        
       //根据id获得用户对象
        System.out.println("根据id获得用户对象");
        Ibatis ibatis1=testDAOImpl.getById("1");
        System.out.println(ibatis.getName());
        
       //新增用户对象
        System.out.println("-----------------");
        System.out.println("新增用户对象前");
        List result1=new ArrayList();
        result1=testDAOImpl.getList();
        for (Iterator iter = result1.iterator(); iter.hasNext();) {
            Ibatis element = (Ibatis) iter.next();
            System.out.println(element.getName());

        }    
        testDAOImpl.save(new Ibatis("3","3")); //新增用户
        System.out.println("新增用户对象后");
        List result2=new ArrayList();
        result2=testDAOImpl.getList();
        for (Iterator iter = result2.iterator(); iter.hasNext();) {
            Ibatis element = (Ibatis) iter.next();
            System.out.println(element.getName());
        }    
        System.out.println("-----------------");
        
        //删除用户对象
        System.out.println("-----------------");
        System.out.println("删除用户对象前");
        List result3=new ArrayList();
        result3=testDAOImpl.getList();
        for (Iterator iter = result3.iterator(); iter.hasNext();) {
            Ibatis element = (Ibatis) iter.next();
            System.out.println(element.getName());

        }    
        testDAOImpl.delete("3"); //删除用户
        System.out.println("删除用户对象后");
        List result4=new ArrayList();
        result4=testDAOImpl.getList();
        for (Iterator iter = result4.iterator(); iter.hasNext();) {
            Ibatis element = (Ibatis) iter.next();
            System.out.println(element.getName());
        }    
        System.out.println("-----------------");
        
        
        //更新用户对象
        System.out.println("-----------------");
        System.out.println("更新用户对象前");
        List result5=new ArrayList();
        result5=testDAOImpl.getList();
        for (Iterator iter = result5.iterator(); iter.hasNext();){
            Ibatis element = (Ibatis) iter.next();
            System.out.println(element.getName());

        }    
        
        Ibatis ibatis3=testDAOImpl.getById("1");
        ibatis3.setName("new1");
        testDAOImpl.update(ibatis3);//更新用户对象
        
        
        System.out.println("更新用户对象后");
        List result6=new ArrayList();
        result6=testDAOImpl.getList();
        for (Iterator iter = result6.iterator(); iter.hasNext();) {
            Ibatis element = (Ibatis) iter.next();
            System.out.println(element.getName());
        }    
        System.out.println("-----------------");
}
    }

 

 

ps:测试结果

信息: Pre-instantiating singletons in org.s[email protected]110c31: defining beans [dataSource,sqlMapClient,testDAO]; root of factory hierarchy
获得全查询列表
1
2
根据用户名获得用户对象
1
根据id获得用户对象
1
-----------------
新增用户对象前
1
2
新增用户对象后
1
2
3
-----------------
-----------------
删除用户对象前
1
2
3
删除用户对象后
1
2
-----------------
-----------------
更新用户对象前
1
2
更新用户对象后
new1
2
----------------- 

 

 参考文档:http://blog.csdn.net/daryl715/article/details/1760793

相关标签: jar包