zoukankan      html  css  js  c++  java
  • spring整合hibernate的详细步骤

      Spring整合hibernate需要整合些什么?

      1. 由IOC容器来生成hibernate的sessionFactory.
      2. 让hibernate使用spring的声明式事务

    整合步骤:

    • 加入hibernate:

        1.导jar包

        2.配置hibernate的配置文件,hibernate.cfg.xml.(其实可以省略这个配置文件,但还是加上比较舒服):

    <?xml version="1.0"?>
    <!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
    
    <hibernate-configuration>
        <session-factory>
        <!-- 配置hibernate的基本属性 -->
            <!-- 配置数据源 ,因为需要把数据源配置到IOC容器中,所以这里不需要再配置-->
            <!-- 配置数据库方言 -->
            <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
            <!-- 显示并格式化sql语句 -->
            <property name="show_sql">true</property>
            <property name="format_sql">true</property>
            <!-- 自动生成数据库表结构 -->
            <property name="hbm2ddl.auto">update</property>
            <!-- 使用getCurrentSession方式打开会话 -->
            <property name="hibernate.current_session_context_class">thread</property>
            
            <!-- 关联的*.hbm.xml也在容器配置sessionFactory实例时在进行配置,这里也不再配置 -->
        </session-factory>
    </hibernate-configuration>

        3.编写持久化类以及对应的hbm.xml.

    • 加入Spring

        1.导入jar包.

        2.配置spring配置文件(重点,详见注释):

          beans.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"
        xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
        <!-- 导入资源文件 -->
        <context:property-placeholder location="db.properties"
            ignore-resource-not-found="false" />
        <!-- 配置c3p0连接池 -->
        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <property name="user" value="${jdbc.user}"></property>
            <property name="password" value="${jdbc.password}"></property>
            <property name="driverClass" value="${jdbc.driverClass}"></property>
            <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
            <property name="initialPoolSize" value="${jdbc.initialPoolSize}"></property>
            <property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
        </bean>
        
        <!--配置hibernate的SessionFactory实例,通过Spring提供的LocalSessionFactorybean进行配置  -->
        <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
                <!-- 配置数据源属性 -->
            <property name="dataSource" ref="dataSource"></property>
                <!-- 配置hibernate.cfg.xml的位置及名称-->
            <property name="configLocation" value="hibernate.cfg.xml"></property>
                <!-- 配置hibernate映射文件的位置及名称,可以使用通配符-->
            <property name="mappingLocations" value="com/wang/entity/*.hbm.xml"></property>
        </bean>
        <!-- 配置Spring的声明式事务 -->
            <!-- 1.配置事务管理器 -->
            <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="*"/>
                    <tx:method name="get*" read-only="true"/>
                </tx:attributes>
            </tx:advice>
            <!-- 3.配置事务切点,并把切点和事务属性关联起来 -->
            <aop:config>
                <aop:pointcut expression="execution(* com.wang.service.*.*(..))" id="pointcut"/>
                <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/>
            </aop:config>
    </beans>

    到此,配置工作就完成了,接下来可以测试一下代码,看看自动生成的数据库表.

       如果你选择不使用hibernate.cfg.xml,那么可以在上面的beans.xml中,删掉<property name="configLocation" value="hibernate.cfg.xml"></property>,同时添加以下代码: 

    <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
                    <prop key="hibernate.show_sql">true</prop>
                    <prop key="hibernate.format_sql">true</prop>
                    <prop key="hibernate.hbm2ddl.auto">update</prop>
                </props>
            </property>
  • 相关阅读:
    Netty实例
    八大排序算法Java实现
    在linux服务器上初步定位java接口耗时
    Manjaro 18.X 配置记录
    Linux下搭建ELK和初步使用
    ubuntu下docker简单安装rabbitmq
    Linux禁启用ping的方法
    ubuntu下docker简单安装mysql
    Ubuntu18优化桌面版的运行速度
    ubuntu下webpack初始化vue项目
  • 原文地址:https://www.cnblogs.com/fingerboy/p/5286340.html
Copyright © 2011-2022 走看看