Spring+Ibatis集成开发实例!

Spring+Ibatis集成开发实例

首先建立数据库demo,然后建表(本文选mysql)

建表数据库脚本:

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 ","来宾");
insert into ibatis values (" 2 ","用户");

DAO接口:

package com.etong.community.test.dao;

import java.util.List;

import com.etong.community.test.daomain.Ibatis;

public interface IbatisDAO ... {

publicListgetList();

publicIbatisgetByName(Stringname);

publicIbatisgetById(Stringid);

publicvoidsave(Ibatisibatis);

publicvoiddelete(Stringid);

publicvoidupdate(Ibatisibatis);

}

Ibatis配置文件(sqlMap-config.xml):

<? xmlversion="1.0"encoding="UTF-8" ?>
<! DOCTYPEsqlMapConfigPUBLIC"-//iBATIS.com//DTDSQLMapConfig2.0//EN"
"http://www.ibatis.com/dtd/sql-map-config-2.dtd"
>

< sqlMapConfig >
< sqlMap resource ="com/etong/community/test/dao/ibatis/maps/ibatis.xml" />
</ sqlMapConfig >

ibatis sql map的文件ibatis.xml

<? xmlversion="1.0"encoding="UTF-8" ?>
<! DOCTYPEsqlMapPUBLIC"-//ibatis.apache.org//DTDSQLMap2.0//EN""http://ibatis.apache.org/dtd/sql-map-2.dtd" >
< sqlMap >
< typeAlias type ="com.etong.community.test.daomain.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" >
SELECTID,NAMEFROMIBATIS
</ select >

<!-- 根据用户名获得用户对象 -->
< select id ="getUsersByName" resultMap ="ibatisTest" >
SELECTID,NAMEFROMIBATISWHERENAME=#value#
</ select >

<!-- 根据id获得用户对象 -->
< select id ="getUsersById" resultClass ="user" >
SELECTID,NAMEFROMIBATISWHEREID=#value#
</ select >

<!-- 新增用户对象 -->
< insert id ="insertUsers" parameterClass ="user" >
INSERTINTOIBATIS(ID,NAME)VALUES(#id#,#name#)
</ insert >

<!-- 删除用户对象 -->
< delete id ="deleteUsers" >
DELETEFROMIBATISWHEREID=#value#
</ delete >

<!-- 更新用户对象 -->
< update id ="updateUsers" parameterClass ="user" >
UPDATEIBATISSETNAME=#name#WHEREID=#id#
</ update >
</ sqlMap >

spring配置文件(applicationContext.xml)

<? xmlversion="1.0"encoding="GB18030" ?>
< beans xmlns ="http://www.springframework.org/schema/beans"
xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation
="http://www.springframework.org/schema/beanshttp://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 > Allen </ value >
</ property >
< property name ="url" >
< value > jdbc:mysql://localhost:3306/ibatis </ value >
</ property >
</ bean >

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

</ bean >

< bean id ="testDAO" class ="com.etong.community.test.dao.ibatis.SqlMapIbatisDAO" >
< property name ="dataSource" >
< ref bean ="dataSource" />
</ property >
< property name ="sqlMapClient" >
< ref bean ="sqlMapClient" />
</ property >
</ bean >
</ beans >

DAO实现类:

package com.etong.community.test.dao.ibatis;

import java.util.List;

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

import com.etong.community.test.dao.IbatisDAO;
import com.etong.community.test.daomain.Ibatis;

/***/ /**
*
@authoradmin
*
*/

public class SqlMapIbatisDAO extends SqlMapClientDaoSupport implements IbatisDAO ... {

/**//*
*(non-Javadoc)
*
*@seecom.etong.community.test.IbatisDAO#delete(java.lang.String)
*/

publicvoiddelete(Stringid)...{
getSqlMapClientTemplate().delete(
"deleteUsers",id);

}


/**//*
*(non-Javadoc)
*
*@seecom.etong.community.test.IbatisDAO#getById(java.lang.String)
*/

publicIbatisgetById(Stringid)...{
return(Ibatis)getSqlMapClientTemplate().queryForObject(
"getUsersById",id);
}


/**//*
*(non-Javadoc)
*
*@seecom.etong.community.test.IbatisDAO#getByName(java.lang.String)
*/

publicIbatisgetByName(Stringname)...{
return(Ibatis)getSqlMapClientTemplate().queryForObject(
"getUsersByName",name);
}


/**//*
*(non-Javadoc)
*
*@seecom.etong.community.test.IbatisDAO#getList()
*/

publicListgetList()...{
returngetSqlMapClientTemplate().queryForList("getAllUsers",null);
}


/**//*
*(non-Javadoc)
*
*@seecom.etong.community.test.IbatisDAO#save(com.etong.community.test.Ibatis)
*/

publicvoidsave(Ibatisibatis)...{
getSqlMapClientTemplate().insert(
"insertUsers",ibatis);

}


/**//*
*(non-Javadoc)
*
*@seecom.etong.community.test.IbatisDAO#update(com.etong.community.test.Ibatis)
*/

publicvoidupdate(Ibatisibatis)...{
getSqlMapClientTemplate().update(
"updateUsers",ibatis);

}


}

domain对象

package com.etong.community.test.daomain;

import java.io.Serializable;

public class Ibatis implements Serializable ... {


privatestaticfinallongserialVersionUID=1L;

privateStringcolumn;
privateStringid;
privateStringname;
privateStringcolumnValue;

publicStringgetId()...{
returnid;
}


publicvoidsetId(Stringid)...{
this.id=id;
}


publicStringgetName()...{
returnname;
}
<
分享到:
评论

你可能感兴趣的:(DAO,spring,mysql,bean,ibatis)