SpringMVC-Mybatis學習總結整理(上)

(一)web.xml文件配置css

首先,在web.xml文件中配置spring文件位置,這樣能夠解析全部的spring-*.xml格式文件java

<!--配置spring文件位置 -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:com/xxx/xxx/config/spring-*.xml</param-value>
</context-param>

繼續Spring的Servlet在web.xml中的配置,url類型能夠自定義web

<!-- springMVC配置 -->
<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*:com/xxx/xxx/config/spring-servlet.xml</param-value>
        </init-param>
    <load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>*.htm</url-pattern>
</servlet-mapping>

另外須要對url訪問進行過濾,防止直接訪問後臺資源,這裏攔截.htm訪問,對一些前臺須要用到的靜態資源則能夠跳過過濾不設攔截spring

<!-- 配置spring filter -->
<filter>
    <filter-name>permissionFilter</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    <init-param>
        <param-name>targetFilterLifecycle</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>permissionFilter</filter-name>
    <url-pattern>*.htm</url-pattern>
</filter-mapping>
<!--jpg,png,js,css 文件不進行過濾-->
<servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.jpg</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.png</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.js</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.css</url-pattern> </servlet-mapping>

另外還能夠配置項目的編碼格式sql

<!-- 配置utf-8編碼格式 -->
<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>
    <url-pattern>/*</url-pattern>
</filter-mapping>

爲了防止JavaBeans致使的內存泄露,咱們可使用一個名爲org.springframework.web.util.IntrospectorCleanupListener的監聽器,在web應用關閉的時候,清除JavaBeans Introspector的監聽器。express

<!-- 此監聽器出用於主要爲了解決java.beans.Introspector致使內存泄漏的問題 -->
<listener>
    <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>

(二)配置spring-servlet.xmlmybatis

 配置spring掃描註解包,完成請求和註解mvc

<!-- 啓動 spring mvc 註解 -->
<context:annotation-config/>    
<!-- 配置掃描註解的包-->
<context:component-scan base-package="com.xxx.xxx">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />    
</context:component-scan>
<!-- 啓動SpringMVC的請求和註解 -->  
<bean  
    class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">  
    <property name="messageConverters">  
        <list>
            <ref bean="mappingJacksonHttpMessageConverter" /> <!-- JSON轉換器 -->  
        </list>  
    </property>  
</bean>  
<!-- 完成請求和註解POJO的映射 -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<!-- 配置數據源 --> <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
   <property name="jndiName" value="java:xxx"/>
</bean>

<!-- 對轉向頁面的路徑解析。prefix:前綴, suffix:後綴 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp"/> <!-- 默認的註解映射的支持 --> <mvc:annotation-driven /> <!-- 對靜態資源的控制 --> <mvc:default-servlet-handler/>

spring中配置mybatis,這裏在spring中配置mybatis的sqlSessionFactory,這樣在程序中就不須要直接使用 SqlSessionFactoryBean 或和其對應的 SqlSessionFactoryapp

<!-- 在spring中配置mybatis的配置映射文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
     <property name="dataSource" ref="dataSource" />  
     <!-- 自動掃描mapping.xml文件 -->  
     <property name="mapperLocations" value="classpath:com/xxx/xxx/mapping/*.xml"></property>
     <!-- MyBatis屬性信息設置 -->
     <property name="configLocation" value="classpath:com/xxx/xxx/config/mybatis.xml"></property>  
 </bean>  
 <!-- DAO接口所在包名,Spring會自動查找其下的類 -->  
 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
     <property name="basePackage" value="com.xxx.xxx.dao" />  
     <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
 </bean> 
 <!-- 定義事物 -->
 <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
     <property name="dataSource" ref="dataSource" />  
 </bean>
 <!--tx:annotation-driven transaction-manager="txManager" /-->
 <tx:advice id="txAdvice" transaction-manager="txManager">
     <tx:attributes>
         <tx:method name="query*" propagation="NOT_SUPPORTED" read-only="true"/>
         <tx:method name="select*" propagation="NOT_SUPPORTED" read-only="true"/>
         <tx:method name="get*" propagation="NOT_SUPPORTED" read-only="true"/>
         <tx:method name="modify*" propagation="NOT_SUPPORTED" read-only="true"/>
         <tx:method name="save*" propagation="NOT_SUPPORTED" read-only="true"/>
         <tx:method name="delete*" propagation="NOT_SUPPORTED" read-only="true"/>
         <tx:method name="update*" propagation="NOT_SUPPORTED" read-only="true"/>
         <tx:method name="*" propagation="REQUIRED"/>
     </tx:attributes>
 </tx:advice>

 另外mybatis須要導入兩個jar包jsp

Mybatis.xml文件配置,這裏對數據類型的配置和其餘屬性配置不做贅述

<?xml version="1.0" encoding="UTF-8"?>  
<!DOCTYPE configuration  
PUBLIC "-//mybatis.org//DTDConfig 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"
> <configuration> ... ... </configuration>
相關文章
相關標籤/搜索