zoukankan      html  css  js  c++  java
  • spring配置hibernate的sessionFactory

    1.首先通过dataSource来配置sessionFactory

      

    <!--读入配置文件 -->
        <bean id="propertyConfigurer"  class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
                <property name="locations">
                    <value>classpath*:jdbc.properties</value>
                </property>
        </bean>
        
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"  destroy-method="close">  
             <property name="driverClassName" value="${jdbc.driverClassName}" />
             <property name="url" value="${jdbc.url}" />
             <property name="username" value="${jdbc.username}" />
             <property name="password" value="${jdbc.password}" />
        </bean> 
        <bean id="sessionFactory"  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
            <property name="dataSource" ref="dataSource" />
            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                    <prop key="hibernate.show_sql">true</prop>
                    <prop key="hibernate.hbm2ddl">update</prop>
                </props>
            </property>
            <property name="mappingResources">
                 <list>
                    <value>classpath*:/test/domain/MyBean.hbm.xml</value>
                    <value>classpath*:/test/domain/BasicBean.hbm.xml</value>
                </list>
            </property>
        </bean>
    applicationContext.xml

    2.通过Hibernate.cfg.xml来配置sessionFactory

    <bean id="sessionFactory"
            class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
            <property name="configLocation"  value="classpath:hibernate.cfg.xml"></property>
    </bean>


    <!--<bean id="sessionFactory"
            class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
            <property name="configLocation"  value="classpath:hibernate.cfg.xml"></property>
    </bean>-->
    <?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">
    <!-- Generated by MyEclipse Hibernate Tools.                   -->
    <hibernate-configuration>
        <session-factory>
            <property name="dialect">
                org.hibernate.dialect.SQLServerDialect
            </property>
            <property name="connection.url">
                jdbc:sqlserver://localhost:1433
            </property>
            <property name="connection.username">sa</property>
            <property name="connection.password">123456</property>
            <property name="connection.driver_class">
                com.microsoft.sqlserver.jdbc.SQLServerDriver
            </property>
            <property name="myeclipse.connection.profile">
                SQLServerDriver
            </property>
            <property name="show_sql">true</property>
            <property name="connection.autocommit">true</property>
            <mapping
                resource="com/chinasoft/graduation/entity/MyCourse.hbm.xml" />
            <mapping
                resource="com/chinasoft/graduation/entity/Students.hbm.xml" />
            <mapping
                resource="com/chinasoft/graduation/entity/Course.hbm.xml" />
            <mapping
                resource="com/chinasoft/graduation/entity/Annoucement.hbm.xml" />
        </session-factory>
    
    </hibernate-configuration>
    Hibernate.cfg.xml
  • 相关阅读:
    python中的内置函数的思维导图
    练习(面试题):关于生成器函数的求和问题
    推导式, 生成器表达式
    生成器
    静态代码块
    java中内存的划分
    静态方法
    Chapter07Scanner类、Random类、ArrayList类
    泛型
    对象数组
  • 原文地址:https://www.cnblogs.com/feitianshaoxai/p/6604871.html
Copyright © 2011-2022 走看看