跟上一篇一样的例子
去掉了注解采用xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<context:property-placeholder location="classpath:c3p0.properties"/>
<bean id="dataSources" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="initialPoolSize" value="${initialSize}"></property>
<property name="maxPoolSize" value="${maxIdle}"></property>
<property name="minPoolSize" value="${minIdle}"></property>
<property name="driverClass" value="${driverClassName}"></property>
<property name="jdbcUrl" value="${url}"></property>
<property name="user" value="${jdbcusername}"></property>
</bean>
<bean id="studentDao" class="top.demo.xmlTransaction.StudentDao" autowire="byType">
</bean>
<bean id="namedParameterJdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
<constructor-arg name="dataSource" ref="dataSources"></constructor-arg>
</bean>
<bean id="serviceTran" class="top.demo.xmlTransaction.ServiceTran" autowire="byType"></bean>
<!-- 配置事务管理器 -->
<bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<constructor-arg name="dataSource" ref="dataSources"></constructor-arg>
</bean>
<!-- 配置事务的属性 -->
<tx:advice transaction-manager="dataSourceTransactionManager" id="advice">
<tx:attributes>
<!-- 配置哪个方法名字 具有哪些属性参数 隔离级别、超时时间、传播行为等 在注解中配也是一样的 -->
<tx:method name="changeStudentInfo" isolation="READ_COMMITTED" read-only="false" timeout="3"
propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<!-- 配置事务的切点 ,在哪些类哪些方法执行时进行,于事务的属性关联在一起-->
<aop:config>
<aop:pointcut expression="execution(* top.demo.xmlTransaction.ServiceTran.changeStudentInfo(..))" id="cut"/>
<aop:advisor advice-ref="advice" pointcut-ref="cut"/>
</aop:config>
</beans>
两个关键的类
对比一下 ,其实只是去掉了注解
package top.demo.xmlTransaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
public class ServiceTran {
private StudentDao dao;
public StudentDao getDao() {
return dao;
}
public void setDao(StudentDao dao) {
this.dao = dao;
}
public void changeStudentInfo(int firstId,int sencondId) {
Student st1=dao.getOne(firstId);
Student st2=dao.getOne(sencondId);
if(st1==null||st2==null) throw new RuntimeException("没有找到该学生");
boolean res=dao.updateInfo(firstId, st2);
if(res!=false) throw new RuntimeException("修改学生信息出错");
res=dao.updateInfo(sencondId, st1);
if(res==false) throw new RuntimeException("修改学生信息出错");
}
}
package top.demo.xmlTransaction;
import java.util.HashMap;
import java.util.Map;
import javax.swing.tree.RowMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
public class StudentDao {
private NamedParameterJdbcTemplate jdbc;
public NamedParameterJdbcTemplate getJdbc() {
return jdbc;
}
public void setJdbc(NamedParameterJdbcTemplate jdbc) {
this.jdbc = jdbc;
}
public StudentDao(NamedParameterJdbcTemplate jdbc) {
this.jdbc=jdbc;
}
public StudentDao() {
}
/*
* +-----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+----------------+
| id_index | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(10) | YES | | NULL | |
| id_car | int(11) | YES | | NULL | |
| home_addr | varchar(30) | YES | | NULL | |
+-----------+-------------+------+-----+---------+----------------+
*
*
* */
public int insert(Student st) {
String sql="insert into studentinfo (name,id_car,home_addr) value (:name,:idCar,:home)";
if(st.getHome()==null||st.getHome().equals("")) st.setHome("杭州");
if(st.getName()==null||st.getName().equals("")) st.setName("test");
if(st.getIdCar()==null||st.getIdCar().equals("")) st.setIdCar("123456789");
//insert into studentinfo (name,id_car,home_addr) value ('你','123456','hz');
System.out.println(st);
//SqlParameterSource接口 BeanPropertySqlParameterSource实现类,把sql的具名参数和bean的属性映射起来
SqlParameterSource paramSource=new BeanPropertySqlParameterSource(st);
int res=jdbc.update(sql, paramSource);
return res;
}
public Student getOne(int id) {
String sql="select id_index as id,name,id_car as idCar,home_addr as home from studentinfo where id_index=:id";
//行映射对象
BeanPropertyRowMapper<Student> rowMapper=new BeanPropertyRowMapper<>(Student.class);
//具名参数 key value 的映射
Map<String, Object>paramMap =new HashMap<>();
paramMap.put("id", id);
Student student=jdbc.queryForObject(sql, paramMap, rowMapper);
return student;
}
public boolean updateInfo(int id,Student nowInfo)
{
String sql="update studentinfo set id_car=:idCar,name=:name,home_addr=:home where id_index=:id";
//update studentinfo set id_car='123456',name='测',home_addr='火星' where id_index=5;
Map<String, Object> paramMap=new HashMap<>();
paramMap.put("idCar", nowInfo.getIdCar());
paramMap.put("name", nowInfo.getName());
paramMap.put("home",nowInfo.getHome());
paramMap.put("id",id);
int res=jdbc.update(sql,paramMap);
return res>0?true:false;
}
}
测试
package top.demo.xmlTransaction;
import java.sql.SQLException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
public class MAIN {
public static void main(String argv[]) throws SQLException {
ApplicationContext app=new ClassPathXmlApplicationContext("xmltransaction.xml");
ServiceTran serviceTran=(ServiceTran) app.getBean("serviceTran");
StudentDao dao=(StudentDao) app.getBean("studentDao");
System.out.println(dao.getOne(2));
System.out.println(dao.getOne(4));
//serviceTran.changeStudentInfo(2,4);
System.out.println(dao.getOne(2));
System.out.println(dao.getOne(4));
}
}
因为故意抛了异常 所以回滚了,正常