SpringMVC+Spring+Mybatis整合配置文件小记

来源:互联网 发布:网络电视剧上瘾全集 编辑:程序博客网 时间:2024/06/05 10:53

web.xml

<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"         version="3.0">    <display-name>SSM Web Application</display-name>      <!-- 字符编码过滤器 -->    <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>        <servlet-name>spring-mvc</servlet-name>    </filter-mapping>    <!-- The front controller of this Spring Web application, responsible for handling all application requests -->    <servlet>        <servlet-name>spring-mvc</servlet-name>        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>        <init-param>            <param-name>contextConfigLocation</param-name>            <param-value>WEB-INF/applicationContext-mvc.xml</param-value>        </init-param>        <load-on-startup>1</load-on-startup>    </servlet>    <!-- Map all *.spring requests to the DispatcherServlet for handling -->    <servlet-mapping>        <servlet-name>spring-mvc</servlet-name>        <url-pattern>/</url-pattern>    </servlet-mapping>    <!-- Spring 容器加载 通过<listener>加载,SpringMVC通过<servlet>加载,见上。(这个解释如果有误请指正!)-->    <listener>        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>    </listener>    <listener>        <listener-class>org.springframework.web.context.request.RequestContextListener        </listener-class>    </listener>    <session-config>        <session-timeout>30</session-timeout>    </session-config>    <!-- 欢迎页面 -->    <welcome-file-list>        <welcome-file>index.jsp</welcome-file>    </welcome-file-list>    <jsp-config>        <jsp-property-group>            <url-pattern>*.jsp</url-pattern>            <el-ignored>false</el-ignored>            <page-encoding>UTF-8</page-encoding>        </jsp-property-group>    </jsp-config></web-app>

applicationContext-mvc.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:mvc="http://www.springframework.org/schema/mvc"       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        http://www.springframework.org/schema/mvc        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">    <!-- 自动扫描 Controller ,不包含 Spring 的 bean -->    <context:component-scan base-package="com.hpu.**.controller" use-default-filters="false">        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>        <context:include-filter type="annotation"                                expression="org.springframework.web.bind.annotation.ControllerAdvice"/> <!-- @ControllerAdvice,是spring3.2提供的新注解 -->    </context:component-scan>    <!-- 默认的注解映射的支持 -->    <mvc:annotation-driven>        <mvc:argument-resolvers>            <bean class="com.hpu.dvc.spring.web.method.support.PageableHandlerMethodArgumentResolver" />        </mvc:argument-resolvers>        <mvc:path-matching suffix-pattern="false" />    </mvc:annotation-driven>    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">        <!--<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>-->        <property name="prefix" value="/WEB-INF/view/"/>        <!--可为空,方便实现自已的依据扩展名来选择视图解释类的逻辑 -->        <property name="suffix" value=".jsp"/>    </bean>    <!-- 文件上传 -->    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">        <property name="defaultEncoding" value="UTF-8"/>        <property name="maxUploadSize" value="2097152"/>    </bean>    <!-- 拦截器 -->    <mvc:interceptors>        <!-- 拦截所有资源 -->        <bean class="com.hpu.dvc.interceptor.AuthInterceptor"/>        <mvc:interceptor>            <!-- 拦截制定资源 -->            <mvc:mapping path="/dv/**"/>            <bean class="com.hpu.dvc.interceptor.AuthInterceptor"/>        </mvc:interceptor>    </mvc:interceptors>    <!-- 对静态资源文件的访问-->    <mvc:default-servlet-handler/>    <mvc:resources mapping="/resources/**" cache-period="111"                   location="/resources/,classpath:/resources/,classpath:/static/,classpath:/META-INF/resources/webjars/"/></beans>

