• Hibernate+Spring整合开发步骤


    Hibernate是一款ORM关系映射框架+Spring是结合第三方插件的大杂烩,Hibernate+Spring整合开发效率大大提升。

    整合开发步骤如下:

    第一步:导入架包:

    1、Hibernate基础包+Spring基础包(AOP代理包和cglib...)

    第二步:在spring配置文件中配置datasource(数据库连接信息要么写在hibernate.cfg.xml中;要么写在datasource中)

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
               http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
               http://www.springframework.org/schema/context
               http://www.springframework.org/schema/context/spring-context-2.5.xsd
               http://www.springframework.org/schema/tx
               http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
               http://www.springframework.org/schema/aop
               http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
    
        <!-- 设置类扫描器;自动装配Bean -->
        <context:component-scan base-package="com.msit.ssh.sh" />
    
        <!-- 引入外部属性文件 -->
        <bean
            class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="location" value="classpath:jdbc.properties"></property>
        </bean>
    
        <!-- 配置数据源 -->
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
            <!-- 配置数据源信息 -->
            <property name="url">
                <value>${connection.url}</value>
            </property>
            <property name="driverClassName">
                <value>${connection.driver_class}</value>
            </property>
            <property name="username">
                <value>${connection.username}</value>
            </property>
            <property name="password">
                <value>${connection.password}</value>
            </property>
            <!-- 最大连接数 -->
            <property name="maxActive">
                <value>${jdbc.maxactive}</value>
            </property>
            <!-- 最大空闲数 -->
            <property name="maxIdle">
                <value>${jdbc.maxidle}</value>
            </property>
            <!-- 最小空闲数 -->
            <property name="minIdle">
                <value>${jdbc.minidle}</value>
            </property>
        </bean>
    
        <!-- sessionFactory(管理hibernate sessionfactory) -->
        <bean id="sessionFactory"
            class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
            <!-- 注入数据源 -->
            <property name="dataSource" ref="dataSource" />
            <!-- 第一种方式:引入hibernate.cfg.xml -->
            <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
            
            
            
            <!-- 第二种方式:所有的hibernate配置配置在spring中 -->
            
            <!-- 配置映射文件 -->
            <!-- 
            <property name="mappingDirectoryLocations">
                <list>
                    <value>com/msit/ssh/sh/entity/User.hbm.xml</value>
                </list>
            </property> -->
            
            <!-- 配置其他选项 -->
            <!-- <property name="hibernateProperties">
                <props>
                    <prop key=""></prop>
                    <prop key="show_sql">true</prop>
                    <prop key="hbm2ddl.auto">update</prop>
                </props>
            </property> -->
        </bean>
    
        <!-- 配置hibernate事务管理器 -->
        <bean id="transactionManager"
            class="org.springframework.orm.hibernate3.HibernateTransactionManager">
            <!-- 注入sessionFactory -->
            <property name="sessionFactory" ref="sessionFactory" />
        </bean>
    
        <!-- 事务通知 -->
        <tx:advice id="txAdvice" transaction-manager="transactionManager">
            <tx:attributes>
                <!-- 配置哪些方法需要用到事务;哪些方法不需要事务 -->
                <tx:method name="*" />
                <tx:method name="get*" propagation="NOT_SUPPORTED" />
            </tx:attributes>
        </tx:advice>
    
        <!-- 配置aop -->
        <aop:config>
            <!-- 配置切入点 -->
            <aop:pointcut id="txPointcut"
                expression="execution(* com.msit.ssh.sh.service.impl.*.*(..))" />
            <!-- 配置事务通知 -->
            <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut" />
        </aop:config>
    
    
    
    
        <!-- <bean class="org.springframework.orm.hibernate3.HibernateTemplate"> 
            <property name="sessionFactory" ref="sessionFactory"></property></bean> -->
    
        <!-- <bean id="hibernatedaosuppert"
            class="org.springframework.orm.hibernate3.support.HibernateDaoSupport"
            abstract="true">
            <property name="sessionFactory" ref="sessionFactory"></property>
        </bean>
    
        <bean id="userdao" class="com.msit.ssh.sh.dao.impl.UserDaoImpl"
            parent="hibernatedaosuppert">
        </bean> -->
    
    </beans>

    hibernate.cfg.xml

    <!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
    
    <hibernate-configuration>
        <session-factory>
        
            <!-- 数据库连接信息 -->
            <!-- <property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
            <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
            <property name="connection.username">db2</property>
            <property name="connection.password">db2</property> -->
            
            
            <property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
            
            <!-- 其他配置 -->
            <property name="show_sql">true</property>
            <property name="hbm2ddl.auto">update</property>
            <mapping resource="com/msit/ssh/sh/entity/User.hbm.xml"/>
            
            
            <!-- <class-cache
                class="org.hibernate.test.legacy.Simple"
                region="Simple"
                usage="read-write"/> -->
        </session-factory>
    </hibernate-configuration>

    3、配置sessionfactory(hibernate交给sprig管理)

    里边注入数据源(datasource);再把hibernate配置文件引入进来
        或者hibernate所有配置都写在sessionfactory中(舍弃了hibernate配置文件)
    
        <!-- sessionFactory(管理hibernate sessionfactory) -->
        <bean id="sessionFactory"
            class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
            <!-- 注入数据源 -->
            <property name="dataSource" ref="dataSource" />
            <!-- 引入hibernate.cfg.xml -->
            <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
        </bean>
        

    4、dao层继承HibernateDaoSuppert(抽象类)(不能用注解);必须要标明abstract="true"

    如果使用此方式:
            
            <bean id="hibernatedaosuppert"
            class="org.springframework.orm.hibernate3.support.HibernateDaoSupport"
            abstract="true">
            //将sessionFactory注入给HibernateDaoSuppert
            <property name="sessionFactory" ref="sessionFactory"></property>
            </bean>
        
        //dao层Bean必须配置parent="hibernatedaosuppert"
        
            <bean id="userdao" class="com.msit.ssh.sh.dao.impl.UserDaoImpl"
                parent="hibernatedaosuppert">
        
            </bean>
            
        还想用注解怎么办?
            //编写一个超类继承HibernateDaoSuppert
            public class BaseHibernateDaoSuppert extends HibernateDaoSupport{
        
                @Resource
                //注入sessionFactory
                public void setMySessionFactory(SessionFactory sessionFactory){
                    this.setSessionFactory(sessionFactory);
                }
        
            }
            
            //dao层就继承超类
            @Repository("userdao")
            public class UserDaoImpl extends BaseHibernateDaoSuppert implements IUserDao 
        
        
    您可以通过点击 右下角 的按钮 来对文章内容作出评价, 也可以通过左下方的 关注按钮 来关注我的博客的最新动态。 
    
    如果文章内容对您有帮助, 不要忘记点击右下角的 推荐按钮 来支持一下哦   
    
    如果您对文章内容有任何疑问, 可以通过评论或发邮件的方式联系我: 2276292708@qq.com
    
    如果需要转载,请注明出处,谢谢!!
  • 相关阅读:
    Spring Boot学习笔记
    打造高效率的软件测试
    如何将测试结果在jenkins上发布
    如何在docker container中运行web自动化测试
    Jmeter中随机读取测试文件的内容
    如何提高UI自动化测试的质量
    mac系统上添加定时任务
    keypass口令管理实践
    GPG实践
    树的遍历
  • 原文地址:https://www.cnblogs.com/wlx520/p/4804632.html
Copyright © 2020-2023  润新知