SSH2事务管理(transactionproxyfactorybean)

废话不多说,开始SSH 事务的配置

 

1、JSP页面发送请求到server端

  <!-- 后面的action不能少,它会根据action前面的字符串去匹配是否有对应的请求 -->
  <form action="personAdd.action" method="post" enctype="multipart/form-data">
    用户名:<input type="text" name="name"><br>
    密码:<input type="text" name="password"><br>
    年龄:<input type="text" name="age"><br>
    性别:<input type="text" name="sex"><br>
    文件:<input type="file" name="myfile"><br>
    <input type="submit" value="提交">
    <input type="reset" value="重置">
 </form>

 

2、被struts2拦截器拦截

   a)struts配置文件

<!-- 如果指明了method方法,则调用该类的addperson方法,默认是调用execute方法 -->
<action name="personAdd" class="personAddSpring" method="addPerson">
<result name="success" type="redirectAction">personList</result>
</action>

   b)到spring配置文件中找到具体的类,然后注入

<bean id="personAddSpring" class="hb.person.action.PersonAction"></bean> 

 

3、找到PersonAction类的addPerson方法

public String addPerson(){
	Person person = new Person();
	person.setAge(new Integer(this.age));
	person.setName(this.name);
	try {
		person.setPassword(MD5Util.EncoderPwdByMd5(this.password));
	} catch (UnsupportedEncodingException e) {
		e.printStackTrace();
	} catch (NoSuchAlgorithmException e) {
		e.printStackTrace();
	}
	person.setBirthday(Calendar.getInstance().getTime());
	if(this.myfile != null){
		System.out.println(this.myfileFileName);
//			File temp = new File("c:\\huangbiao");
//			try {
//				FileUtils.copyFile(this.myfile, temp);
//			} catch (IOException e) {
//				e.printStackTrace();
//			}
		try {
			FileInputStream fis = new FileInputStream(this.myfile);
			byte[] buf = new byte[fis.available()];
			while(fis.read(buf)!=-1){
			}
			person.setAttach(buf);
			fis.close();
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}	
	IPersonService personService = (IPersonService)BeanUtil.getBean("ipersonService");
	personService.addPerson(person);
	return "success";
}

 

得到的personService保存“对象”,里面的内容就是操作数据库了

 

4、写一个得到service的类

package hb.util;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class BeanUtil {

	private static ApplicationContext applicationContext;
	private static String[] configs = {"E:\\workspace\\mycode\\WebRoot\\WEB-INF\\config\\spring\\applicationContext_bean_person.xml", 
				"E:\\workspace\\mycode\\WebRoot\\WEB-INF\\config\\spring\\applicationContext.xml"};
	public static Object getBean(String bean){
		System.out.println("test");
		if(null == applicationContext){
			//使用根据工程名称来读取文件的方式得到context,因为这个能接收参数
			applicationContext =  new FileSystemXmlApplicationContext(configs);
			
		}
		
		return applicationContext.getBean(bean);
	}
	
	public static void main(String[]args){
		applicationContext =  new FileSystemXmlApplicationContext("/WebRoot/WEB-INF/config/spring/applicationContext*.xml");
	}
}

 5、在spring配置文件中配置事务

<bean id="dataSource"
	class="org.apache.commons.dbcp.BasicDataSource">
	<property name="driverClassName"
		value="oracle.jdbc.driver.OracleDriver">
	</property>
	<property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"></property>
	<property name="username" value="huangbiao"></property>
	<property name="password" value="huangbiao"></property>
	<property name="maxActive" value="100"></property>
	<property name="maxIdle" value="30"></property>
	<property name="maxWait" value="500"></property>
	<property name="defaultAutoCommit" value="true"></property>
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
	<property name="dataSource" ref="dataSource"></property>
	<property name="hibernateProperties">
		<props>
			<prop key="hibernate.dialect">
				org.hibernate.dialect.Oracle9Dialect
			</prop>
			<prop key="hibernate.show_sql">true</prop>
		</props>
	</property>

	<property name="mappingResources">
		<list>
			<value>hb/person/model/Person.hbm.xml</value>
			<value>hb/dept/model/Dept.hbm.xml</value>
		</list>
	</property>
</bean>

<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
	<property name="sessionFactory">
			<ref local="sessionFactory"/>
	</property>
</bean>	

<!-- 配置事务开始 -->
<bean id="ipersonServiceTarget" class="hb.person.service.imp.PersonServiceImp">
	<property name="persondao" ref="ipersonDao"></property>
	<property name="deptdao" ref="ideptDao"></property>
</bean>

<bean id="ipersonDao" class="hb.person.dao.imp.PersonDaoImp">
	<property name="sessionFactory">
		<ref bean="sessionFactory"/>
	</property>
</bean>

<bean id="ipersonService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
	<property name="transactionManager">
		<ref bean="transactionManager"/>
	</property>
	<property name="target">
		<ref local="ipersonServiceTarget"/>
	</property>
	<property name="transactionAttributes">
		<props>
			<prop key="*">PROPAGATION_REQUIRED</prop>
		</props>
	</property>
</bean>
<!-- 配置事务结束 -->

 

 6、在PersonServiceImp中注入了两个dao(persondao和deptdao),也就是两张表需要在这个方法中进行操作

 

备注:上面的配置文件还少了一张表的配置,这个是我为了测试添加的

 

<!-- model dept -->
<bean id="ideptServiceTarget" class="hb.dept.service.imp.DeptServiceImp">
	<property name="deptDao" ref="ideptDao"></property>
</bean>

<bean id="ideptDao" class="hb.dept.dao.imp.DeptDaoImp">
	<property name="sessionFactory">
		<ref bean="sessionFactory"/>
	</property>
</bean>

<bean id="ideptService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
	<property name="transactionManager">
		<ref bean="transactionManager"/>
	</property>
	<property name="target">
		<ref local="ideptServiceTarget"/>
	</property>
	<property name="transactionAttributes">
		<props>
			<prop key="*">PROPAGATION_REQUIRED</prop>
		</props>
	</property>
</bean>

 

如果只是一张表的操作可以像dept表这样配置,如果是多张表操作可以像person表那样配置

 

7、实现多表操作的具体实现

package hb.person.service.imp;

import hb.dept.dao.IDeptDao;
import hb.dept.model.Dept;
import hb.dept.service.IDeptService;
import hb.person.dao.IPersonDao;
import hb.person.model.Person;
import hb.person.service.IPersonService;
import hb.util.BeanUtil;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

import org.hibernate.HibernateException;


public class PersonServiceImp implements IPersonService {

	private IPersonDao persondao;
	private IDeptDao deptdao;
	
	public IPersonDao getPersondao() {
		return persondao;
	}

	public void setPersondao(IPersonDao persondao) {
		this.persondao = persondao;
	}

	public void addPerson(Person person) {
		persondao.addPerson(person);
		
		IDeptService service = (IDeptService)BeanUtil.getBean("ideptService");
		Dept dept = new Dept();
		String temp = "2";
		//new Integer("dsaf");//模拟操作出现异常
		dept.setDmanager(temp);
		dept.setDname(temp);
		dept.setNote(temp);
		service.addDept(dept);
		//new Integer("dsfads");测试事务
	}

	public IDeptDao getDeptdao() {
		return deptdao;
	}

	public void setDeptdao(IDeptDao deptdao) {
		this.deptdao = deptdao;
	}

	public Person findPersonById(String id) {
		return persondao.findPersonById(id);
	}

	public List findPersonList(int start, int end) {
		return persondao.findPersonList(start, end);
	}

	@Override
	public List findPersonList(int start, int end, Person person) throws HibernateException, SecurityException, IllegalArgumentException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
		return  persondao.findPersonList(start, end, person);
	}

	@Override
	public void updatePerson(Person person) {
		persondao.updatePerson(person);
	}

	@Override
	public void deletePersonByKeyid(String keyid) {
		persondao.deletePersonByKeyid(keyid);
	}

}

 

 备注:查看addPerson()方法,里面模拟了添加dept对象,如果出现异常,两张表都操作不成功

你可能感兴趣的:(transaction)