码迷,mamicode.com
首页 > 编程语言 > 详细

Spring+Ibatis集成开发实例

时间:2014-08-18 16:01:12      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:style   http   color   java   os   io   文件   数据   

首先简历数据库demo(本文选mysql)

数据库脚本:

 

bubuko.com,布布扣CREATE TABLE `ibatis` (
bubuko.com,布布扣  `id` 
varchar(20NOT NULL,
bubuko.com,布布扣  `name` 
varchar(20default NULL,
bubuko.com,布布扣  
PRIMARY KEY  (`id`)
bubuko.com,布布扣) ENGINE
=InnoDB DEFAULT CHARSET=gb2312;
bubuko.com,布布扣
bubuko.com,布布扣
insert into ibatis values("1","1");
bubuko.com,布布扣
insert into ibatis values("2","2");

 

DAO接口:

 

bubuko.com,布布扣package ch10.SpringAndIbatis;
bubuko.com,布布扣
bubuko.com,布布扣
import java.util.List;
bubuko.com,布布扣
bubuko.com,布布扣
public interface IDAO {
bubuko.com,布布扣  
public List getList();
bubuko.com,布布扣  
public Ibatis getByName(String name);
bubuko.com,布布扣  
public Ibatis getById(String id);
bubuko.com,布布扣  
public void save(Ibatis ibatis);
bubuko.com,布布扣  
public void delete(String id);
bubuko.com,布布扣  
public void update(Ibatis ibatis);
bubuko.com,布布扣}

bubuko.com,布布扣

 

Ibatis配置文件:

 

bubuko.com,布布扣<?xml version="1.0" encoding="UTF-8"?> 
bubuko.com,布布扣
<!DOCTYPE sqlMapConfig 
bubuko.com,布布扣PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN" 
bubuko.com,布布扣"http://www.ibatis.com/dtd/sql-map-config-2.dtd"
> 
bubuko.com,布布扣
<sqlMapConfig> 
bubuko.com,布布扣
bubuko.com,布布扣
<sqlMap resource="ch10/SpringAndIbatis/Ibatis.xml" /> 
bubuko.com,布布扣
bubuko.com,布布扣
</sqlMapConfig>

 

ibatis sql map文件ibatis.xml:

 

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

spring配置文件

 

bubuko.com,布布扣<?xml version="1.0" encoding="UTF-8"?>
bubuko.com,布布扣
<beans
bubuko.com,布布扣    
xmlns="http://www.springframework.org/schema/beans"
bubuko.com,布布扣    xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
bubuko.com,布布扣    xsi:schemaLocation
="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
bubuko.com,布布扣
bubuko.com,布布扣
bubuko.com,布布扣
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
bubuko.com,布布扣  
<property name="driverClassName">
bubuko.com,布布扣    
<value>com.mysql.jdbc.Driver</value>
bubuko.com,布布扣  
</property>
bubuko.com,布布扣  
<property name="username">
bubuko.com,布布扣    
<value>root</value>
bubuko.com,布布扣  
</property>
bubuko.com,布布扣  
<property name="password">
bubuko.com,布布扣    
<value>1234</value>
bubuko.com,布布扣  
</property>
bubuko.com,布布扣  
<property name="url">
bubuko.com,布布扣    
<value>jdbc:mysql://localhost:3306/spring</value>
bubuko.com,布布扣  
</property>
bubuko.com,布布扣
</bean>
bubuko.com,布布扣
bubuko.com,布布扣
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
bubuko.com,布布扣  
<!-- 此处应注入ibatis配置文件,而非sqlMap文件,否则会出现“there is no statement.....异常” -->
bubuko.com,布布扣  
<property name="configLocation">
bubuko.com,布布扣     
<value>ch10/SpringAndIbatis/sqlMapConfig.xml</value>
bubuko.com,布布扣  
</property>
bubuko.com,布布扣
bubuko.com,布布扣
</bean>
bubuko.com,布布扣
bubuko.com,布布扣
<bean id="testDAO" class="ch10.SpringAndIbatis.TestDAO">
bubuko.com,布布扣   
<property name="dataSource">
bubuko.com,布布扣   
<ref bean="dataSource"/>
bubuko.com,布布扣 
</property>
bubuko.com,布布扣  
<property name="sqlMapClient">
bubuko.com,布布扣    
<ref bean="sqlMapClient"/>
bubuko.com,布布扣  
</property>
bubuko.com,布布扣
</bean>
bubuko.com,布布扣
bubuko.com,布布扣
</beans>

DAO实现类:

 

bubuko.com,布布扣package ch10.SpringAndIbatis;
bubuko.com,布布扣
bubuko.com,布布扣
import java.util.List;
bubuko.com,布布扣
bubuko.com,布布扣
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
bubuko.com,布布扣
bubuko.com,布布扣
public class TestDAO extends SqlMapClientDaoSupport implements IDAO {
bubuko.com,布布扣
bubuko.com,布布扣    
public void delete(String id) {
bubuko.com,布布扣        getSqlMapClientTemplate().delete(
"deleteUsers", id);
bubuko.com,布布扣    }

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

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

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

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

bubuko.com,布布扣
bubuko.com,布布扣    
public void update(Ibatis ibatis) {
bubuko.com,布布扣        getSqlMapClientTemplate().update(
"updateUsers", ibatis);
bubuko.com,布布扣    }

bubuko.com,布布扣
bubuko.com,布布扣}

bubuko.com,布布扣

domain对象

 

bubuko.com,布布扣package ch10.SpringAndIbatis;
bubuko.com,布布扣
bubuko.com,布布扣
public class Ibatis {
bubuko.com,布布扣  
bubuko.com,布布扣
bubuko.com,布布扣
private String id;
bubuko.com,布布扣   
private String name;
bubuko.com,布布扣   
public String getId() {
bubuko.com,布布扣        
return id;
bubuko.com,布布扣   }

bubuko.com,布布扣   
public void setId(String id) {
bubuko.com,布布扣        
this.id = id;
bubuko.com,布布扣   }

bubuko.com,布布扣   
public String getName() {
bubuko.com,布布扣        
return name;
bubuko.com,布布扣   }

bubuko.com,布布扣   
public void setName(String name) {
bubuko.com,布布扣        
this.name = name;
bubuko.com,布布扣   }

bubuko.com,布布扣   
public Ibatis(){
bubuko.com,布布扣       
bubuko.com,布布扣   }

bubuko.com,布布扣
public Ibatis(String id, String name) {
bubuko.com,布布扣    
super();
bubuko.com,布布扣    
this.id = id;
bubuko.com,布布扣    
this.name = name;
bubuko.com,布布扣}

bubuko.com,布布扣}

bubuko.com,布布扣

 

 

測试代码:

 

 

bubuko.com,布布扣package ch10.SpringAndIbatis;
bubuko.com,布布扣
bubuko.com,布布扣
import java.util.ArrayList;
bubuko.com,布布扣
import java.util.Iterator;
bubuko.com,布布扣
import java.util.List;
bubuko.com,布布扣
bubuko.com,布布扣
import org.springframework.context.ApplicationContext;
bubuko.com,布布扣
import org.springframework.context.support.ClassPathXmlApplicationContext;
bubuko.com,布布扣
bubuko.com,布布扣
public class Test {
bubuko.com,布布扣
bubuko.com,布布扣    
/**
bubuko.com,布布扣     * 
@param args
bubuko.com,布布扣     
*/

bubuko.com,布布扣    
public static void main(String[] args) {
bubuko.com,布布扣        ApplicationContext context
=new ClassPathXmlApplicationContext("ch10/SpringAndIbatis/applicationContext.xml");
bubuko.com,布布扣        TestDAO testDAOImpl
=(TestDAO)context.getBean("testDAO");
bubuko.com,布布扣
bubuko.com,布布扣        
//获得全查询列表
bubuko.com,布布扣
        System.out.println("获得全查询列表");
bubuko.com,布布扣        List result
=new ArrayList();
bubuko.com,布布扣        result
=testDAOImpl.getList();
bubuko.com,布布扣        
for (Iterator iter = result.iterator(); iter.hasNext();) {
bubuko.com,布布扣            Ibatis element 
= (Ibatis) iter.next();
bubuko.com,布布扣            System.out.println(element.getName());
bubuko.com,布布扣
bubuko.com,布布扣        }
    
bubuko.com,布布扣        
bubuko.com,布布扣       
//依据username获得用户对象
bubuko.com,布布扣
        System.out.println("依据username获得用户对象");
bubuko.com,布布扣        Ibatis ibatis
=testDAOImpl.getByName("1");
bubuko.com,布布扣        System.out.println(ibatis.getName());
bubuko.com,布布扣        
bubuko.com,布布扣       
//依据id获得用户对象
bubuko.com,布布扣
        System.out.println("依据id获得用户对象");
bubuko.com,布布扣        Ibatis ibatis1
=testDAOImpl.getById("1");
bubuko.com,布布扣        System.out.println(ibatis.getName());
bubuko.com,布布扣        
bubuko.com,布布扣       
//新增用户对象
bubuko.com,布布扣
        System.out.println("-----------------");
bubuko.com,布布扣        System.out.println(
"新增用户对象前");
bubuko.com,布布扣        List result1
=new ArrayList();
bubuko.com,布布扣        result1
=testDAOImpl.getList();
bubuko.com,布布扣        
for (Iterator iter = result1.iterator(); iter.hasNext();) {
bubuko.com,布布扣            Ibatis element 
= (Ibatis) iter.next();
bubuko.com,布布扣            System.out.println(element.getName());
bubuko.com,布布扣
bubuko.com,布布扣        }
    
bubuko.com,布布扣        testDAOImpl.save(
new Ibatis("3","3")); //新增用户
bubuko.com,布布扣
        System.out.println("新增用户对象后");
bubuko.com,布布扣        List result2
=new ArrayList();
bubuko.com,布布扣        result2
=testDAOImpl.getList();
bubuko.com,布布扣        
for (Iterator iter = result2.iterator(); iter.hasNext();) {
bubuko.com,布布扣            Ibatis element 
= (Ibatis) iter.next();
bubuko.com,布布扣            System.out.println(element.getName());
bubuko.com,布布扣        }
    
bubuko.com,布布扣        System.out.println(
"-----------------");
bubuko.com,布布扣        
bubuko.com,布布扣        
//删除用户对象
bubuko.com,布布扣
        System.out.println("-----------------");
bubuko.com,布布扣        System.out.println(
"删除用户对象前");
bubuko.com,布布扣        List result3
=new ArrayList();
bubuko.com,布布扣        result3
=testDAOImpl.getList();
bubuko.com,布布扣        
for (Iterator iter = result3.iterator(); iter.hasNext();) {
bubuko.com,布布扣            Ibatis element 
= (Ibatis) iter.next();
bubuko.com,布布扣            System.out.println(element.getName());
bubuko.com,布布扣
bubuko.com,布布扣        }
    
bubuko.com,布布扣        testDAOImpl.delete(
"3"); //删除用户
bubuko.com,布布扣
        System.out.println("删除用户对象后");
bubuko.com,布布扣        List result4
=new ArrayList();
bubuko.com,布布扣        result4
=testDAOImpl.getList();
bubuko.com,布布扣        
for (Iterator iter = result4.iterator(); iter.hasNext();) {
bubuko.com,布布扣            Ibatis element 
= (Ibatis) iter.next();
bubuko.com,布布扣            System.out.println(element.getName());
bubuko.com,布布扣        }
    
bubuko.com,布布扣        System.out.println(
"-----------------");
bubuko.com,布布扣        
bubuko.com,布布扣        
bubuko.com,布布扣        
//更新用户对象
bubuko.com,布布扣
        System.out.println("-----------------");
bubuko.com,布布扣        System.out.println(
"更新用户对象前");
bubuko.com,布布扣        List result5
=new ArrayList();
bubuko.com,布布扣        result5
=testDAOImpl.getList();
bubuko.com,布布扣        
for (Iterator iter = result5.iterator(); iter.hasNext();) {
bubuko.com,布布扣            Ibatis element 
= (Ibatis) iter.next();
bubuko.com,布布扣            System.out.println(element.getName());
bubuko.com,布布扣
bubuko.com,布布扣        }
    
bubuko.com,布布扣        
bubuko.com,布布扣        Ibatis ibatis3
=testDAOImpl.getById("1");
bubuko.com,布布扣        ibatis3.setName(
"new1");
bubuko.com,布布扣        testDAOImpl.update(ibatis3);
//更新用户对象
bubuko.com,布布扣
        
bubuko.com,布布扣        
bubuko.com,布布扣        System.out.println(
"更新用户对象后");
bubuko.com,布布扣        List result6
=new ArrayList();
bubuko.com,布布扣        result6
=testDAOImpl.getList();
bubuko.com,布布扣        
for (Iterator iter = result6.iterator(); iter.hasNext();) {
bubuko.com,布布扣            Ibatis element 
= (Ibatis) iter.next();
bubuko.com,布布扣            System.out.println(element.getName());
bubuko.com,布布扣        }
    
bubuko.com,布布扣        System.out.println(
"-----------------");
bubuko.com,布布扣        
bubuko.com,布布扣     }

bubuko.com,布布扣
bubuko.com,布布扣}

bubuko.com,布布扣

 

測试结果:

获得全查询列表
1
2
依据username获得用户对象
1
依据id获得用户对象
1
-----------------
新增用户对象前
1
2
新增用户对象后
1
2
3
-----------------
-----------------
删除用户对象前
1
2
3
删除用户对象后
1
2
-----------------
-----------------
更新用户对象前
1
2
更新用户对象后
new1
2
-----------------

Spring+Ibatis集成开发实例,布布扣,bubuko.com

Spring+Ibatis集成开发实例

标签:style   http   color   java   os   io   文件   数据   

原文地址:http://www.cnblogs.com/hrhguanli/p/3919567.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!