[ssh] spring-springmvc-hibernate整合之配置文件

本文介绍了如何在Java Web应用中整合Spring MVC框架与Hibernate框架。通过配置web.xml实现前端控制,利用spring-mvc.xml设置MVC注解驱动、控制器扫描、拦截器、JSON数据转换器及视图解析器等;并通过spring-hibernate.xml完成数据库连接池、Session工厂、事务管理等配置。

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

1.web.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

    <!--spring mvc-->
    <servlet>
        <servlet-name>stuManagement</servlet-name>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <!--配置文件名称及路径-->
                <param-value>classpath:config/springmvc.xml</param-value>
            </init-param>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>stuManagement</servlet-name>
        <url-pattern>/</url-pattern> 
    </servlet-mapping>
    <!---->
    
    <!--spring-hibernate-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <!--配置文件名称及路径-->
        <param-value>classpath:config/spring-hibernate.xml</param-value>
    </context-param>
    <!---->
    
    <!--字符编码-->
    <filter>
        <filter-name>characterEncodingFilter</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>characterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <!---->
    
</web-app>

2.spring-mvc配置文件:

依赖的jar包:

1. spring-framwork.jar ,commons-log.jar ,commons-dbcp.jar ,commons-pool.jar
2. com.fasterxml.jackon.annotations.jar ,jackon-core.jar ,jackon-databind.jar
<?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:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:util="http://www.springframework.org/schema/util"

       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/util
        http://www.springframework.org/schema/util/spring-util-3.0.xsd">
    
    <mvc:annotation-driven/>
    <!--控制器(bean)-->
    <context:component-scan base-package="Controller"/>
    <!---->
    
    <!--拦截器-->
    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/Management/*"/> <!-- 管理员登录拦截 -->
            <bean class="Interceptor.UserInterceptor"></bean>
        </mvc:interceptor>
    </mvc:interceptors>
    
    <!--json数据转换器-->
    <bean id="mappingJacksonHttpMessageConverter"
          class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
        <property name="supportedMediaTypes">
            <list>
                <value>application/json;charset=UTF-8</value>
            </list>
        </property>
    </bean>
    <!---->
    
    <!--视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/View/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
    <!---->

</beans>

3.spring-hibernate配置文件:

依赖的jar包:

1. hibernate-required.jar ,mysql-connection-java.jar 
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"

       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!--bean-->
    <context:component-scan base-package="Model"/>
    <!---->

    <!--注入session factory-->
    
    <!--指定JDBC要连接的数据库-->
    <bean id="DataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/数据库名"/>
        <property name="username" value="账户名"/>
        <property name="password" value="密码"/>
    </bean>
    <!---->
    
    <!--配置hibernate-->
    <bean id="SessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="dataSource" ref="DataSource"/>
        <!--配置hibernate关于数据库操作-->
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>
        </property>
        <!---->
    </bean>
    <!---->
    
    <!---->

    <!--配置事务管理机制-->
    <tx:annotation-driven/>
    <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory" ref="SessionFactory"/>
    </bean>
    <!---->
    
</beans>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值