zoukankan      html  css  js  c++  java
  • Spring整合Hibernate

    Spring配置文件

    <?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"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
    
    
    
        <!-- 导入资源文件 -->
        <context:property-placeholder location="classpath:db.properties" />
    
        <!-- 配置 C3P0 数据源 -->
        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <property name="user" value="${user}"></property>
            <property name="password" value="${password}"></property>
            <property name="jdbcUrl" value="${jdbcUrl}"></property>
            <property name="driverClass" value="${driverClass}"></property>
    
            <property name="initialPoolSize" value="${initPoolSize}"></property>
            <property name="maxPoolSize" value="${maxPoolSize}"></property>
        </bean>
    
        <!-- 配置 SessionFactory -->
        <bean id="sessionFactory"
            class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
            <property name="dataSource" ref="dataSource"></property>
            <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
            <property name="mappingLocations" value="classpath:com/entities/*.hbm.xml"></property>
        </bean>
        
        <!-- 配置 Spring 的声明式事务 -->
        <!-- 1. 配置 hibernate 的事务管理器 -->
        <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
            <property name="sessionFactory" ref="sessionFactory"></property>
        </bean>
    
        <!-- 2. 配置事务属性 -->
        <tx:advice id="txAdvice" transaction-manager="transactionManager">
            <tx:attributes>
                <tx:method name="*get*" read-only="true"/>
                <tx:method name="*"/>
            </tx:attributes>
        </tx:advice>
        
        <!-- 3. 配置事务切入点, 再把事务属性和事务切入点关联起来 -->
        <aop:config>
            <aop:pointcut expression="execution(* com.service.*.*(..))" id="txPointcut"/>
            <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
        </aop:config>
        
    
    </beans>

    Hibernate配置文件

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE hibernate-configuration PUBLIC
            "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
            "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
    <hibernate-configuration>
        <session-factory>
        
            <!-- 配置 hibernate 的基本属性 -->
        
            <!-- 方言 -->
            <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
            
            <!-- 是否显示及格式化 SQL -->
            <property name="hibernate.show_sql">true</property>
            <property name="hibernate.format_sql">true</property>
        
            <!-- 生成数据表的策略 -->
            <property name="hibernate.hbm2ddl.auto">update</property>
            
            <!-- 二级缓存相关 -->
        </session-factory>
    </hibernate-configuration>

    web.xml

      <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
      </context-param>
      <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>

    目录结构

    jar包

  • 相关阅读:
    简单的四则运算
    11月28日-课堂测验
    01-实现简单的登录界面
    06-继承与多态-动手动脑
    04-String-动手动脑
    04-String
    03-类与对象-动手动脑
    iOS 审核加急通道使用--转载来源--有梦想的蜗牛
    多线程 队列的简单操作
    随机排列
  • 原文地址:https://www.cnblogs.com/lusufei/p/7372789.html
Copyright © 2011-2022 走看看