`
moxu
  • 浏览: 63792 次
  • 性别: Icon_minigender_1
  • 来自: 长沙
文章分类
社区版块
存档分类
最新评论

pring配置sessionFactory

 
阅读更多


方法1:

第一步: 在spring文件中配置如下:

<!-- 配置sessionFactory -->
 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  <property name="configLocation">
   <value>classpath:hibernate.cfg.xml</value>
  </property> 
 </bean>

 

第二步:配置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="hibernate.connection.url">jdbc:mysql://localhost/spring_hibernate_2</property>
  <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
  <property name="hibernate.connection.username">root</property>
  <property name="hibernate.connection.password">root</property>
  <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
  <property name="hibernate.show_sql">true</property>
  <property name="hibernate.current_session_context_class">thread</property>
  <!--
  <property name="hibernate.current_session_context_class">jta</property>
   -->  
  <mapping resource="com/bjsxt/usermgr/model/User.hbm.xml"/>
  <mapping resource="com/bjsxt/usermgr/model/Log.hbm.xml"/>
 </session-factory>
</hibernate-configuration>

 

 

方法2:用AnnotationSessionFactoryBean类来配置sessionFactory

  <bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="annotatedClasses">
     <list>
      <value>com.bjsxt.model.User</value>
      <value>com.bjsxt.model.Log</value>
     </list>
     </property>
     <property name="hibernateProperties">
     <props>
      <prop key="hibernate.dialect">
        org.hibernate.dialect.MySQLDialect
       </prop>
      <prop key="hibernate.show_sql">true</prop>
     </props>
  </property>
 </bean>

 

分享到:
评论

相关推荐

    spring配置sessionFactory(spring3.2.3+hibernate4.2.2)

    本文将详细讲解如何在Spring 3.2.3版本中配置SessionFactory,以便整合Hibernate 4.2.2,实现对数据库操作的高效管理。SessionFactory是Hibernate的核心组件,它负责创建Session对象,而Session则是与数据库交互的...

    使用 HibernateSessionFactory 类

    5. **创建SessionFactory**: 最后,使用配置对象创建`SessionFactory`: ```java ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder(config).build(); SessionFactory sessionFactory = ...

    一个模拟Spring将SessionFactory注入到HibernateTemplate的小例子

    1. **配置SessionFactory**:在Spring的配置文件(如applicationContext.xml)中,我们需要配置Hibernate的SessionFactory。这通常涉及设置数据源、Hibernate配置文件路径、映射文件等信息。例如: ```xml ...

    HibernateSessionFactory 代码

    `SessionFactory`的创建是基于Hibernate的配置文件(通常为`hibernate.cfg.xml`)和映射文件(`.hbm.xml`)。配置文件包含了数据库连接信息,而映射文件定义了Java对象与数据库表之间的映射关系。 创建`...

    HibernateSessionFactory.java

    HibernateSessionFactory.java

    springboot集成hibernate

    接下来,我们需要创建SessionFactory,并在`@Configuration`类中配置它: ```java import org.hibernate.SessionFactory; import org.springframework.context.annotation.Bean; import org.springframework....

    多数据源---SessionFactory

    3. **配置SessionFactory**:对于每个DataSource,我们需要创建对应的SessionFactory。这通常可以通过Spring的`LocalSessionFactoryBean`完成,通过设置`dataSource`属性指向对应的DataSource实例。 4. **使用...

    新Hibernate SessionFactory().getCurrentSession()猫腻

    4. `spring-config.xml`和`beans.xml`: 这些是Spring框架的配置文件,可能包含SessionFactory和相关bean的配置,以及事务管理的设置,这些设置将直接影响到`getCurrentSession()`的行为。 5. `ehcache_hibernate.xml...

    hibernate中的SessionFactory

    hibernate中的SessionFactoryhibernate中的SessionFactoryhibernate中的SessionFactory

    SessionFactory.getCurrentSession与openSession的区别

    至于文件名称“spring_hibernate_1”,这可能是一个包含有关Spring与Hibernate整合的教程或示例代码的压缩包,其中可能有具体的例子来展示如何在Spring中配置SessionFactory和使用这两种方法来管理数据库会话。...

    SSH整合中 hibernate托管给Spring得到SessionFactory

    整合Hibernate托管给Spring时,需要在Spring的配置文件中配置SessionFactory。通过设置&lt;prop key="hibernate.current_session_context_class"&gt;thread,意味着我们希望使用Thread级别的Session管理,这样每个线程都会...

    Hibernate中的sessionFactory

    由于SessionFactory在初始化时完成了大量的配置工作,包括解析XML映射文件、建立对象-关系映射、缓存配置等,所以它是一个重量级对象,通常在一个应用程序中只有一个SessionFactory实例,以便高效地管理和复用。...

    spring数据源配置

    配置中涉及的数据源、SessionFactory以及事务管理器都是实现这一目标的关键组成部分。这些配置不仅限于XML方式,在现代开发中更推荐使用基于注解或Java配置的方式,但理解XML配置的基本原理仍然是非常重要的。

    HibernateSessionFactory

    用于获得Session会话及关闭Session会话

    MySSH.rarSSH框架,spring管理sessionFactory

    例如,通过在Spring配置文件中定义SessionFactory bean,我们可以控制其初始化、关闭,以及事务管理等行为。 在Spring中,我们通常会使用`&lt;bean&gt;`标签来声明SessionFactory,如下所示: ```xml &lt;bean id="...

Global site tag (gtag.js) - Google Analytics