1、ibatis配置文件只保留如下内容
<?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>
<!--
配置映射文件
-->
<sqlMap resource="cn/mldn/ibatis/vo/News.xml" />
</sqlMapConfig>
2、Spring配置文件
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="org.gjt.mm.mysql.Driver">
</property>
<property name="url" value="jdbc:mysql://localhost/test"></property>
<property name="username" value="root"></property>
<property name="password" value="mysqladmin"></property>
</bean>
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="configLocation">
<value>classpath:SqlMapConfig.xml</value>
</property>
</bean>
<bean id="sqlMapClientTemplate" class="org.springframework.orm.ibatis.SqlMapClientTemplate">
<property name="sqlMapClient">
<ref bean="sqlMapClient" />
</property>
</bean>
<bean id="newsdaoimpl" class="cn.mldn.ibatis.dao.impl.NewsDAOImpl">
<property name="sqlMapClientTemplate">
<ref bean="sqlMapClientTemplate" />
</property>
</bean>
3、实现类如下
package cn.mldn.ibatis.dao.impl;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
import cn.mldn.ibatis.dao.NewsDAO;
import cn.mldn.ibatis.vo.News;
public class NewsDAOImpl extends SqlMapClientDaoSupport
implements NewsDAO {
public boolean doCreate(News news) throws Exception {
news.setId((Integer) this.getSqlMapClientTemplate().insert(
"saveNews", news));
return true;
}
public boolean doDelete(int id) throws Exception {
this.getSqlMapClientTemplate().delete("deleteNewsById", id);
return true;
}
public boolean doUpdate(News news) throws Exception {
this.getSqlMapClientTemplate().update("updateNews", news);
return true;
}
public List<News> findAll() throws Exception {
List all = this.getSqlMapClientTemplate()
.queryForList("findAllNews");
return all;
}
public News findById(int id) throws Exception {
News news = (News) this.getSqlMapClientTemplate().queryForObject(
"findNewsById", id);
return news;
}
public List<News> findAll(int cp, int ls) throws Exception {
Map map = new HashMap();
map.put("start", (cp - 1) * ls);
map.put("ls", ls);
List all = this.getSqlMapClientTemplate().queryForList(
"findAllNewsSplit", map);
return all;
}
}
package cn.mldn.ibatis.service.impl;
import java.util.List;
import cn.mldn.ibatis.dao.NewsDAO;
import cn.mldn.ibatis.service.NewsService;
import cn.mldn.ibatis.vo.News;
public class NewsServiceImpl implements NewsService {
private NewsDAO newsdao;
public void setNewsdao(NewsDAO newsdao) {
this.newsdao = newsdao;
}
public boolean doCreate(News news) throws Exception {
boolean flag = false;
flag = this.newsdao.doCreate(news);
return flag;
}
public boolean doDelete(int id) throws Exception {
boolean flag = this.newsdao.doDelete(id);
return flag;
}
public boolean doUpdate(News news) throws Exception {
boolean flag = this.newsdao.doUpdate(news);
return flag;
}
public List<News> findAll() throws Exception {
List all = this.newsdao.findAll();
return all;
}
public News findById(int id) throws Exception {
News news = null;
news = this.newsdao.findById(id);
return news;
}
}
4、Spring的配置文件中加入一下内容
<bean id="newsserviceimpl" class="cn.mldn.ibatis.service.impl.NewsServiceImpl">
<property name="newsdao">
<ref bean="newsdaoimpl" />
</property>
</bean>
<!--实务处理-->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
</bean>
<bean id="transactionInterceptor"
class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager">
<ref bean="transactionManager"></ref>
</property>
<!--
对doxxx方法进行事务处理。
PROPAGATION_REQUIRED表示事物处理方式。
PROPAGATION_REQUIRED:对当前方法使用同一个事务。
该方法内的所有操作都在同一个连接下完成。如果正常执行,
则提交,如果出现异常,自动回滚,该方法执行后自动关闭连接。
PROPAGATION_REQUIRED_NEW:为每个操作建立一个事务,即便中间有一个失败,后面的也继续执行。
PROPAGATION_NEVER:不处理事务
-->
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<list>
<value>*serviceimpl</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>transactionInterceptor</value>
</list>
</property>
</bean>