spring,hibernate配置事务

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());
	}
}
时间: 2024-08-13 20:13:12

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

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

atitit.spring hibernate的事务机制 spring不能保存对象的解决 sessionFactory.openSession() 不能. . log黑头马sql语言.. sessionFactory.getCurrentSession().update(user); 中间走ok兰..log黑头也有累.. 在Spring中使用Hibernate.假设我们配置了TransactionManager.那么我们就不应该调用SessionFactory的openSession()来获得S

Spring hibernate配置中mappingLocations、mappingDirecto

mappingLocations.mappingDirectoryLocations与mappingJarLocations 区别 由于spring对hibernate配置文件hibernate.cfg.xml的集成相当好, 所以,在项目中我一直使用spring的org.springframework.orm.hibernate.LocalSessionFactoryBean来取代hibernate.cfg.xml文件的功能 LocalSessionFactoryBean有好几个属性用来查找hi

spring +spring+ hibernate配置1

这种配置方式是将Spring .SpringMVC.Hibernate三个模块分开配置,交叉引用!hibernate连接配置使用.properties文件 web.xml配置 <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.

Spring+hibernate 配置实例

转自:http://www.cnblogs.com/hongten/archive/2012/03/10/java_spring_hibernate.html 项目结构: http://www.cnblogs.com/hongten/gallery/image/112469.html 使用的jar包: hibernate核心安装包:hibernate3.jarlib\rquired\*.jarlib\optional\encache-1.2.3.jar    (二级缓存) lib\test\sl

spring hibernate配置切换数据源,实现读写分离

spring的配置如下 <!-- 主数据源--> <bean id="masterDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <prope

Spring+Hibernate配置多数据源

配置说明 在实际应用中,经常会用到读写分离,这里就这种情况进行Spring+Hibernate的多数据源配置.此处的配置只是让读的方法操作一个数据库,写的方法操作另外一个数据库. 注:我这里的配置JDBC连接是放在properties文件中的,当然你也可以直接写在ApplicationCpntext.xml文件中或其他的配置方式. 配置步骤 1.配置多数据库 在jdbc.Properties中,配置两个数据库的连接.在此处的例子如下(我这里都是使用的MySql,如果要使用其他的请更换驱动): [

springmvc+spring+hibernate配置过程

开发工具:eclipse 项目目录: jar包: web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:s

Spring注解配置事务管理——问题

在上下文中配置: <!-- 配置注解驱动的Spring MVC控制器编程模型. --> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /

Strut2 spring hibernate 整合

一.创建web项目工程 wzz 点击finish 2.添加spring Jar包   AOP,Core,Persistence Core ,web jar 点击next 点击Finish 3.配置Database Driver 我使用的是JTDS jar包,jtds在配置url地址时与使用sql Seriver的url地址有点不太一样,然后直接点击Finish即可 4.添加hibernate 配置 点击next 选择使用Spring的applicationContext.xml,点击next 选