当前位置: 代码迷 >> Web前端 >> spring2.5.6调整hibernate3.3.2_Annotation声明式事务管理-第二节
  详细解决方案

spring2.5.6调整hibernate3.3.2_Annotation声明式事务管理-第二节

热度:669   发布时间:2013-01-28 11:49:56.0
spring2.5.6整合hibernate3.3.2_Annotation声明式事务管理-第二节

一、根据第一节项目稍作修改即可,看截图需要修改的地方。???


?
?二、按画红线处依次贴出代码

UserDAOImpl.java

?

package com.iskyshop.dao.impl;

import javax.annotation.Resource;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Component;

import com.iskyshop.dao.UserDAO;
import com.iskyshop.model.User;
@Component
public class UserDAOImpl implements UserDAO {
	@Resource
	private SessionFactory sessionFactory;
	
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@Override
	public void saveUser(User user) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		session.save(user);
		
//		Session session = sessionFactory.openSession();
//		session.beginTransaction();
//		session.save(user);
//		session.getTransaction().commit();
	}

}

?UserServiceImpl.java

?

package com.iskyshop.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.iskyshop.dao.UserDAO;
import com.iskyshop.model.User;
import com.iskyshop.service.UserService;
@Service
@Transactional
public class UserServiceImpl implements UserService{
	@Resource
	private UserDAO userDAO;
	
	public UserDAO getUserDAO() {
		return userDAO;
	}

	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}

	@Override
	public void saveUser(User user) {
		// TODO Auto-generated method stub
		userDAO.saveUser(user);
	}

}

?Test.java

package com.iskyshop.test;

import java.util.Date;

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

import com.iskyshop.model.User;
import com.iskyshop.service.UserService;

public class Test {
	public static void main(String args[]){
		ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
		//因为动态代理是根据接口来动态生成 如果使用转换 会导致方法的丢失,所以下边这段代码应改为。
		//UserServiceImpl usi = (UserServiceImpl) ctx.getBean("userServiceImpl");
		UserService usi = (UserService) ctx.getBean("userServiceImpl");
		User user = new User();
		user.setUsername("iskyshop");
		user.setPassword("www.iskyshop.com");
		user.setAddTime(new Date());
		usi.saveUser(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/tx 
           http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
           http://www.springframework.org/schema/aop 
           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
	<context:annotation-config />
	<context:component-scan base-package="com.iskyshop" />
	
	<!--配置数据源 -->
	<bean
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<value>classpath:jdbc.properties</value>
		</property>
	</bean>

	<bean id="dataSource" destroy-method="close"
		class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="${jdbc.driverClassName}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
	</bean>

	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="packagesToScan" value="com.iskyshop.model"></property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.format_sql">true</prop>
				<prop key="hibernate.hbm2ddl.auto">update</prop>
				<prop key="hibernate.jdbc.batch_size">20</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" />
</beans>

?

  相关解决方案