zoukankan      html  css  js  c++  java
  • ssm框架整合,配置文件中的配置内容

    使用idea工具开发,用maven进行管理。

    最近在写毕业设计,因为对ssm框架一直半解,常常导致出来问题不知道去哪里找,或者读懂了bug却解决不了问题的尴尬局面。今天就重新去理解一下ssm的配置文件,以及之间的相互作用。

    先说一下有几个配置文件。

    resources--applicationContext.xml   : spring容器的配置文件,实现框架整合

                    --db.properties                  : 数据库常量配置(无难度)

          --log4j.properties     :日志信息的存放(开发时为debug,上线改为info)

          --springmvc.xml       :springMVC的配置文件。

          -- sqlMapConfig.xml        :mybats的配置文件,整合后 几乎只剩下 《别名》

    WEBINF -- web.xml        :让配置文件随着项目的启动而加载

    1.  applicationContext.xml 配置文件说明

      ①添加约束条件

      ②开启注解扫描(除了控制器,其他都扫描)

      扫描方式有两种:

      <context:annotation-config>:注解扫描是针对已经在Spring容器里注册过的Bean

      <context:component-scan>:不仅具备<context:annotation-config>的所有功能,还可以在指定的package下面扫描对应的bean

      用法:

      <!--自动扫描包-->
        <context:component-scan base-package="cn.hfbin.crud">
            <!--除了描控制器不扫描外 其它全部扫描-->
            <context:exclude-filter type="annotation"expression="org.springframework.stereotype.Controller"/>
        </context:component-scan>

      

      ③加载jdbc.properties文件,并且配置数据源

      <context:property-placeholder location="classpath:jdbc.properties" />

      <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}" />

      </bean>

      ④配置sqlSessionFactory (Spring和Mybatis整合)

      <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

         <property name="dataSource" ref="dataSource"></property>

        <!-- 指定mybatis配置文件的位置 -->

        <property name="configLocation" value="classpath:sqlMapConfig.xml"></property>

      </bean>

      

      ⑤事务控制的配置

      <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

            <!--控制住数据源  -->
            <property name="dataSource" ref="DataSource"></property>
        </bean>  <!--开启基于注解的事务,使用xml配置形式的事务(必要主要的都是使用配置式)  -->
        <aop:config>
            <!-- 切入点表达式 -->
            <aop:pointcut expression="execution(* cn.hfbin.crud.service..*(..))" id="txPoint"/>
            <!-- 配置事务增强 -->
            <aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"/>
        </aop:config>
    
        <!--配置事务增强,事务如何切入  -->
        <tx:advice id="txAdvice" transaction-manager="transactionManager">
            <tx:attributes>
                <!-- 所有方法都是事务方法 -->
                <tx:method name="*"/>
                <!--以get开始的所有方法  -->
                <tx:method name="get*" read-only="true"/>
            </tx:attributes>
        </tx:advice>

    2.  springmvc.xml 配置文件说明
      
      ① 扫描controller
       <!-- SpringMVC 的配置文件 ,包含网站跳转的逻辑控制器 -->
        <context:component-scan base-package="cn.hfbin" use-default-filters="false">
        <!-- 只扫描控制器 -->
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
        </context:component-scan>
      
      ② 注解驱动
      
      <!-- 能支持springmvc更高级的一些功能:JSR303校验  ,快捷的ajax ,映射动态请求-->
       <mvc:annotation-driven/>

      ③视图解析器
       <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">        <property name="prefix" value="/WEB-INF/view/"></property>
            <property name="suffix" value=".jsp"></property>
        </bean>
      
    3. sqlMapConfig.xml 配置

      ① 别名设置(2种方法) (扫描的是pojo的类)

      单个配置:
      <typeAliases> 

       <typeAlias type="cn.edu.hpu.ssm.po.User" alias="user"/>     

      </typeAliases>
      
      批量设置(推荐)
      <typeAliases>  

                   <package name="cn.edu.hpu.ssm.po"/>              

      </typeAliases

     
     ②扫描mapper
      <mappers>  

                 <mapper resource="sqlmap/User.xml"/>  

        <mapper resource="mapper/UserMapper.xml"/>  

      </mappers>

       这里要注意的是:mapper也可以在applicationContext中扫描。

        在applicationContext配置如下:

      

     <!-- 配置SqlSessionFactory-->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <!-- 加载mybatis的配置文件-->
            <property name="configLocation" value="classpath:config/mybatis/SqlMapConfig.xml"/>
            <!-- 数据源-->
            <property name="dataSource" ref="dataSource"/>
            <!-- mapper.xml扫描-->
            <property name="mapperLocations" value="classpath:config/mapper/*.xml"/>
        </bean>
    
        <!-- mapper扫描器-->
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
            <property name="basePackage" value="com.youye.mapper"/>
        </bean>
    

      

    在配置sqlSessionFactory时,配置mapperLocations属性,用来加载mapper.xml文件,mapper扫描器配置则如上所示。

    mapper扫描器中的两个property的顺序不能交换,即SQLSessionFactoryBeanName必须在前。

    4. web.xml 配置
      这个比较简单,基本上大家的推荐都是一样的。
      
     <!-- 启动spring容器 -->
        <!-- needed for ContextLoaderListener -->
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:applicationContext.xml</param-value>
        </context-param>
    
        <!-- Bootstraps the root web application context before servlet initialization -->
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
    
        <!-- 启动springmvc的前端控制器 ,拦截所有请求 -->
        <servlet>
            <servlet-name>springmvc</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <load-on-startup>1</load-on-startup>
        </servlet>
    
        <!-- Map all requests to the DispatcherServlet for handling -->
        <servlet-mapping>
            <servlet-name>springmvc</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
    <!-- 编码过滤器 --> <filter> <filter-name>CharacterEncodingFilter</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>forceRequestEncoding</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>forceResponseEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/</url-pattern> </filter-mapping> <!-- 使用Rest风格的URL --> <filter> <filter-name>HiddenHttpMethodFilter</filter-name> <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> </filter> <filter-mapping> <filter-name>HiddenHttpMethodFilter</filter-name> <url-pattern>/</url-pattern> </filter-mapping></web-app> 注意(这里加入了支持Rest风格的URL 的配置): <filter> <filter-name>HiddenHttpMethodFilter</filter-name> <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> </filter> <filter-mapping> <filter-name>HiddenHttpMethodFilter</filter-name> <url-pattern>/</url-pattern> </filter-mapping>
    
    
    
     
     
     
     
     


      

      

        

      

  • 相关阅读:
    Java实现 LeetCode 27 移除元素
    Java实现 LeetCode 26 删除排序数组中的重复项
    Java实现 LeetCode 26 删除排序数组中的重复项
    Java实现 LeetCode 26 删除排序数组中的重复项
    Java实现 LeetCode 25 K个一组翻转链表
    Java实现 LeetCode 25 K个一组翻转链表
    Java实现 LeetCode 25 K个一组翻转链表
    Java实现 LeetCode 24 两两交换链表中的节点
    Java实现 LeetCode 24 两两交换链表中的节点
    Java实现 LeetCode 24 两两交换链表中的节点
  • 原文地址:https://www.cnblogs.com/dong-dong-1/p/8724127.html
Copyright © 2011-2022 走看看