`
ajax_xu
  • 浏览: 154766 次
  • 性别: Icon_minigender_1
  • 来自: 西安
社区版块
存档分类
最新评论

hibernate.cfg.xml spring 上下文

阅读更多
hibernate.cfg.xml
<?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">

<hibernate-configuration>

    <session-factory>
	   	<!--
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost/hibernate2</property>
        <property name="connection.username">root</property>
        <property name="connection.password"></property>
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
  -->	

        <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
        <property name="connection.url">jdbc:oracle:thin:@localhost:1521:MYORACLE</property>
        <property name="connection.username">scott</property>
        <property name="connection.password">tiger</property>
      	<property name="dialect">org.hibernate.dialect.OracleDialect</property>
              

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>
	

        <!-- Enable Hibernate's automatic session context management -->
        <property name="current_session_context_class">thread</property>

        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>
        <property name="format_sql">true</property>

        <!-- Drop and re-create the database schema on startup
        <property name="hbm2ddl.auto">update</property>
		 -->
        <!--   
        <mapping resource="com/hibernate/entity/user.hbm.xml"/>
        -->

		<mapping class="com.hibernate.entity.many2one.Group"/>
		<mapping class="com.hibernate.entity.many2one.User"/>
		 <!-- 
		<mapping class="com.hibernate.entity.many2many.Student"/>
		<mapping class="com.hibernate.entity.many2many.Teacher"/>
		 -->
    </session-factory>

</hibernate-configuration>


HibernateUtil.java 获取sessionFactory
package JTest;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {
	 private static final SessionFactory sessionFactory = buildSessionFactory();

	    private static SessionFactory buildSessionFactory() {
	        try {
	            // Create the SessionFactory from hibernate.cfg.xml
//	            return new Configuration().configure().buildSessionFactory(); 创建xml方法的sessionFactory
	            return new AnnotationConfiguration().configure().buildSessionFactory();
	        }
	        catch (Throwable ex) {
	            // Make sure you log the exception, as it might be swallowed
	            System.err.println("Initial SessionFactory creation failed." + ex);
	            throw new ExceptionInInitializerError(ex);
	        }
	    }

	    public static SessionFactory getSessionFactory() {
	        return sessionFactory;
	    }

	}



spring 上下文
ClassPathXmlApplicationContext  ctx =  new ClassPathXmlApplicationContext("beans.xml");
		UserService u = (UserService) ctx.getBean("userService");
分享到:
评论

相关推荐

    Hibernate的配置文件

    可以设置`current_session_context_class`属性来选择Session的上下文。例如: ```xml &lt;!-- Other properties --&gt; &lt;property name="current_session_context_class"&gt;thread ``` 在实际开发中,`hibernate....

    webwork-spring-hibernate.rar_spring_webwork

    5. **部署描述符**:在web.xml中,需要配置DispatcherServlet、Filter(如Spring的ContextLoaderListener和DelegatingFilterProxy)来启动Spring应用上下文,并将请求转发给WebWork。 6. **测试与调试**:配置完成...

    Struts2.Hibernate3.2.Spring2.0整合续一

    在SSH整合中,Spring作为核心框架,负责整个应用的上下文管理和依赖注入,可以将Struts2和Hibernate的组件纳入管理,提高代码的可测试性和可维护性。 SSH整合的步骤通常包括以下部分: 1. 配置Struts2:添加Struts2...

    整合Struts2.2与Hibernate3.6需要的Spring3.1相关JAR文件

    它们各自负责应用程序的不同层面:Struts2处理MVC(Model-View-Controller)架构中的控制层,Hibernate专注于对象关系映射(ORM),而Spring则提供了全面的应用程序上下文管理以及依赖注入功能。将这三个框架整合在...

    sturts2.3.15.1+Spring3.2+hibernate3.6整合

    1. 配置文件:如web.xml(Web应用部署描述符),struts.xml(Struts2配置),spring-context.xml(Spring应用上下文配置),hibernate.cfg.xml(Hibernate配置)。 2. 模型类(实体类):对应数据库中的表,实现了...

    Struts1.x Spring2.x Hibernate3.x DWR2.x整合工具文档v1.00

    - **配置Spring ContextLoaderListener**:通过`ContextLoaderListener`来加载Spring的应用上下文。 ```xml &lt;param-name&gt;contextConfigLocation &lt;param-value&gt;classpath:applicationContext*.xml org....

    SSH集成步骤 SSH集成步骤

    - 创建`applicationContext.xml`,作为Spring的应用上下文配置文件,用于定义Bean的配置、依赖注入等。 - 在`web.xml`中配置Spring的DispatcherServlet,使其作为Web应用的前端控制器。 4. **添加Hibernate支持**...

    s2sh整合步骤及配置文件配置

    - 在`web.xml`中配置Spring上下文的初始化参数`contextConfigLocation`,指明`applicationContext.xml`的位置。 - 添加Spring的上下文监听器`ContextLoaderListener`,确保Spring在应用启动时加载配置。 - 在`...

    jbpm4整合struts2+spring2.5+hibernate3.3

    配置文件是整合的关键部分,例如jbpm.cfg.xml是jbpm的主要配置文件,通常需要导入多个子配置,如jbpm.spring.default.cfg.xml、jbpm.spring.hibernate.cfg.xml等,这些配置文件会定义流程引擎、Spring上下文、...

    struts+spring+hibernate整合配置笔记

    - 配置struts.properties或struts.xml,指定Spring的上下文加载器。 - 在struts-config.xml中配置Action和ActionForm,使用Spring的`&lt;controller&gt;`标签来引用由Spring管理的Action类。 2. **配置Spring** - 创建...

    整合Struts2.3.20 Spring4.1.3 Hibernate4.3.7 所需jar包

    2. 配置web.xml,添加过滤器和监听器以初始化Spring上下文和Struts2。 3. 创建Spring配置文件,定义bean以及依赖注入。 4. 配置Struts2的struts.xml文件,定义Action类和结果页面。 5. 配置Hibernate的hibernate.cfg...

    [浪曦原创]Struts2.Hibernate3.2.Spring2.0整合续二(风中叶)

    这些框架分别承担着不同的职责:Struts2负责表现层管理,Hibernate3.2处理持久层操作,而Spring2.0则作为整体的依赖注入容器和应用上下文,负责业务逻辑的协调和管理。 Struts2是MVC(模型-视图-控制器)架构模式的...

    ssh集成开发样板工程(struts2+spring+hibernate)

    2. 集成Spring:在web.xml中配置ContextLoaderListener,加载Spring应用上下文。将Action类声明为Spring的bean,实现依赖注入。 3. 配置Hibernate:设置hibernate.cfg.xml,包括数据库连接信息、方言、缓存等。编写...

    Struts+Spring+Hibernate补充内容

    这段配置指定了Spring的上下文位置,即`/WEB-INF/classes/applicationContext-*.xml`,这正是前面提到的分离配置文件。 **2.2 Hibernate配置示例** 在`applicationContext-common.xml`中配置Hibernate的...

    使用Spring、Hibernate、Struts的一些错误总结.txt

    - 使用日志记录工具,如log4j,来跟踪异常发生的上下文,以便更准确地定位问题源。 ### 2. Hibernate Configuration Error 异常信息显示无法从URL `file:src/hibernate.cfg.xml` 配置Hibernate。这通常是因为配置...

    Struts2.3.4.1+Hibernate4.1.7+Spring3.1.2

    例如,Struts2的核心库负责处理HTTP请求,Spring的上下文和依赖注入库管理组件,而Hibernate的库则处理数据库交互。开发者需要将这些jar包添加到项目的类路径中,以确保系统能够识别并正确使用它们。 为了集成这三...

    Struts+Spring+Hibernate 整合教程.rar

    4. 使用Spring的WebApplicationContextLoaderListener监听器加载Spring上下文。 5. 在Struts的Action中,通过Spring的ApplicationContext获取Service Bean,进行业务操作。 6. 在Service中,通过SessionFactory获取...

    struts1+spring+hibernate所需jar包

    配置文件(如`struts-config.xml`、`spring-servlet.xml`、`hibernate.cfg.xml`)也需要正确设置,以定义应用程序的行为和组件间的交互。 总的来说,"struts1+spring+hibernate所需jar包"是Java Web开发中的基础...

    junit+hibernate3.2+spring2.5工程

    接着,配置Spring的上下文文件(applicationContext.xml),声明数据源、事务管理器和DAO工厂。最后,编写测试类,利用Spring的ApplicationContext获取Bean并进行测试。 5. 测试实践:在"testUniversal"目录下的...

Global site tag (gtag.js) - Google Analytics