spring hibernate listener 简单配置

本文详细介绍了Spring与Hibernate框架的整合配置过程,包括Spring XML配置文件的bean定义、Hibernate的session factory配置、以及如何在web应用中通过监听器初始化业务组件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、springXML配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-4.0.xsd">
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
         <!-- <property name="dataSource">
            <ref bean="dataSource" />
        </property> --> 
        <property name="configLocation" value="classpath:hibernate.cfg.xml" />
    </bean>
    <context:component-scan base-package="com.enjoy.traffic.deal"></context:component-scan>

</beans>

二、hibernate.cfg.xml配置文件

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
    <!-- 方言 -->
    <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
    <!-- oracle驱动 -->
    <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
     <!-- JDBC URL -->
    <property name="connection.url">jdbc:oracle:thin:@192.168.10.224:1521:MCSJT</property>
     <!-- 数据库用户名-->
    <property name="connection.username">MCS_JT</property>
     <!-- 数据库密码-->
    <property name="connection.password">MCS_JT</property> 
    <!-- 设置是否显示SQL语句-->
    <property name="show_sql">true</property>
    <!-- 设置是否格式化SQL语句 -->
    <property name="format_sql">true</property>
    <!-- 设置使用线程-->
    <property name="current_session_context_class">thread</property>
    <!-- <mapping resource="hibernate/vo/user.xml"/> -->
    </session-factory>
</hibernate-configuration>

三,web监听

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
public class ServerStart implements ServletContextListener{
    @Override
    public void contextDestroyed(ServletContextEvent arg0) {
        // TODO Auto-generated method stub
    }

    @Override
    public void contextInitialized(ServletContextEvent arg0) {
        // TODO Auto-generated method stub
        System.out.println("start server ...");
        WebApplicationContext application = WebApplicationContextUtils.getWebApplicationContext(arg0.getServletContext());
        WaterDeal waterDeal = (WaterDeal)application.getBean("waterDeal");
        waterDeal.cad();
    }

}
四、hibernate动态导入

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值