applicationContext.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:aop="http://www.springframework.org/schema/aop"       xmlns:context="http://www.springframework.org/schema/context"       xmlns:jdbc="http://www.springframework.org/schema/jdbc"       xmlns:tx="http://www.springframework.org/schema/tx"       xsi:schemaLocation="        http://www.springframework.org/schema/aop        http://www.springframework.org/schema/aop/spring-aop-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/jdbc        http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd        http://www.springframework.org/schema/tx        http://www.springframework.org/schema/tx/spring-tx-4.0.xsd        http://www.springframework.org/schema/context        http://www.springframework.org/schema/context/spring-context-4.0.xsd">    <!-- Scans for application @Components to deploy 不应该包含 spring mvc-->    <context:component-scan base-package="com.hpu.**">        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>        <context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>    </context:component-scan>    <!-- 数据库配置文件位置 -->    <!--<context:property-placeholder location="classpath:/jdbc.dev.properties"/>-->     <context:property-placeholder location="classpath:/jdbc.local.properties"/>    <bean id="propertiesReader"          class="org.springframework.beans.factory.config.PropertiesFactoryBean">        <property name="locations">            <list>                <value>classpath:com/hpu/dvc/utils/dvc.properties</value>            </list>        </property>    </bean>    <!-- Hikari方式配置数据源 -->    <bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="shutdown">        <property name="driverClassName" value="${jdbc.driverClassName}"/>        <property name="jdbcUrl" value="${jdbc.url}"/>        <property name="username" value="${jdbc.username}"/>        <property name="password" value="${jdbc.password}"/>        <!-- 连接只读数据库时配置为true, 保证安全 -->        <property name="readOnly" value="false"/>        <!-- 等待连接池分配连接的最大时长(毫秒),超过这个时长还没可用的连接则发生SQLException, 缺省:30秒 -->        <property name="connectionTimeout" value="30000"/>        <!-- 一个连接idle状态的最大时长(毫秒),超时则被释放(retired),缺省:10分钟 -->        <property name="idleTimeout" value="600000"/>        <!-- 一个连接的生命时长(毫秒),超时而且没被使用则被释放(retired),缺省:30分钟,建议设置比数据库超时时长少30秒,参考MySQL wait_timeout参数(show variables like '%timeout%';) -->        <property name="maxLifetime" value="1800000"/>        <!-- 连接池中允许的最大连接数。缺省值:10;推荐的公式:((core_count * 2) + effective_spindle_count) -->        <property name="maximumPoolSize" value="15"/>        <property name="connectionTestQuery" value="${jdbc.connectionTestQuery}"/>    </bean>    <!-- 使用JDBC事物 -->    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">        <property name="dataSource" ref="dataSource"/>    </bean>    <!-- AOP配置事务 -->    <tx:advice id="transactionAdvice" transaction-manager="transactionManager">        <tx:attributes>            <tx:method name="find*" propagation="REQUIRED"/>            <tx:method name="delete*" propagation="REQUIRED"/>            <tx:method name="update*" propagation="REQUIRED"/>            <tx:method name="insert*" propagation="REQUIRED"/>            <tx:method name="*" read-only="true"  propagation="NOT_SUPPORTED"/>        </tx:attributes>    </tx:advice>    <!-- 配置AOP切面 -->    <aop:config>        <aop:pointcut id="mybatisMapperPointcut" expression="execution(* com.hpu..mapper.*.*(..))"/>        <aop:pointcut id="defaultServiceOperation" expression="execution(* com.hpu..Service.*.*(..))"/>        <aop:advisor pointcut-ref="mybatisMapperPointcut" advice-ref="transactionAdvice"/>        <aop:advisor pointcut-ref="defaultServiceOperation" advice-ref="transactionAdvice"/>    </aop:config>    <!-- 使用annotation注解方式配置事务 -->    <tx:annotation-driven transaction-manager="transactionManager"/>    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">        <property name="dataSource" ref="dataSource"/>        <property name="configLocation" value="classpath:sqlMapConfig.xml" />        <property name="mapperLocations" value="classpath*:mapper/**/*.xml" />    </bean>    <!-- 配置SQLSession模板 -->    <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">        <constructor-arg index="0" ref="sqlSessionFactory"/>    </bean>    <!-- Mapper 接口所在包名,Spring会自动查找其下的类 -->    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">        <property name="basePackage" value="com.hpu.**.mapper" />        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>    </bean>    <jdbc:initialize-database data-source="dataSource" ignore-failures="NONE" enabled="#{'${hbm2ddl.auto}' == 'create'}">        <jdbc:script encoding="UTF-8" location="classpath:sql/hqldb.sql"/>        <jdbc:script encoding="UTF-8" location="classpath:sql/page_design.sql"/>        <jdbc:script encoding="UTF-8" location="classpath:sql/DIM_CURRENCY.sql"/>        <jdbc:script encoding="UTF-8" location="classpath:sql/DIM_KPI.sql"/>        <jdbc:script encoding="UTF-8" location="classpath:sql/DIM_KPI_TREE.sql"/>        <jdbc:script encoding="UTF-8" location="classpath:sql/DIM_ORG.sql"/>        <jdbc:script encoding="UTF-8" location="classpath:sql/DIM_ORG_TREE.sql"/>        <jdbc:script encoding="UTF-8" location="classpath:sql/DM_KPI.sql"/>        <jdbc:script encoding="UTF-8" location="classpath:sql/DM_KPI_DATA_INSERT.sql"/>    </jdbc:initialize-database></beans>

jdbc.dev.properties

jdbc.driverClassName=com.ibm.db2.jcc.DB2Driverjdbc.url=jdbc.username=jdbc.password=jdbc.connectionTestQuery=select 1 from sysibm.sysdummy1hbm2ddl.auto=false
0 0
原创粉丝点击