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包

  • 相关阅读:
    php之面向对象(2)
    PHP ON 阿里云的环境配置攻略
    InitPHP框架搭建高可用WEB应用
    PHP移动互联网开发笔记(6)——MySQL数据库基础回顾[1]
    5 个不用 Bootstrap 的理由
    http中get与post的区别
    django的CSRF保护机制
    博客园项目开发中的难点
    centos7.5静态网页基于nginx建站(python3.6 mysql5.7 nginx安装以及配置)
    python3面向对象常用特性知识点
  • 原文地址:https://www.cnblogs.com/lusufei/p/7372789.html
Copyright © 2011-2022 走看看