您的位置:首页 > 编程语言 > Java开发

spring mvc搭载mybatis精简实例配置文件

2017-01-28 22:52 375 查看
spring-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">
<!-- XML转码器 -->
<bean
id="fmXmlEscape"
class="freemarker.template.utility.XmlEscape"
/>

<!-- 配置freeMarker的模板路径 --> 
<bean id="freemarkerConfig"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="configLocation" value="classpath:freemarker.properties" />
<property name="templateLoaderPath" value="/WEB-INF/ftl/" />
<property name="freemarkerVariables">
<map>
<entry key="xml_escape" value-ref="fmXmlEscape" />
</map>
</property>
</bean>
<!-- 配置freeMarker视图解析器 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.freemarker.FreeMarkerView" />
<property name="contentType" value="text/html;
charset=utf-8" />
<property name="cache" value="true" />
<property name="suffix" value=".ftl" />
<property name="order" value="2" />

<property name="requestContextAttribute" value="request" />
<property name="exposeRequestAttributes" value="true" />
<property name="exposeSessionAttributes" value="true" />
<property name="exposeSpringMacroHelpers" value="true" />
</bean>

<!-- jsp视图解析器 -->
<bean
id="jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property
name="viewClass"
value="org.springframework.web.servlet.view.JstlView"
/>
<property
name="prefix"
value="/WEB-INF/jsp"
/>
<property
name="suffix"
value=".jsp"
/>
<property
name="order"
value="1"
/>
</bean>
<!-- 
  <bean id="jsonConverter"
class="org.springframework.http.converter.json.GsonHttpMessageConverter"></bean>
 -->
  <bean
id="jsonConverter"
class="com.zhrenjie04.alex.web.core.generic.AlexGsonHttpMessageConverter"></bean>
<bean
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property
name="messageConverters">
<list>
<ref
bean="jsonConverter"
/>
</list>
</property>
</bean>
<!-- 扫描控制器类 -->
<context:component-scan
base-package="com/zhrenjie04/alex/web/controller/**"
/>

<!-- 配置静态资源 -->
<mvc:resources
mapping="/css/**"
location="/css/"
/>
<mvc:resources
mapping="/img/**"
location="/img/"
/>
<mvc:resources
mapping="/js/**"
location="/js/"
/>
<!-- 采用注解方式配置MVC -->
<mvc:annotation-driven
content-negotiation-manager="contentNegotiationManager">
<mvc:message-converters>
<bean
class="org.springframework.http.converter.StringHttpMessageConverter">
                <property
name="supportedMediaTypes">
                    <list>
                        <value>application/json;charset=utf-8</value>
                        <value>application/xml;charset=utf-8</value>
                    </list>
                </property>
            </bean>
</mvc:message-converters>
</mvc:annotation-driven>

<bean
id="contentNegotiationManager"
class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
<property
name="favorPathExtension"
value="true"
/>
<property
name="favorParameter"
value="true"
/>
<property
name="defaultContentType"
value="text/html"
/>
<property
name="mediaTypes">
<value>
json=application/json
xml=application/xml
</value>
</property>
</bean>
<!-- 支持上传文件 -->
<bean
id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 设置上传文件的最大尺寸为1MB -->
<property
name="maxUploadSize">
<value>1048576</value>
</property>
<property
name="defaultEncoding">
<value>UTF-8</value>
</property>
</bean>
</beans> 

spring-config.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:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:security="http://www.springframework.org/schema/security"
xsi:schemaLocation="  
            http://www.springframework.org/schema/aop   
            http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  
            http://www.springframework.org/schema/security   
            http://www.springframework.org/schema/security/spring-security-3.2.xsd  
            http://www.springframework.org/schema/beans   
            http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
            http://www.springframework.org/schema/data/jpa   
            http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.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">
<!-- Activates annotation-based bean configuration -->
<context:annotation-config
/>

<!-- 数据库配置文件位置 -->
<context:property-placeholder
location="classpath:jdbc.properties"
/>

