• spring和hibernate整合,事务管理


    一、spring和hibernate整合开发步骤

    1 引入jar文件,用户libarary列表如下

    //spring_core
    spring3.2.9corecommons-logging-1.2.jar
    spring3.2.9corespring-beans-3.2.9.RELEASE.jar
    spring3.2.9corespring-context-3.2.9.RELEASE.jar
    spring3.2.9corespring-core-3.2.9.RELEASE.jar
    spring3.2.9corespring-expression-3.2.9.RELEASE.jar
    
    //hibernate core
    hibernate3.6coreantlr-2.7.6.jar
    hibernate3.6corecommons-collections-3.1.jar
    hibernate3.6coredom4j-1.6.1.jar
    hibernate3.6corehibernate3.jar
    hibernate3.6corehibernate-jpa-2.0-api-1.0.1.Final.jar
    hibernate3.6corejavassist-3.12.0.GA.jar
    hibernate3.6corejta-1.1.jar
    hibernate3.6coreslf4j-api-1.6.1.jar
    hibernate3.6coreslf4j-nop-1.7.25.jar
    
    //DB_connector
    DB-connectorc3p0-0.9.1.2.jar
    DB-connectormysql-connector-java-5.1.40-bin.jar
    DB-connectorspring-jdbc-3.2.9.RELEASE.jar
    
    //AOP
    springAOPaopalliance.jar
    springAOPaspectjrt.jar
    springAOPaspectjweaver.jar
    springAOPspring-aop-3.2.9.RELEASE.jar
    
    //spring_ORM
    spring-ORMspring-orm-3.2.9.RELEASE.jar
    spring-ORMspring-tx-3.2.9.RELEASE.jar

    2 Entity/Dao/Service类代码如下

    //entity/Employee.java
    public class Employee {
        private int id;
        private String ename;
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getEname() {
            return ename;
        }
        public void setEname(String ename) {
            this.ename = ename;
        }
    }
    
    //dao/EmployeeDao.java
    public class EmployeeDao {
        private SessionFactory sessionFactory;
        
        public void setSessionFactory(SessionFactory sessionFactory) {
            this.sessionFactory = sessionFactory;
        }
        
        public void saveEmp(Employee emp){
            sessionFactory.getCurrentSession().save(emp);
        }
    
    }
    
    //service/EmployeeService.java
    public class EmployeeService {
        private EmployeeDao employeeDao;
        
        public void setEmployeeDao(EmployeeDao employeeDao) {
            this.employeeDao = employeeDao;
        }
        
        public void saveEmp(Employee emp){
            employeeDao.saveEmp(emp);
        }
    
    }
    View Code

    3 类对象和数据表关系映射配置,Employee.hbm.xml

    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC 
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
    
    <hibernate-mapping 
        package="com.huitong.entity">
        
        <class name="Employee" table="employee">
            <id name="id" column="id">
                <generator class="native"></generator>
            </id>
            
            <property name="ename" column="ename"></property>
        </class>
    
    </hibernate-mapping>

    4 Spring和hibernate/事务配置都放在一个配置文件中bean.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:p="http://www.springframework.org/schema/p"
        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-4.3.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/tx
               http://www.springframework.org/schema/tx/spring-tx.xsd
               http://www.springframework.org/schema/aop
               http://www.springframework.org/schema/aop/spring-aop.xsd">
               
               <!-- 1 配置dataSource -->
               <bean id="c3p0DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
                   <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
                   <property name="jdbcUrl" value="jdbc:mysql:///day26?useSSL=true"></property>
                   <property name="user" value="root"></property>
                   <property name="password" value="123456"></property>
                   <property name="initialPoolSize" value="2"></property>
                   <property name="maxPoolSize" value="20"></property>
                   <property name="maxStatements" value="10"></property>
                   <property name="acquireIncrement" value="2"></property>
               </bean>
               
               <!-- 2 配置sessionFactory -->
               <bean id="sessionFac" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
                   <!-- 2.1 配置数据源 -->
                   <property name="dataSource" ref="c3p0DataSource"></property>
                   
                   <!-- 2.2 数据库其他配置 -->
                   <property name="hibernateProperties">
                       <props>
                           <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
                           <prop key="hibernate.show_sql">true</prop>
                       </props>
                   </property>
                   
                   <!-- 2.3 加载映射文件 -->
                   <property name="mappingLocations">
                       <list>
                           <value>classpath:com/huitong/entity/*.hbm.xml</value>
                       </list>
                   </property>
               </bean>
               
               <!-- 3 配置dao/service/action类 -->
               <bean id="employeeDao" class="com.huitong.dao.EmployeeDao">
                   <property name="sessionFactory" ref="sessionFac"></property>
               </bean>
               
               <bean id="employeeService" class="com.huitong.service.EmployeeService">
                   <property name="employeeDao" ref="employeeDao"></property>
               </bean>
               
               <!-- 4 事务配置 -->
               <!-- 4.1 transaction manager class config -->
               <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
                   <property name="sessionFactory" ref="sessionFac"></property>
               </bean>
               
               <!-- 4.2 transaction advice config -->
               <tx:advice id="txAdvice" transaction-manager="txManager">
                   <tx:attributes>
                       <tx:method name="get*" read-only="true"/>
                       <tx:method name="query*" read-only="true"/>
                       <tx:method name="*"/>
                   </tx:attributes>
               </tx:advice>
               
               <!-- 4.3 AOP config -->
               <aop:config>
                   <aop:pointcut expression="execution(* com.huitong.service.*.save*(..))" id="pt"/>
                   <aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/>
               </aop:config>
               
    </beans>

    5 至此完成开发,简单测试如下

    public void fun1(){
            ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
            EmployeeService employeeService = (EmployeeService) ac.getBean("employeeService");
            System.out.println(employeeService);    // spring ok?
            
            Employee employee = new Employee();
            employee.setEname("good");
            employeeService.saveEmp(employee);      //hibernate ok?
        }

    二、事务配置

    1) 声明事务管理器类

    2)事务增强

    3)AOP配置拦截点

  • 相关阅读:
    OCP-1Z0-053-200题-178题-187
    OCP-1Z0-053-200题-179题-232
    OCP-1Z0-053-200题-181题-407
    OCP-1Z0-053-200题-182题-408
    OCP-1Z0-053-V13.02-408题
    OCP-1Z0-053-200题-183题-232
    OCP-1Z0-053-200题-184题-270
    OCP-1Z0-053-200题-185题-44
    OCP-1Z0-053-200题-186题-61
    OCP-1Z0-053-200题-187题-610
  • 原文地址:https://www.cnblogs.com/zhaopengcheng/p/6794961.html
Copyright © 2020-2023  润新知