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;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
insert
into
ibatis
values
("
1
","来宾");
insert
into
ibatis
values
("
2
","用户");
DAO接口:
package
com.etong.community.test.dao;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
import
java.util.List;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
import
com.etong.community.test.daomain.Ibatis;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
public
interface
IbatisDAO
...
{
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
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);
}
Ibatis配置文件(sqlMap-config.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"
>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
<
sqlMapConfig
>
<
sqlMap
resource
="com/etong/community/test/dao/ibatis/maps/ibatis.xml"
/>
</
sqlMapConfig
>
ibatis sql map的文件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
="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
>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
<!--
获得全查询列表
-->
<
select
id
="getAllUsers"
resultMap
="ibatisTest"
>
SELECT ID,NAME FROM IBATIS
</
select
>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
<!--
根据用户名获得用户对象
-->
<
select
id
="getUsersByName"
resultMap
="ibatisTest"
>
SELECT ID,NAME FROM IBATIS WHERE NAME=#value#
</
select
>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
<!--
根据id获得用户对象
-->
<
select
id
="getUsersById"
resultClass
="user"
>
SELECT ID,NAME FROM IBATIS WHERE ID=#value#
</
select
>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
<!--
新增用户对象
-->
<
insert
id
="insertUsers"
parameterClass
="user"
>
INSERT INTO IBATIS (ID,NAME) VALUES (#id#,#name#)
</
insert
>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
<!--
删除用户对象
-->
<
delete
id
="deleteUsers"
>
DELETE FROM IBATIS WHERE ID=#value#
</
delete
>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
<!--
更新用户对象
-->
<
update
id
="updateUsers"
parameterClass
="user"
>
UPDATE IBATIS SET NAME=#name# WHERE ID=#id#
</
update
>
</
sqlMap
>
spring配置文件(applicationContext.xml)
<?
xml version="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/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"
>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
<
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
>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
<
bean
id
="sqlMapClient"
class
="org.springframework.orm.ibatis.SqlMapClientFactoryBean"
>
<!--
此处应注入ibatis配置文件,而非sqlMap文件,否则会出现“there is no statement.....异常”
-->
<
property
name
="configLocation"
>
<
value
>
sqlMap-config.xml
</
value
>
</
property
>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
</
bean
>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
<
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;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
import
java.util.List;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
import
org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
import
com.etong.community.test.dao.IbatisDAO;
import
com.etong.community.test.daomain.Ibatis;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
/** */
/**
* @author admin
*
*/
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
public
class
SqlMapIbatisDAO
extends
SqlMapClientDaoSupport
implements
IbatisDAO
...
{
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//*
* (non-Javadoc)
*
* @see com.etong.community.test.IbatisDAO#delete(java.lang.String)
*/
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public void delete(String id) ...{
getSqlMapClientTemplate().delete("deleteUsers", id);
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//*
* (non-Javadoc)
*
* @see com.etong.community.test.IbatisDAO#getById(java.lang.String)
*/
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public Ibatis getById(String id) ...{
return (Ibatis) getSqlMapClientTemplate().queryForObject(
"getUsersById", id);
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//*
* (non-Javadoc)
*
* @see com.etong.community.test.IbatisDAO#getByName(java.lang.String)
*/
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public Ibatis getByName(String name) ...{
return (Ibatis) getSqlMapClientTemplate().queryForObject(
"getUsersByName", name);
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//*
* (non-Javadoc)
*
* @see com.etong.community.test.IbatisDAO#getList()
*/
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public List getList() ...{
return getSqlMapClientTemplate().queryForList("getAllUsers", null);
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//*
* (non-Javadoc)
*
* @see com.etong.community.test.IbatisDAO#save(com.etong.community.test.Ibatis)
*/
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public void save(Ibatis ibatis) ...{
getSqlMapClientTemplate().insert("insertUsers", ibatis);
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//*
* (non-Javadoc)
*
* @see com.etong.community.test.IbatisDAO#update(com.etong.community.test.Ibatis)
*/
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public void update(Ibatis ibatis) ...{
getSqlMapClientTemplate().update("updateUsers", ibatis);
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
}
domain对象
package
com.etong.community.test.daomain;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
import
java.io.Serializable;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
public
class
Ibatis
implements
Serializable
...
{
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
private static final long serialVersionUID = 1L;
private String column;
private String id;
private String name;
private String columnValue;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public String getId() ...{
return id;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public void setId(String id) ...{
this.id = id;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public String getName() ...{
return name;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public void setName(String name) ...{
this.name = name;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public Ibatis() ...{
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/** *//**
* @param string
*/
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public void setColumn(String column) ...{
this.column = column;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public String getColumn() ...{
return column;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public String getColumnValue() ...{
return columnValue;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public void setColumnValue(String columnValue) ...{
this.columnValue = columnValue;
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public Ibatis(String id, String name) ...{
super();
this.id = id;
this.name = name;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
}
测试代码:
package
com.etong.community.test;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
import
java.util.ArrayList;
import
java.util.Iterator;
import
java.util.List;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
import
org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
import
com.etong.community.test.dao.ibatis.SqlMapIbatisDAO;
import
com.etong.community.test.daomain.Ibatis;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
public
class
TestClient
...
{
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/** *//**
* @param args
*/
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public static void main(String[] args) ...{
ApplicationContext context = new ClassPathXmlApplicationContext(
"applicationContext.xml");
SqlMapIbatisDAO testDAOImpl = (SqlMapIbatisDAO) context
.getBean("testDAO");
// 获得全查询列表
System.out.println("获得全查询列表");
List result = new ArrayList();
result = testDAOImpl.getList();
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
for (Iterator iter = result.iterator(); iter.hasNext();) ...{
Ibatis element = (Ibatis) iter.next();
System.out.println(element.getName());
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
// 根据用户名获得用户对象
System.out.println("根据用户名获得用户对象");
Ibatis ibatis = testDAOImpl.getByName("来宾");
System.out.println(ibatis.getName());
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
// 根据id获得用户对象
System.out.println("根据id获得用户对象");
Ibatis ibatis1 = testDAOImpl.getById("1");
System.out.println(ibatis1.getName());
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
// 新增用户对象
System.out.println("-----------------");
System.out.println("新增用户对象前");
List result1 = new ArrayList();
result1 = testDAOImpl.getList();
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
for (Iterator iter = result1.iterator(); iter.hasNext();) ...{
Ibatis element = (Ibatis) iter.next();
System.out.println(element.getName());
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
}
testDAOImpl.save(new Ibatis("3", "客人")); // 新增用户
System.out.println("新增用户对象后");
List result2 = new ArrayList();
result2 = testDAOImpl.getList();
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
for (Iterator iter = result2.iterator(); iter.hasNext();) ...{
Ibatis element = (Ibatis) iter.next();
System.out.println(element.getName());
}
System.out.println("-----------------");
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
// 删除用户对象
System.out.println("-----------------");
System.out.println("删除用户对象前");
List result3 = new ArrayList();
result3 = testDAOImpl.getList();
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
for (Iterator iter = result3.iterator(); iter.hasNext();) ...{
Ibatis element = (Ibatis) iter.next();
System.out.println(element.getName());
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
}
testDAOImpl.delete("3"); // 删除用户
System.out.println("删除用户对象后");
List result4 = new ArrayList();
result4 = testDAOImpl.getList();
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
for (Iterator iter = result4.iterator(); iter.hasNext();) ...{
Ibatis element = (Ibatis) iter.next();
System.out.println(element.getName());
}
System.out.println("-----------------");
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
// 更新用户对象
System.out.println("-----------------");
System.out.println("更新用户对象前");
List result5 = new ArrayList();
result5 = testDAOImpl.getList();
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
for (Iterator iter = result5.iterator(); iter.hasNext();) ...{
Ibatis element = (Ibatis) iter.next();
System.out.println(element.getName());
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
Ibatis ibatis3 = testDAOImpl.getById("1");
ibatis3.setName("new1");
testDAOImpl.update(ibatis3);// 更新用户对象
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
System.out.println("更新用户对象后");
List result6 = new ArrayList();
result6 = testDAOImpl.getList();
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
for (Iterator iter = result6.iterator(); iter.hasNext();) ...{
Ibatis element = (Ibatis) iter.next();
System.out.println(element.getName());
}
System.out.println("-----------------");
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
}
测试结果:
获得全查询列表
来宾
用户
根据用户名获得用户对象
来宾
根据id获得用户对象
来宾
-----------------
新增用户对象前
来宾
用户
新增用户对象后
来宾
用户
客人
-----------------
-----------------
删除用户对象前
来宾
用户
客人
删除用户对象后
来宾
用户
-----------------
-----------------
更新用户对象前
来宾
用户
更新用户对象后
new1
用户
-----------------