Spring与Hibernate的整合,不配置事务管理器,事务会自动提交(Hibernate默认手动提交)
来源:互联网 发布:百度旗下软件 编辑:程序博客网 时间:2024/05/16 08:27
数据库为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;}@Overridepublic 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")@Overridepublic 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
- Spring与Hibernate的整合,不配置事务管理器,事务会自动提交(Hibernate默认手动提交)
- Hibernate 事务管理器的事务提交
- spring整合hibernate事务管理器配置
- Spring Hibernate 单元测试配置为事务自动回滚时需要提交事务时的处理
- Hibernate的事务提交
- Spring 手动提交事务
- hibernate与spring整合:配置事务之后并不会滚的问题
- Hibernate的事务提交(ZZ)
- Spring基础五之Springmvc和hibernate事务不提交
- HIBERNATE 事务提交
- Hibernate事务未提交
- 二、hibernate提交事务
- Hibernate——ThreadLocal模式下管理的Session会在事务提交后自动关闭
- sql developer默认是不自动提交事务的,如何查询未被提交的事务
- 解决在Spring整合Hibernate配置tx事务管理器出现错误的问题
- 验证mysql的自动提交事务和手动提交事务(java版)
- Spring整合Hibernate事务
- 自动提交 与 事务
- Linux下修改.bash_profile文件后再次用CRT启动环境变量未生效的解决方法,oracle命令无法使用
- windows7下TexLive2016的安装和配置
- 《小强升职记》 读书笔记 含思维导图
- 想研究一下人才培训内容,发现英语该提高了
- Unable to schedule 'load' operation
- Spring与Hibernate的整合,不配置事务管理器,事务会自动提交(Hibernate默认手动提交)
- 线性方程求解
- io流
- altium designer 中器件原理图库中,将一个器件分成几部分是如何操作的?就是如何用part表示?
- eclipse通过svn插件导入项目后调试本地环境
- Keil修改工程名
- 修改ssh默认端口号以及禁止root账号远程登录
- SpringMVC和Spring的配置文件扫描包详解
- java随机生成指定精度小数、定长字符串------随笔