spring+hibernate+springMVC配置文件

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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring.xml</param-value>
    </context-param>
    
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
    <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:springmvc-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Spring</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

springmvc-servlet.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:util="http://www.springframework.org/schema/util" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">

    <!-- 启动扫描所有的controller -->
    <context:component-scan base-package="com.spring.controller" />

    <!-- 完成请求和注解POJO的映射 -->
    <!-- 这两个类用来启动基于Spring MVC的注解功能,将控制器与方法映射加入到容器中 -->
    <bean
        class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    </bean>
    <bean
        class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
    </bean>
    
    <!-- web请求的拦截器 
    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/**" />
            <mvc:exclude-mapping path="/register"/>
            <mvc:exclude-mapping path="/test/*"/>
            <mvc:exclude-mapping path="/login"/>
            <mvc:exclude-mapping path="/findPwd"/>
            <mvc:exclude-mapping path="/getTRegion"/>
            <mvc:exclude-mapping path="/getIndustry"/>
            <mvc:exclude-mapping path="/getTRegionByParentId"/>
            <mvc:exclude-mapping path="/getMessageCode"/>
            <mvc:exclude-mapping path="/listRecommend"/>
            <mvc:exclude-mapping path="/news/getNewsDetail"/>
            <mvc:exclude-mapping path="/css/ceos.css"/>
            <bean class="cn.kiiwii.framework.interceptor.MyInterceptor"></bean>
        </mvc:interceptor>
    </mvc:interceptors>
    
    -->

    <!-- jsp页面解析器,当Controller返回XXX字符串时,先通过拦截器,然后该类就会在/WEB-INF/views/目录下,查找XXX.jsp文件 -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
    <!-- 表示当访问主页时自动转发到index控制器   -->
    <mvc:view-controller path="/" view-name="login"/> 
    <!-- 多段文件上传 -->
    <!-- <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="1073741824" />
    </bean>
    -->
    <!-- <mvc:default-servlet-handler /> -->
</beans>

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:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
    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
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-4.0.xsd 
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
    ">

    <!-- 载入常用的配置 -->
    <context:property-placeholder location="classpath:jdbc_hibernate.properties,classpath:druid.properties" />
    <!-- 注解扫描所有包 -->
    <!-- <context:annotation-config/> -->
    <context:component-scan base-package="com.spring.*" />

    <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${jdbc.driverClassName}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
    
    <!-- 创建sessionFactory -->
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.show_sql">
                    ${hibernate.show_sql}
                </prop>
                <prop key="hibernate.use_sql_comments">
                    ${hibernate.use_sql_comments}
                </prop>
                <prop key="hibernate.dialect">
                    ${hibernate.dialect}
                </prop>
                <prop key="hibernate.jdbc.fetch_size">
                    ${hibernate.jdbc.fetch_size}
                </prop>
                <prop key="hibernate.jdbc.batch_size">
                    ${hibernate.jdbc.batch_size}
                </prop>
                <prop key="hibernate.use_outer_join">
                    ${hibernate.use_outer_join}
                </prop>
            </props>
        </property>
        <property name="dataSource">
            <ref bean="dataSource" />
        </property>
        
        <!--  
        <property name="mappingResources">    
            <value>com/spring/model/User.hbm.xml </value>    
        </property>  
        -->

        <!-- 自动扫描注解方式配置的hibernate类文件 -->
        <!-- 
        <property name="packagesToScan">
            <list>
                <value>com.spring.model</value>
            </list>
        </property>
         -->
         <property name="packagesToScan">
            <list>
                <value>com.spring.model</value>
            </list>
        </property>
        
        <!--  <property name="annotatedClasses">
            <list>
                <value>com.spring.model.User</value>
            </list>
        </propert > -->
        
    </bean>
    
    <!-- 配置Jdbc模板 -->
    <!-- <bean id="hibernateTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean> -->
    <!-- Hibernate Template Bean配置 -->
    <!-- <bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean> -->
    <bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    
    <!-- 事务配置 start -->
    <!-- <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource">
        </property>
    </bean>

    <tx:advice id="txadvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="save*" propagation="REQUIRED" />
            <tx:method name="delete*" propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="transfer*" propagation="REQUIRED" />
            <tx:method name="upgrade*" propagation="REQUIRED" />
            <tx:method name="change*" propagation="REQUIRED" />
            <tx:method name="do*" propagation="REQUIRED" />
            <tx:method name="*" propagation="REQUIRED" read-only="true" />
        </tx:attributes>
    </tx:advice>
    <aop:config>
        <aop:pointcut expression="execution(* com.spring.manager.save(..))"
            id="txpointcut" />
        <aop:advisor advice-ref="txadvice" pointcut-ref="txpointcut" />
    </aop:config> -->
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    <!--注解方式事务管理 -->
    <!-- <tx:annotation-driven transaction-manager="transactionManager"/> -->
    <!--统配方式事务管理 -->
    <tx:advice id="txadvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="save" propagation="REQUIRED" />
            <tx:method name="delete*" propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="transfer*" propagation="REQUIRED" />
            <tx:method name="upgrade*" propagation="REQUIRED" />
            <tx:method name="change*" propagation="REQUIRED" />
            <tx:method name="do*" propagation="REQUIRED" />
            <tx:method name="*" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>
    <aop:config>
        <aop:pointcut expression="execution(* com.spring.manager.*.*(..))"
            id="txpointcut" />
        <aop:advisor advice-ref="txadvice" pointcut-ref="txpointcut" />
    </aop:config>

    <!-- 事务配置 end -->
    
    <!-- 强制使用cglib代理 -->
    <!--<aop:aspectj-autoproxy proxy-target-class="true" />  -->

</beans>

jdbc_hibernate.properties,druid.properties

#######################DB#######################

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/apartment
jdbc.username=root
jdbc.password=123
jdbc.fetch_size=50

sessionTime=2000

hibernate.show_sql=ture
hibernate.use_sql_comments=true
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.jdbc.fetch_size=50
hibernate.jdbc.batch_size=30
hibernate.use_outer_join=true
hibernate.cache.use_query_cache=true

proxool.alias=proxoolPool
proxool.houseKeepingSleepTime=30000
proxool.prototypeCount=20
proxool.minimumConnectionCount=5
proxool.maximumConnectionCount=300
proxool.maximumActiveTime=300000
proxool.trace=false
proxool.verbose=true
proxool.houseKeepingTestSql=select CURRENT_DATE
proxool.simultaneousBuildThrottle=20
proxool.statisticsLogLevel = INFO
proxool.testBeforeUse = true

jar包

6E6D4345-D8BB-466A-A5D9-2220A699FEE3.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容