1、所需jar
cglib-nodep-2.1_3.jar
commons-dbcp.jar
commons-logging-1.1.1.jar
commons-pool-1.6.jar
ibatis-2.3.0.677.jar
mysql-connector-java-5.0.8-bin.jar
spring.jar
spring-context.jar
spring-ibatis.jar
2、spring配置文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <!--<value>cfg.cfg</value>--> </list> </property> </bean> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName"> <value>com.mysql.jdbc.Driver</value> </property> <property name="url"> <value>jdbc:mysql://localhost:3306/test</value> </property> <property name="username"> <value>root</value> </property> <property name="password"> <value>hitv</value> </property> </bean> <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"> <property name="configLocation"> <value>/mysql-sql-map-config.xml</value> </property> <property name="dataSource"> <ref local="dataSource" /> </property> </bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource"> <ref local="dataSource"/> </property> </bean> <bean id="tempProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <property name="transactionManager"> <ref bean="transactionManager"/> </property> <property name="target"> <ref local="tempService"/> </property> <property name="transactionAttributes"> <props> <prop key="*">PROPAGATION_REQUIRED,-Exception</prop> </props> </property> </bean> <bean id="tempDao" class="test.TempDao"> <property name="sqlMapClient" ref="sqlMapClient"></property> </bean> <bean id="tempService" class="test.TempService"> <property name="tempDao" ref="tempDao" /> </bean> </beans>
3、ibatis配置文件
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <!DOCTYPE sqlMapConfig PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-config-2.dtd"> <sqlMapConfig> <settings cacheModelsEnabled="true" enhancementEnabled="true" lazyLoadingEnabled="true" maxSessions="128" maxTransactions="32" maxRequests="512" useStatementNamespaces="true" /> <sqlMap resource="test/temp.xml" /> </sqlMapConfig>
<?xml version="1.0" encoding="GBK" standalone="no"?> <!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd"> <sqlMap namespace="temp"> <typeAlias alias="temp" type="test.TempVO" /> <resultMap id="TempVO-Result-List" class="temp"> <result property="id" column="id" /> <result property="name" column="name" /> <result property="type" column="types" /> </resultMap> <select id="getAllTemp" resultMap="TempVO-Result-List"> select * from temp </select> <insert id="insertTemp"> insert into temp(name,types) values(#name#,#type#) </insert> </sqlMap>
4、java代码
package test; public class TempVO { private int id; private String name; private int type; 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 int getType() { return type; } public void setType(int type) { this.type = type; } }
package test; import java.sql.SQLException; import java.util.List; import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport; public class TempDao extends SqlMapClientDaoSupport{ public List<TempVO> getAllTemp() throws SQLException { return getSqlMapClientTemplate().queryForList("temp.getAllTemp"); } public Object insertTemp(TempVO temp) throws SQLException { return getSqlMapClientTemplate().insert("temp.insertTemp", temp); } }
package test; import java.sql.SQLException; import java.util.List; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TempService { private TempDao tempDao; public TempDao getTempDao() { return tempDao; } public void setTempDao(TempDao tempDao) { this.tempDao = tempDao; } public void getAllTemp() throws SQLException { List<TempVO> li = tempDao.getAllTemp(); System.out.println(li.size()); } public void insertTemp() throws Exception{ TempVO temp = new TempVO(); temp.setName("name"); temp.setType(1); tempDao.insertTemp(temp); if(true){ //throw new RuntimeException("dsad"); } TempVO temp2 = new TempVO(); temp2.setName("name"); temp2.setType(2); tempDao.insertTemp(temp2); } public static void main(String[] args){ try{ String[] xmls = {"applicationContext.xml"}; ApplicationContext context = new ClassPathXmlApplicationContext(xmls); TempService tempService = (TempService) context.getBean("tempProxy"); tempService.insertTemp(); }catch (Exception e) { e.printStackTrace(); } } }
5、注意事项
spring配置事务时,需要指定exception,否则默认Unchecked Exceptions回滚
获取bean时是获取代理bean
<bean id="tempProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref bean="transactionManager"/>
</property>
<property name="target">
<ref local="tempService"/>
</property>
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED,-Exception</prop>
</props>
</property>
</bean>
<bean id="tempService" class="test.TempService">
<property name="tempDao" ref="tempDao" />
</bean>
TempService tempService = (TempService) context.getBean("tempProxy");
6、事务回滚实际是对代理对象做的操作,如果是bean内部方法a调用方法b,如果方法b做了事务处理,实际不会生效,
需要再找回当前代理对象,通过代理对象调用方法b
((TempService)AopContext.currentProxy()).b();
有时需要将代理对象的exposeProxy设置成true
<property name="exposeProxy">
<value>true</value>
</property>
相关推荐
很好的spring+ibatis事务的配置文档.
struts2+spring+ibatis+mysql AOP日志管理,异常捕获 tomcat6.0+jdk1.6
struts+spring+ibatis做的一个增删改查例子
有关Struts2+Spring+Hibernate和Struts2+Spring+Ibatis的整合实例demo 原创,完全基于eclipse开发
struts2+spring+Ibatis框架包
struts+spring+ibatis的Demo struts+spring+ibatis的Demo struts+spring+ibatis的Demo
SPRING与IBATIS整合采用保留IBATIS事务,可自己控制回滚!
里面有spring+struts2+ibatis整合的jar包,
webwork+spring+ibatis很适合初学者的实例
是不是好东西你们去鉴定,SpringMVC + Spring + ibatis 可以配置多数据源,这个Demo 扩展性极强,就看你们自己如何发挥。
JSF+Spring+Ibatis示例,对学习JAVA企业应用开发有巨大的帮助!
struts2+spring+ibatis+oracle+分页搜索+上传附件实例!完整版!
struts2+spring+ibatis的小demo
spring+ibatis+oracle分页缓存源码
maven3+struts2+spring+ibatis,本来是用maven3+struts2+spring+hibernate但考虑到hibernate在多表级联查询的时候执行效率不高,所以改用性能更好不过sql比较麻烦的的ibatis,本项目只有登录和插入数据,仅供参考: ...
一个简单的struts+spring+ibatis示例的源码,在源码中包含一个简单的分布功能
struts+spring+ibatis框架集成.pdf
struts2+spring+ibatis项目实例
Struts+Spring+Ibatis整合框架搭建配置文档
搭建spring mvc+spring+ibatis所需所有jar包、亲测可用!!