Spring与Hibernate的整合,不配置事务管理器,事务会自动提交(Hibernate默认手动提交)

数据库为MySQL,Spring版本3.2.1,Hibernate版本3.3.2。

Spring配置文件:

<?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-3.0.xsd">  
       
    <!-- 数据源 -->  
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">  
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>  
        <property name="url" value="jdbc:mysql://localhost:3306/test"/>  
        <property name="username" value="root"/>  
        <property name="password" value="xxxxxx"/>
        <property name="initialSize" value="1"></property>
        <property name="maxActive" value="100"></property>
        <property name="maxIdle" value="2"></property>
        <property name="minIdle" value="1"></property>
    </bean>  
      
    <!-- sessionFactory -->  
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
        <property name="dataSource" ref="dataSource" />  
        <property name="mappingResources">  
            <list> 
                <value>com/zzj/entity/Config.hbm.xml</value>  
            </list>  
        </property>
        <property name="hibernateProperties">  
            <value>  
                hibernate.dialect=org.hibernate.dialect.MySQLDialect
                hibernate.show_sql=true
            </value>  
        </property>  
    </bean> 
    
    <!-- Dao --> 
    <bean id="configDao" class="com.zzj.dao.ConfigDao">
    	<property name="sessionFactory" ref="sessionFactory"/>
    </bean>
      
</beans>  

实体类:

package com.zzj.entity;

public class Config {
	private Integer id;
	private String name;
	private String value;
	
	public Config() {
	}
	
	public Config(Integer id, String name, String value) {
		this.id = id;
		this.name = name;
		this.value = value;
	}

	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	public String getValue() {
		return value;
	}
	public void setValue(String value) {
		this.value = value;
	}

	@Override
	public String toString() {
		return "Config [id=" + id + ", name=" + name + ", value=" + value + "]";
	}
	
}
Hibernate映射文件:
<?xml version="1.0" encoding="UTF-8"?>    
<!DOCTYPE hibernate-mapping PUBLIC    
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"    
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
<hibernate-mapping>  
    <class name="com.zzj.entity.Config" table="config">
        <id name="id" column="id">
            <generator class="identity" />
        </id>
        <property name="name" column="name" />
        <property name="value" column="value" />
    </class>
</hibernate-mapping>
Dao类:
package com.zzj.dao;

import java.sql.SQLException;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class ConfigDao extends HibernateDaoSupport {
	
	public void save(){
		getHibernateTemplate().execute(new HibernateCallback<Integer>() {

			@SuppressWarnings("deprecation")
			@Override
			public Integer doInHibernate(Session session) throws HibernateException, SQLException {
				System.out.println("autoCommit:" + session.connection().getAutoCommit());
				
				Query query = session.createQuery("update Config set value = :value where name = :name");
				query.setString("name", "Tom");
				query.setString("value", "11");
				return query.executeUpdate();
			}
		});
	}
}
测试类:
package com.zzj;

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

import com.zzj.dao.ConfigDao;

public class Test {
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-common.xml");
		ConfigDao configDao = (ConfigDao) context.getBean("configDao");
		configDao.save();
	}
}

Spring没有配置事务管理器,Dao类中也没有开启和提交事务的操作,事务也提交了!

注意:Dao类中输出的autoCommit为true。





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值