<bean
id="propertyPlaceholderConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property
name="locations">
<list>
<value>classpath:jdbc.properties</value>
</list>
</property>
</bean>
<!-- 配置dbcp数据源 -->
<bean
id="dataSource"
class="org.apache.commons.dbcp.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}"
/>
<property
name="minIdle"
value="${jdbc.minIdle}"
/>        <!-- 队列中的最小等待数 -->
<property
name="maxIdle"
value="${jdbc.maxIdle}"
/>        <!-- 队列中的最大等待数 -->
<property
name="maxWait"
value="${jdbc.maxWait}"
/>        <!-- 最长等待时间,单位毫秒 -->
<property
name="maxActive"
value="${jdbc.maxActive}"
/>    <!-- 最大活跃数 -->
<property
name="initialSize"
value="${jdbc.initialSize}"
/><!-- 初始大小 -->
</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="query*"
read-only="true"
propagation="SUPPORTS"
/>
<tx:method
name="pageQuery*"
read-only="true"
propagation="SUPPORTS"
/>
<tx:method
name="jsonPageQuery*"
read-only="true"
propagation="SUPPORTS"
/>
<tx:method
name="delete*"
propagation="REQUIRED"
/>
<tx:method
name="update*"
propagation="REQUIRED"
/>
<tx:method
name="insert*"
propagation="REQUIRED"
/>
<tx:method
name="*"
propagation="SUPPORTS"
/>
</tx:attributes>
</tx:advice>

<!-- 配置AOP切面 -->
<aop:config>
<aop:pointcut
id="transactionPointcut"
expression="execution(* com.zhrenjie04.alex..service.*.*(..))"
/>
<aop:advisor
pointcut-ref="transactionPointcut"
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="mapperLocations"
value="classpath:mapper/${jdbc.sqlType}/**/*Mapper.xml"></property>
<property
name="configLocation"
value="classpath:mybatis-configuration.xml"
/>
</bean>

<bean
class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property
name="annotationClass"
value="org.springframework.stereotype.Repository"
/>
<property
name="basePackage"
value="com.zhrenjie04.alex.web"
/>
</bean>
<!-- Scans for application @Components to deploy -->
<context:component-scan
base-package="com.zhrenjie04.alex.web"
/>

</beans>
mybatis-configuration.xml:

<?xml
version="1.0"
encoding="UTF-8"?>   
<!DOCTYPE configuration   
    PUBLIC
"-//mybatis.org//DTD Config 3.0//EN"   
    "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<!-- 全局的映射器启用或禁用缓存。 -->
<setting
name="cacheEnabled"
value="false"
/>
<!-- 全局启用或禁用延迟加载 -->
<setting
name="lazyLoadingEnabled"
value="true"
/>
<!-- 允许或不允许多种结果集从一个单独的语句中返回 -->
<setting
name="multipleResultSetsEnabled"
value="true"
/>
<!-- 使用列标签代替列名 -->
<setting
name="useColumnLabel"
value="true"
/>
<!-- 允许JDBC支持生成的键 -->
<setting
name="useGeneratedKeys"
value="true"
/>
<!-- 配置默认的执行器 -->
<setting
name="defaultExecutorType"
value="SIMPLE"
/>
<!-- 设置超时时间 -->
<setting
name="defaultStatementTimeout"
value="25000"
/>
</settings>
</configuration>  
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="e-platform-manager"
version="3.0">
<display-name>e-platform-manager</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 Dispatcher
Servlet</servlet-name>
</filter-mapping>

<filter>
<filter-name>httpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>

<filter-mapping>
<filter-name>httpMethodFilter</filter-name>
<servlet-name>Spring MVC Dispatcher
Servlet</servlet-name>
</filter-mapping>

<filter>
<filter-name>onlineFilter</filter-name>
<filter-class>com.zhrenjie04.alex.web.core.filter.OnlineFilter</filter-class>
</filter>

<filter-mapping>
<filter-name>onlineFilter</filter-name>
<servlet-name>Spring MVC Dispatcher
Servlet</servlet-name>
</filter-mapping>

<filter>
<filter-name>noCachedFilter</filter-name>
<filter-class>com.zhrenjie04.alex.web.core.filter.NoCachedFilter</filter-class>
</filter>

<filter-mapping>
<filter-name>noCachedFilter</filter-name>
<servlet-name>Spring MVC Dispatcher
Servlet</servlet-name>
</filter-mapping>

<!-- Spring 容器加载 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-config.xml</param-value>
</context-param>

<!-- The front controller of this Spring Web application, responsible for 
handling all application requests -->
<servlet>
<servlet-name>Spring MVC Dispatcher
Servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-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 Dispatcher
Servlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

<session-config>
<session-timeout>30</session-timeout>
</session-config>

<welcome-file-list>
<welcome-file>index.do</welcome-file>
</welcome-file-list>
<jsp-config>
<jsp-property-group>
<url-pattern>*.jsp</url-pattern>
<trim-directive-whitespaces>true</trim-directive-whitespaces>
</jsp-property-group>
</jsp-config>
<error-page>
<error-code>404</error-code>
<location>/404.jsp</location>
</error-page>
</web-app>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: