spring提供了ibatis的模板类封装,通过简单的设置就能使用ibatis了
1. 编写POJO
package model;
public class Product{
private int id;//产品ID
private int user_id;//用户ID
private int count;//商品数量
private String name;//商品名称
private String detail;//商品细节
private String time;//发布时间
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public String getDetail() {
return detail;
}
public void setDetail(String detail) {
this.detail = detail;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public int getUser_id() {
return user_id;
}
public void setUser_id(int user_id) {
this.user_id = user_id;
}
}
2. 在Spring配置文件中配置IBATIS
/****** /web-inf/bean-main.xml*******/
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<!-- 1.配置数据源 -->
<bean id="myDataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"
value="com.microsoft.jdbc.sqlserver.SQLServerDriver" />
<property name="url"
value="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=game" />
<property name="username" value="sa" />
<property name="password" value="123456" />
</bean>
<!-- 2.配置数据工厂(ibatis) -->
<bean id="myIbatis"
class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation"
value="/WEB-INF/ibatis-sql-map-config.xml" />
<property name="dataSource" ref="myDataSource" />
</bean>
</beans>
其中<property name="configLocation"
value="/WEB-INF/ibatis-sql-map-config.xml" />指定ibatis配置文件路径
3.配置IBATIS
/****** /web-inf/ibatis-sql-map-config.xml *******/
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMapConfig
PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<settings useStatementNamespaces="true" />
<sqlMap resource="ibatis/product.xml"/>
</sqlMapConfig>
其中<settings useStatementNamespaces="true" />表示允许使用命名空间,保证不同的映射文件存在相同的查询定义标签ID时不冲突,在使用命名空间之前,必须配置。而sqlmap标签用于指定映射文件路径,所有路径都相对于应用程序的类路径,比如/web-inf/classes,在开发阶段,相对于SRC路径就可以了。。。。
4.配置IBATIS映射
/****** /ibatis/product.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 namespace="product">
<typeAlias alias="Product" type="model.Product"/>
<select id="readForId" resultClass="Product" parameterClass="java.lang.Integer">
select * from product where id=#value#
</select>
<insert id="add" parameterClass="Product">
insert into product (user_id,name,detail,[time])
values (#user_id#,#name#,#detail#,#time#)
</insert>
<delete id="delete" parameterClass="java.lang.Integer">
delete from product where id=#value#
</delete>
<update id="updateCount" parameterClass="java.lang.Integer">
update product set [count]=[count]-1 where id=#value#
</update>
</sqlMap>
如果查询语句包含比较运算符,用" <![CDATA[ ]]> "包含,typeAlias标签,指定实体类名字,便于后面的标签引用
5.编写DAO类,并继承自ActionSupport类
package dao;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
import java.util.*;
import model.Product;
public class ProductDao extends SqlMapClientDaoSupport {
//读取特定产品
public Product readForId(int id){
return (Product)getSqlMapClientTemplate().queryForObject("product.readForId",id);
}
//增加产品
public void add(Product product){
getSqlMapClientTemplate().insert("product.add",product);
}
//删除产品
public void delete(int id){
getSqlMapClientTemplate().delete("product.delete",id);
}
//更新产品数量
public void updateCount(int id){
getSqlMapClientTemplate().update("product.updateCount",id);
}
}
在spring配置文件中,对DAO注入sqlMapClient对象,本类中,getSqlMapClientTemplate()方法就能得到sqlMapClient对象的引用,以下为注入的例子:
......................................................................
<bean id="productDao" class="dao.ProductDao">
<property name="sqlMapClient" ref="myIbatis" />
</bean>
.....................................................................
配置好IBATIS,配置好DAO,剩下的就是把DAO注入到需要数据库操作的BO对象中,实现业务逻辑了:
......................................................................
<bean id="productBo" class="bo.ProductBo">
<property name="dao" ref="productDao" />
</bean>
.....................................................................