zoukankan      html  css  js  c++  java
  • SpringMVC-Mybatis学习总结整理(上)

    (一)web.xml文件配置

    首先,在web.xml文件中配置spring文件位置,这样可以解析所有的spring-*.xml格式文件

    <!--配置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类型可以自定义

    <!-- 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 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>

    另外还可以配置项目的编码格式

    <!-- 配置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的监听器。

    <!-- 此监听器出用于主要为了解决java.beans.Introspector导致内存泄漏的问题 -->
    <listener>
        <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
    </listener>

    (二)配置spring-servlet.xml

     配置spring扫描注解包,完成请求和注解

    <!-- 启动 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 或和其对应的 SqlSessionFactory

    <!-- 在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包

    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>
  • 相关阅读:
    LiveBinding应用 dataBind 数据绑定
    判断当前运行的平台
    客户端如何连接 DataSnap Server 调用服务的方法
    创建DataSnap Server
    FireDACQuery FDQuery New
    BDE 退出历史 迁移至FireDAC
    窗口叉叉变灰
    c++builder XE6 Remote Debuger 远程调试
    IP Editor IP控件
    C++访问mysql数据库
  • 原文地址:https://www.cnblogs.com/gongshaobo93/p/5050653.html
Copyright © 2011-2022 走看看