码迷,mamicode.com
首页 > 编程语言 > 详细

hibernate与spring整合实现transaction

时间:2015-11-22 20:18:45      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:

实现transaction时出现了大大小小的问题,这里会一一详解。

先贴出applicationContext.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:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
      http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.0.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
           http://www.springframework.org/schema/tx
         http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
    <context:annotation-config />
    <context:component-scan base-package="com.bjsxt"/>
    <!-- <aop:aspectj-autoproxy></aop:aspectj-autoproxy> -->
    <!-- <bean id="logInterceptor" class="com.bjsxt.aop.LogInterceptor"></bean>
    <aop:config>
        
        <aop:aspect id="logAspect" ref="logInterceptor">
            <aop:before method="before" pointcut="execution(public * com.bjsxt.service..*.add(..))" />
            <aop:after method="after" pointcut="execution(public * com.bjsxt.service..*.add(..))"/>
        </aop:aspect>
    
    </aop:config> -->
    <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.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="annotatedClasses">
            <list>
                <value>com.bjsxt.model.User</value>
                 <value>com.bjsxt.model.Logs</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">
                    org.hibernate.dialect.MySQLDialect
                </prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
    </bean>
     <!-- enable the configuration of transactional behavior based on annotations -->
     <tx:annotation-driven transaction-manager="txManager"/>
   <bean id="txManager"
            class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"/>
  </bean>
</beans>

这里注意,一定要加上

xmlns:tx="http://www.springframework.org/schema/tx"
  http://www.springframework.org/schema/tx
         http://www.springframework.org/schema/tx/spring-tx-3.0.xsd

否则会报找不到annotation-driven的错误

贴出service

package com.bjsxt.service;
import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import com.bjsxt.dao.LogsDAO;
import com.bjsxt.dao.UserDAO;
import com.bjsxt.model.Logs;
import com.bjsxt.model.User;
@Component("userService")
public class UserService {
    private UserDAO userDAO;  
    private LogsDAO logsDAO;
    public LogsDAO getLogsDAO() {
        return logsDAO;
    }
    @Resource(name="thelogs")
    public void setLogsDAO(LogsDAO logsDAO) {
        this.logsDAO = logsDAO;
    }

    public void init() {
        System.out.println("init");
    }
    @Transactional
    public void add(User user) {
        userDAO.save(user);
        Logs thelogs=new Logs();
        thelogs.setMsg("add a user");
        logsDAO.sava(thelogs);
    }
    public UserDAO getUserDAO() {
        return userDAO;
    }
    @Resource(name="u")
    public void setUserDAO( UserDAO userDAO) {
        this.userDAO = userDAO;
    }
    public void destroy() {
        System.out.println("destroy");
    }
}

贴出LogsDAOImpl和UserDAOImpl其中的LogsDAOImpl

package com.bjsxt.dao.impl;
import javax.annotation.Resource;

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

import com.bjsxt.dao.LogsDAO;
import com.bjsxt.model.Logs;
@Component("thelogs")
public class LogsDAOImpl implements LogsDAO {
    private SessionFactory sessionFactory;

    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }
    
    @Resource
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    @Override
    public void sava(Logs logs) {
        // TODO Auto-generated method stub
        Session s=sessionFactory.getCurrentSession();
        s.save(logs);
    }

}

测试类

ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService service = (UserService)ctx.getBean("userService");
        System.out.println(service.getClass());
        service.add(new User("22","22"));
        ctx.destroy();

执行结果

class com.bjsxt.service.UserService$$EnhancerByCGLIB$$6dcce04e
session factory class:class org.hibernate.internal.SessionFactoryImpl
Hibernate: insert into User (password, username) values (?, ?)
user saved!
Hibernate: insert into Logs (msg) values (?)

注意的是本次使用的是spring3.1.1和hibernate4.0

如果使用的是hibernate4.3以上的话会报org.hibernate.engine.spi.sessionFactoryImplementor.getConnectinoProvider()这个类找不到。是版本的问题

hibernate与spring整合实现transaction

标签:

原文地址:http://www.cnblogs.com/ouzilin/p/4986580.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!