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">
<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" />
<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。
数据库为MySQL,Spring版本3.2.1,Hibernate版本3.3.2。Spring配置文件: <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schema
老司机6517: