1、实现类:
package cn.com.gan.spring.database;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;
import javax.sql.DataSource;
public class UserDao implements IUserDao {
// private DataSource dataSource;
private JdbcTemplate jdbcTemplate;
// private PlatformTransactionManager tm;
//private DefaultTransactionDefinition df;
public void setDataSource(DataSource dataSource) {
jdbcTemplate = new JdbcTemplate(dataSource);
//tm=new DataSourceTransactionManager(dataSource);
//df=new DefaultTransactionDefinition();
//df.setPropagationBehavior(DefaultTransactionDefinition.PROPAGATION_REQUIRED);
}
@Override
public List<User> find(String name) {
List<Map> list = jdbcTemplate
.queryForList("select * from user where name='" + name + "'");
User user = null;
List<User> relist=new ArrayList<User>();
for (Map map : list) {
user = new User();
user.setName(map.get("name").toString());
user.setAge(Short.parseShort(map.get("age").toString()));
relist.add(user);
}
return relist;
}
@Override
public void insert(User user) {
//TransactionStatus ts=tm.getTransaction(df);
jdbcTemplate.update("insert into user(name,age) values(?,?)",
new Object[] { user.getName(), user.getAge() });
jdbcTemplate.update("insert into user(name,age) values(?,?)",
new Object[] { user.getName(), user.getAge() });
//tm.commit(ts);
}
}
其中貌似insert加入try catch 否则事务失效。例如这样
package cn.com.gan.spring.database;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;
import javax.sql.DataSource;
public class UserDao implements IUserDao {
// private DataSource dataSource;
private JdbcTemplate jdbcTemplate;
// private PlatformTransactionManager tm;
//private DefaultTransactionDefinition df;
public void setDataSource(DataSource dataSource) {
jdbcTemplate = new JdbcTemplate(dataSource);
//tm=new DataSourceTransactionManager(dataSource);
//df=new DefaultTransactionDefinition();
//df.setPropagationBehavior(DefaultTransactionDefinition.PROPAGATION_REQUIRED);
}
@Override
public List<User> find(String name) {
List<Map> list = jdbcTemplate
.queryForList("select * from user where name='" + name + "'");
User user = null;
List<User> relist=new ArrayList<User>();
for (Map map : list) {
user = new User();
user.setName(map.get("name").toString());
user.setAge(Short.parseShort(map.get("age").toString()));
relist.add(user);
}
return relist;
}
@Override
public void insert(User user) {
//TransactionStatus ts=tm.getTransaction(df);
try{
jdbcTemplate.update("insert into user(name,age) values(?,?)",
new Object[] { user.getName(), user.getAge() });
jdbcTemplate.update("insert into user(name,age) values(?,?)",
new Object[] { user.getName(), user.getAge() });
}catch(Exception e){
e.printStackTrace();
return ;
}
//tm.commit(ts);
}
}
好像事务就失效了。
2、配置文件
<?xml version="1.0" encoding="UTF-8"?>
<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.5.xsd">
<!--
<bean id="ds"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
-->
<bean id="ds" class="org.apache.commons.dbcp.BasicDataSource">
<property name="url">
<value>jdbc:mysql://localhost/test</value>
</property>
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>pass</value>
</property>
</bean>
<bean id="userdao" class="cn.com.gan.spring.database.UserDao">
<property name="dataSource">
<ref local="ds" />
</property>
</bean>
<bean id="tranmanager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="ds"></property>
</bean>
<bean id="userdaoproxy"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="proxyInterfaces">
<value>cn.com.gan.spring.database.IUserDao</value>
</property>
<property name="target" ref="userdao"></property>
<property name="transactionManager">
<ref local="tranmanager" />
</property>
<property name="transactionAttributes">
<props>
<prop key="insert">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
</beans>
getBean的时候获取代理类,及userdaoproxy。
或者
<?xml version="1.0" encoding="UTF-8"?>
<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.5.xsd">
<!--
<bean id="ds"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
-->
<bean id="ds" class="org.apache.commons.dbcp.BasicDataSource">
<property name="url">
<value>jdbc:mysql://localhost/test</value>
</property>
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>pass</value>
</property>
</bean>
<bean id="userdao" class="cn.com.gan.spring.database.UserDao">
<property name="dataSource">
<ref local="ds" />
</property>
</bean>
<bean id="tranmanager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="ds"></property>
</bean>
<bean id="interceptor"
class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager" ref="tranmanager"></property>
<property name="transactionAttributeSource"
value="cn.com.gan.spring.database.IUserDao.insert=PROPAGATION_REQUIRED">
</property>
</bean>
<bean id="userdaoproxy"
class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<value>cn.com.gan.spring.database.IUserDao</value>
</property>
<property name="target" ref="userdao"></property>
<!--
<property name="transactionAttributes">
<props>
<prop key="insert">PROPAGATION_REQUIRED</prop>
</props>
</property>
-->
<property name="interceptorNames">
<list>
<value>interceptor</value>
</list>
</property>
</bean>
</beans>