zoukankan      html  css  js  c++  java
  • 【JavaWeb】springmvc + hibernate整合配置

    1. 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_3_0.xsd" id="WebApp_ID" version="3.0">
          <display-name>test2</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>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
          </welcome-file-list>
    
           <!-- 统一编码 解决中文乱码问题-->
        <filter>
            <filter-name>charsetEncoding</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>charsetEncoding</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
    
        <!-- spring MVC 配置-->
        <servlet>
             <servlet-name>spring</servlet-name>
             <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
             <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>
                classpath:application-config.xml<!-- spring的配置文件 -->
                </param-value>
    
             </init-param>
             <!--设置为1,优先级最高。配置容器在启动的时候就加载这个servlet并实例化-->
             <load-on-startup>1</load-on-startup>
        </servlet>
        <!-- 请求映射 -->
        <servlet-mapping>
            <servlet-name>spring</servlet-name>
            <url-pattern>*.do</url-pattern><!-- 只拦截.do为后缀的请求给spring处理 -->
        </servlet-mapping>
    
    
        </web-app>

    2. spring的配置(spring.xml)

     <?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:task="http://www.springframework.org/schema/task" xmlns:cache="http://www.springframework.org/schema/cache"
            xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc"
            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/task
                http://www.springframework.org/schema/task/spring-task.xsd
            http://www.springframework.org/schema/cache
            http://www.springframework.org/schema/cache/spring-cache.xsd
            http://www.springframework.org/schema/mvc
           http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
    
            <!-- spring 的配置 -->
            <mvc:annotation-driven>
                <mvc:message-converters register-defaults="true">
                    <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                        <constructor-arg value="UTF-8" />
                        <property name="writeAcceptCharset" value="false" />
                    </bean>
                </mvc:message-converters>
            </mvc:annotation-driven>
            <!-- 扫描包 -->
            <context:component-scan base-package="controller,entity,dao,service" />
            <!-- 注解配置 -->  
            <context:annotation-config></context:annotation-config>
            <!-- 引入hibernate配置文件文件 -->
            <import resource="spring-hibernate.xml"></import>
    
            <!-- hibernate ehcache <cache:annotation-driven cache-manager="cacheManager"></cache:annotation-driven> 
                <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"> 
                <property name="cacheManager" ref="ehcache"></property> </bean> <bean id="ehcache" 
                class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" p:configLocation="classpath:config/ehcache.xml" 
                p:shared="true"/> -->
    
        </beans>

    3. spring-hibernate配置(spring-hibernate.xml)

        <?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:jdbc="http://www.springframework.org/schema/jdbc" 
            xmlns:tx="http://www.springframework.org/schema/tx"
            xmlns:aop="http://www.springframework.org/schema/aop"
            xmlns:p="http://www.springframework.org/schema/p"
            xsi:schemaLocation="http://www.springframework.org/schema/jdbc
                http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
                http://www.springframework.org/schema/beans 
                http://www.springframework.org/schema/beans/spring-beans.xsd
                http://www.springframework.org/schema/tx 
                http://www.springframework.org/schema/tx/spring-tx.xsd
                http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
                http://www.springframework.org/schema/context 
                http://www.springframework.org/schema/context/spring-context.xsd">
        <!-- 数据源和事物的配置 -->
            <context:property-placeholder location="classpath:application.properties"/>
    
            <!-- sessionFactory -->
            <bean id="sessionFactory" 
        class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan" value="entity,service" />
        <property name="hibernateProperties">
        <props>
        <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
        <!-- <prop key="hibernate.current_session_context_class">thread</prop>  -->
        </props>
        </property>
        </bean> 
    
            <!-- dataSource -->
            <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
                destroy-method="close">
                <property name="driverClass" value="${jdbc.driverClassName}" />
                <property name="jdbcUrl" value="${jdbc.url}" />
                <property name="user" value="${jdbc.username}" />
                <property name="password" value="${jdbc.password}" />
    
                <!-- these are C3P0 properties -->
                <property name="acquireIncrement" value="${c3p0.acquireIncrement}" />
                <property name="minPoolSize" value="${c3p0.minPoolSize}" />
                <property name="maxPoolSize" value="${c3p0.maxPoolSize}" />
                <property name="maxIdleTime" value="${c3p0.maxIdleTime}" />
            </bean>
    
            <!-- 配置Hibernate事务管理器 -->
        <bean id="transactionManager"
        class="org.springframework.orm.hibernate5.HibernateTransactionManager">
          <property name="sessionFactory" ref="sessionFactory" />
           </bean> 
           <!-- 事物注解配置 -->
           <tx:annotation-driven transaction-manager="transactionManager"/>
    
    
        </beans>

    注:需要导入c3p0的包

    4. 数据库的配置(application.properties)

        #hibernate
        hibernate.max_fetch_depth=3
        hibernate.jdbc.fetch_size=50
        hibernate.jdbc.batch_size=10
        hibernate.show_sql=true
        hibernate.cache.use_second_level_cache=true
        hibernate.cache.use_query_cache=true
        hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory
    
        jdbc.driverClassName=com.mysql.jdbc.Driver
        jdbc.url=jdbc:mysql://localhost:3306/xgp?useSSL=true
        jdbc.username=root
        jdbc.password=****
        jdbc.maxActive=50
    
        c3p0.acquireIncrement=10
        c3p0.minPoolSize=3
        c3p0.maxPoolSize=200
        c3p0.maxIdleTime=6000 
  • 相关阅读:
    oracle里的tns是什么意思
    什么是联机分析处理(OLAP)
    JDBC和ODBC
    mysql 乱码
    eclipse常用快捷键和插件
    数仓项目01:准备工作
    github、maven、idea初始化一个项目
    数仓项目02:集群搭建
    数仓项目03:数据采集层(Flume+Kafka)
    Java Swing 日期控件(转载)
  • 原文地址:https://www.cnblogs.com/cnsec/p/13286766.html
Copyright © 2011-2022 走看看