1. 新建java project

2. 引入jar

3. src下新建package:com.web.model, com.web.dao, com.web.service, bean.xml

4. model下新建User.java

package com.web.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id; @Entity
public class User {
@Id
@GeneratedValue
private int id; @Column(length=32)
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

dao下新建interface  IUserDao.java

package com.web.dao;

import com.web.model.User;

public interface IUserDao {
public void save(User user);
}

dao下新建接口的实现类 UserDao.java

注意:

1. @Repository

2. @Resource 植入sessionfactory

package com.web.dao;

import javax.annotation.Resource;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.SessionAttributes; import com.web.model.User; @Repository
public class UserDao implements IUserDao { @Resource
private SessionFactory sessionFactory;
public SessionFactory getSessionFactory() {
return sessionFactory;
}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public void save(User user) {
Session session = sessionFactory.getCurrentSession();
session.save(user);
} }

service下新建接口IUserService.java

package com.web.service;
import com.web.model.User;
public interface IUserService {
public void save(User user);
}

service下新建接口实现类UserService.java

package com.web.service;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import com.web.dao.UserDao;
import com.web.model.User; @Service
@Transactional
public class UserService implements IUserService {
@Resource
private UserDao userDao; public void save(User user) {
userDao.save(user);
int i=1/0;
userDao.save(user);
} }

beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
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/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<context:annotation-config />
<context:component-scan base-package="com.web" /> <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/spring" />
<property name="username" value="root" />
<property name="password" value="root1234" />
</bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>com.web.model.User</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean> <bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean> <tx:annotation-driven transaction-manager="txManager"/>
<!-- <aop:aspectj-autoproxy proxy-target-class="true"/> -->
</beans>

5. 新建source folder:test  ,新建package:com.web.service,用于测试service

package com.web.service;

import static org.junit.Assert.*;

import javax.annotation.Resource;

import org.hibernate.SessionFactory;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.web.model.User; public class UserServiceTest {
@Resource
private SessionFactory sessionFactory; private ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml"); @Test
public void testSessionFactory() {
sessionFactory=(SessionFactory)ac.getBean("sessionFactory");
System.out.println(sessionFactory);
} @Test
public void testUserService(){
IUserService iuserService=(IUserService)ac.getBean("userService");
System.out.println(iuserService);
iuserService.save(new User());
}
}

spring,hibernate配置事务的更多相关文章

  1. Spring MVC+Spring +Hibernate配置事务,但是事务不起作用

    最近做项目,被一个问题烦恼了很久.使用Spring MVC+Spring +Hibernate开发项目,在使用注解配置事务管理,刚开始发现无论如何数据库都无法更新,但是可以从数据库查询到数据.怀疑是配 ...

  2. atitit.spring hibernate的事务机制 spring不能保存对象的解决

    atitit.spring hibernate的事务机制 spring不能保存对象的解决 sessionFactory.openSession() 不能..log黑头马sql语言.. sessionF ...

  3. spring+hibernate 配置多个数据源过程 以及 spring中数据源的配置方式

    spring+hibernate 配置多个数据源过程 以及 spring中数据源的配置方式[部分内容转载] 2018年03月27日 18:58:41 守望dfdfdf 阅读数:62更多 个人分类: 工 ...

  4. spring +spring+ hibernate配置1

    这种配置方式是将Spring .SpringMVC.Hibernate三个模块分开配置,交叉引用!hibernate连接配置使用.properties文件 web.xml配置 <web-app ...

  5. Spring+hibernate 配置实例

    转自:http://www.cnblogs.com/hongten/archive/2012/03/10/java_spring_hibernate.html 项目结构: http://www.cnb ...

  6. Spring+Hibernate配置多数据源

    配置说明 在实际应用中,经常会用到读写分离,这里就这种情况进行Spring+Hibernate的多数据源配置.此处的配置只是让读的方法操作一个数据库,写的方法操作另外一个数据库. 注:我这里的配置JD ...

  7. spring,hibernate配置事务 beans.xml

    beans.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="htt ...

  8. Spring 配置文件配置事务

    一.引入事务的头文件 xmlns:tx="http://www.springframework.org/schema/tx" http://www.springframework. ...

  9. Strut2 spring hibernate 整合

    一.创建web项目工程 wzz 点击finish 2.添加spring Jar包   AOP,Core,Persistence Core ,web jar 点击next 点击Finish 3.配置Da ...

随机推荐

  1. C/C++语言的标准库函数malloc/free与运算符new/delete的区别

    概括地说 1.malloc与free是C++/C的标准库函数,new/delete是C++的运算符,它们都可用于申请动态内存和释放内存. 2.对于非内部数据类型的对象而言,只用malloc/free无 ...

  2. H5加载优化

  3. 转:JMeter基础--逻辑控制器Logic Controller

    1.ForEach控制器 ForEach控制器在用户自定义变量中读取一系列相关的变量.该控制器下的采样器或控制器都会被执行一次或多次,每次读取不同的变量值.所以ForEach总是和User Defin ...

  4. PHP修改记录

    1. Http请求错误--20151123 参考网址:http://www.lvtao.net/dev/php-nginx-uploadfiy.html 是配置问题,修改php.ini文件: uplo ...

  5. java类的初始化

    转载:http://blog.csdn.net/moreevan/article/details/6968718 我们知道一个类(class)要被使用必须经过装载,连接,初始化这样的过程.下面先对这三 ...

  6. VBS基础篇 - 对象(5) - File对象

    VBS基础篇 - 对象(5) - File对象   描述:提供对文件所有属性的访问,从FSO对象的GetFile方法获得. 使用File对象        要用File对象模型来编程必须先用FileS ...

  7. js 数组切换图片

    <html> <head> <meta charset="utf-8" /> <title></title> <s ...

  8. Bootstrap 3 与 Foundation 5

    开发工程师, 使用 Bootstrap. 前端开发人员, 使用 Foundation. 我们来谈谈为什么. Bootstrap 与 Foundation 有许多关键的区别, 但是, 我想你只需要记住一 ...

  9. Qt之操作系统环境

    来源:http://blog.sina.com.cn/s/blog_a6fb6cc90102uy9k.html Qt中操作系统环境,官方解释如下: QStringList QProcess::syst ...

  10. ip地址的网络配置

    记录一下linux下的网络配置 3.执行命令(通过ifconfig查一下网卡): vi /etc/sysconfig/network-scripts/ifcfg-eth2 注:按字母a,代表插入. 编 ...