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>