关于spring 与Hibernate 结合使用的事务管理

applicationContext.xml  中是这样的    


xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
">
class="org.apache.commons.dbcp.BasicDataSource">

com.mysql.jdbc.Driver


jdbc:mysql://localhost:3305/structs_db


root


12345




   
   
     
        com/lucas/bean/User.hbm.xml
com/lucas/bean/Scary.hbm.xml
     

   

   
     
        hibernate.dialect=org.hibernate.dialect.HSQLDialect
     

   

 




 


 
 















 



实现类中是这样的:

package com.lucas.dao.impl;




import org.springframework.orm.hibernate3.HibernateTemplate;


import com.lucas.dao.ScaryDao;


public class ScaryDaoImpl extends HibernateTemplate implements ScaryDao {


@Override
public void tranfer(){
// TODO Auto-generated method stub
String sql1="update Scary as s set s.banlace=s.banlace+1000 where s.id=1";
String sql2="update Scary as s set s.banlace=s.banlace-1000 wdhere s.id=2";
super.getSession().createQuery(sql1).executeUpdate();
super.getSession().createQuery(sql2).executeUpdate();
}
}


以及

package com.lucas.test;


import static org.junit.Assert.*;


import java.util.List;


import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


import com.lucas.bean.Scary;
import com.lucas.dao.ScaryDao;


public class ScaryDaoImplTest {


@Before
public void setUp() throws Exception {
}


@Test
public void testTranfer() {
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
ScaryDao ud=(ScaryDao)ac.getBean("scaryDaoImpl");
ud.tranfer();
}
}

可实现事务


你可能感兴趣的:(java学习)