Spring + SpringMVC + Hibernate配置

工程结构

这里写图片描述
config内包含
applicationContext.xml
hibernate.cfg.xml
jdbc.properties
springmvc.xml
四个配置文件以下分别是四个文件的内容
applicationContext.xml

    <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd ">  




<!-- 自动扫描与装配Bean -->
    <context:component-scan base-package="com.zit" />

    <context:annotation-config />

    <context:property-placeholder location="classpath:config/jdbc.properties"/>  

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" >  
        <!-- 数据连接信息 -->
        <property name="jdbcUrl" value="${jdbcUrl}"></property>
        <property name="driverClass" value="${driverClass}"></property>
        <property name="user" value="${user}"></property>
        <property name="password" value="${password}"></property>     
    </bean>  

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" >  
        <property name="dataSource" ref="dataSource"></property>  

        <!-- 指定hibernate的映射实体类位置 -->
        <property name="mappingLocations" value="classpath:com/zit/entities/Student.hbm.xml"></property>
         <!-- 指定hibernate的配置文件位置 -->
        <property name="configLocation" value="classpath:config/hibernate.cfg.xml"></property> 
    </bean> 
     <!-- 配置一个事务管理器 -->  
    <bean id="transactionManager"  
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
        <property name="sessionFactory" ref="sessionFactory" />  
    </bean>  

    <tx:annotation-driven transaction-manager="transactionManager"/>

</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>

        <!-- SQL dialect 方言-->
        <property name="dialect">org.hibernate.dialect.SQLServerDialect</property>

        <property name="show_sql">true</property>

        <property name="hbm2ddl.auto">update</property>
        <!-- 映射数据库对应实体类:注解生效方式生效方式 --> 
        <!-- <mapping class="Entity.Student"/> -->

    </session-factory>

</hibernate-configuration>

jdbc.properties

driverClass = com.microsoft.sqlserver.jdbc.SQLServerDriver
jdbcUrl = jdbc:sqlserver://localhost:1433;database=test
user = sa
password = Rfid123456

springmvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd "> 

    <!-- 注解扫描包,注意换成自己的路径 -->  
    <context:component-scan base-package="com.zit">  
        <!-- 只扫描@Controller的部分 -->  
        <!-- <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>   -->
    </context:component-scan>  

    <!-- 定义视图解析器 -->  
    <bean id="viewResolver"  
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
        <property name="prefix" value="/"></property>  
        <property name="suffix" value=".jsp"></property>  
    </bean>  

</beans>  

web.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>SSH</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>

  <!-- 加载所有的配置文件 -->  
    <context-param>  
        <param-name>contextConfigLocation</param-name>  
        <param-value>classpath:config/applicationContext.xml</param-value>  
    </context-param>  
    <!-- 配置Spring监听 -->  
    <listener>  
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
    </listener>  

    <!-- 配置SpringMVC -->  
    <servlet>  
        <servlet-name>SSH</servlet-name>  
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
        <init-param>  
            <param-name>contextConfigLocation</param-name>  
            <param-value>classpath:config/springmvc.xml</param-value>  
        </init-param>  
        <load-on-startup>1</load-on-startup>  
    </servlet>  

    <servlet-mapping>  
        <servlet-name>SSH</servlet-name>  
        <url-pattern>/</url-pattern>  
    </servlet-mapping>  

    <!-- 配置字符集 -->  
    <filter>  
        <filter-name>encodingFilter</filter-name>  
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
        <init-param>  
            <param-name>encoding</param-name>  
            <param-value>UTF-8</param-value>  
        </init-param>  
        <init-param>  
            <param-name>forceEncoding</param-name>  
            <param-value>true</param-value>  
        </init-param>  
    </filter>  
    <filter-mapping>  
        <filter-name>encodingFilter</filter-name>  
        <url-pattern>/*</url-pattern>  
    </filter-mapping>  

  <!-- 加入Hibernate的过滤请求将session打开,不然在Dao层getCurrentSession()时报错 -->
    <filter>  
        <filter-name>openSessionInViewFilter</filter-name>  
        <filter-class>  
            org.springframework.orm.hibernate3.support.OpenSessionInViewFilter  
        </filter-class>  
    </filter>  

    <filter-mapping>  
        <filter-name>openSessionInViewFilter</filter-name>  
        <url-pattern>/*</url-pattern>  
    </filter-mapping>  



</web-app>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值