zoukankan      html  css  js  c++  java
  • hibernate与spring整合实现transaction

    实现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()这个类找不到。是版本的问题

  • 相关阅读:
    deepinmind(转)
    不知道数据库中表的列类型的前提下,使用JDBC正确的取出数据(转)
    shell 监控局域网的主机是否up(转)
    IntelliJ Idea中一个编译报错引发的
    Unity插件之NGUI学习(8)—— Table和NGUI尺寸转换为世界坐标系尺寸
    使用php-fpm状态页观察当前的php-fpm状态
    PHP连接Access数据库代码
    HDU 4107 线段树
    Effective C++之‘宁以pass-by-reference-to-const替换pass-by-value’
    xdebug的安装和配置方法
  • 原文地址:https://www.cnblogs.com/ouzilin/p/4986580.html
Copyright © 2011-2022 走看看