Spring+hibernate,怎么关闭Spring对hibernate的事务控制

2024-12-16 17:20:13
推荐回答(1个)
回答1:

第一步:在beans.xml中配置:







第二步:什么方法上需要事务管理,就在该方法的service层上添加注解 @Transactional

@Transactional
public void save(){
tuserdao.save();
}

第三步:创建session 只能为getCurrentSession()
Session session = sessionfactory.getCurrentSession();

全文beans.xml如下:


xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">














classpath:jdbc.properties


class="org.apache.commons.dbcp.BasicDataSource">












edu.zust.model.Tuser




org.hibernate.dialect.MySQLDialect
true
true