`
moxu
  • 浏览: 64502 次
  • 性别: 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>

 

分享到:
评论

相关推荐

    简单myeclipse工程搭建

    这个类负责初始化SessionFactory,通过调用Configuration类的configure()方法配置并构建SessionFactory。 8. 创建业务逻辑类: 建立一个名为HibernateTest的类,位于com.swh.view包下。这个类包含了添加、查询、...

    hibernateDaoSupport类的运用实例

    在Spring的配置文件(例如`spring.xml`)中,我们需要为这个类配置一个`SessionFactory`: ```xml &lt;property name="sessionFactory"&gt; ``` 这里需要注意的是,`sessionFactory`的配置必须通过`&lt;ref&gt;`元素引用...

    Hibernate3使用经验

    1. **初始化配置**:配置 `SessionFactory`。 ```java Configuration config = new Configuration(); config.configure(); SessionFactory sf = config.buildSessionFactory(); ``` 2. **获取 Session**:通过...

    spring的学习文章

    4. **配置类**:配置Spring框架,如配置数据源、事务管理器等。 通过这种方式,可以充分利用Spring框架的强大功能,比如依赖注入、事务管理等,使得代码更加整洁、易于维护。 #### 总结 Spring框架是一个非常强大...

    spring chm文档

    provided that you do not charge any fee for such copies and further provided that each copy contains this Copyright Notice, whether distributed in print or electronically. ------------------------...

    wicket 学习文档

    此外,Wicket 提供了一种基于规则的配置方式,减少了 XML 配置文件的使用量,简化了应用配置过程。对于开发者来说,Wicket 的学习曲线相对较低,因为它的开发方式与传统的客户端/服务器 (C/S) 应用类似,使得开发者...

    spring_MVC源码

    弃用了struts,用spring mvc框架做了几个项目,感觉都不错,而且使用了注解方式,可以省掉一大堆配置文件。本文主要介绍使用注解方式配置的spring mvc,之前写的spring3.0 mvc和rest小例子没有介绍到数据层的内容,...

    java 学习笔记

    - **Hibernate**:深入理解ORM(对象关系映射)的概念,掌握Hibernate的核心API,如Session、SessionFactory等。 - **Spring框架**:学习Spring的核心模块,如IoC容器、AOP、事务管理等;并探索Spring MVC的使用方法...

    java学习笔记

    - Session、SessionFactory、Transaction等核心API。 - HQL查询语言:基于对象的查询方式。 4. **PL/SQL**: - 存储过程、函数、触发器的编写与调用。 - 数据库事务管理:COMMIT、ROLLBACK。 #### 三、Web开发...

Global site tag (gtag.js) - Google Analytics