转自:https://www.cnblogs.com/dong-dong-1/p/8724127.html

使用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配置如下:

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<!-- 配置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>

ssm框架整合,配置文件中的配置内容的更多相关文章

  1. ssm框架整合配置文件

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  2. ssm 框架整合 代码初步 maven配置

    pom.xml 配置<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --> <de ...

  3. springmvc(二) ssm框架整合的各种配置

    ssm:springmvc.spring.mybatis这三个框架的整合,有耐心一步步走. --WH 一.SSM框架整合 1.1.整合思路 从底层整合起,也就是先整合mybatis与spring,然后 ...

  4. JAVAEE——宜立方商城01:电商行业的背景、商城系统架构、后台工程搭建、SSM框架整合

    1. 学习计划 第一天: 1.电商行业的背景. 2.宜立方商城的系统架构 a) 功能介绍 b) 架构讲解 3.工程搭建-后台工程 a) 使用maven搭建工程 b) 使用maven的tomcat插件启 ...

  5. 转 SSM框架整合to萌新

    作用: SSM框架是spring MVC ,spring和mybatis框架的整合,是标准的MVC模式,将整个系统划分为表现层,controller层,service层,DAO层四层 使用spring ...

  6. SpringMVC札集(10)——SSM框架整合

    自定义View系列教程00–推翻自己和过往,重学自定义View 自定义View系列教程01–常用工具介绍 自定义View系列教程02–onMeasure源码详尽分析 自定义View系列教程03–onL ...

  7. SSM框架——整合搭建流程

    1.首先创建maven工程,使用哪种方式进行创建都可以,可以参考博主之前的文章: <两种方式创建Maven项目[方式二]><两种方式创建Maven项目[方式一]> 2.先看看搭 ...

  8. (转)淘淘商城系列——SSM框架整合之Dao层整合

    http://blog.csdn.net/yerenyuan_pku/article/details/72721093 一个项目中往往有三层即Dao层.Service层和Web层,看标题就知道了,本文 ...

  9. SSM框架整合模板

    SSM框架整合--MAVEN依赖 spring方面(包含了springmvc): spring-webmvc:spring与mvc的整合依赖,主要包括spring的核心包和springmvc需要的包 ...

随机推荐

  1. Oracle VM VirtualBox - VBOX_E_FILE_ERROR (0x80BB0004)

    问题描述: 导入虚拟电脑 D:\LR\虚拟机相关\CentOS-6.7-x86_64-2G-40G-oracle-IP9\CentOS-6.7-x86_64-2G-40G-oracle-IP9.ovf ...

  2. (转) 统计在从1到n的正整数中1出现的次数

    1. 题目描述 输入一个整数n,求从1到n这n个整数的十进制表示中1出现的次数.例如输入12,从1到12这些整数中包含1的数字有1,10,11和12,1一共出现了5次. 2. 题目来源 第一次看到是在 ...

  3. day06_类与对象、封装、构造方法

    02_面向对象与常用类 01_面向对象思想的概述 面向过程:当需要实现一个功能的时候,每个具体的步骤都需要亲力亲为,详细处理每一个细节. 面向对象:当需要实现一个功能的时候,不关心具体步骤,而是找一个 ...

  4. JS高级---正则表达式其他方法的使用

    正则表达式其他方法的使用 正则表达式中:g 表示的是全局模式匹配 正则表达式中:i 表示的是忽略大小写 var str = "中国移动:10086,中国联通:10010,中国电信:10000 ...

  5. ECMAScript基本语法——④变量

    简介 变量:一小块存储数据的内存空间先申请了一块内存空间,规定空间的存储类型,给空间赋值3, 想找到这个3可以通过内存空间的地址值,但是通过地址值太麻烦了,给这个空间起了一个名字a 通过这个a可以找到 ...

  6. JavaScript的Map和Set

    JavaScript的Map和Set 1.map:映射(通过key获得value).增.删 2.set:增.删.判断是否包含某个元素 1. Map <!DOCTYPE html><h ...

  7. Keep-Alive 以及服务器心跳

    Keep-Alive 来源 :http://www.nowamagic.net/academy/detail/23350305 服务器心跳  来源 :http://www.cnblogs.com/lw ...

  8. JS高级---总结继承

    总结继承 面向对象特性: 封装, 继承,多态 继承, 类与类之间的关系, 面向对象的语言的继承是为了多态服务的   js不是面向对象的语言, 但是可以模拟面向对象,模拟继承,为了节省内存   继承: ...

  9. 用python脚本测试接口

    自己写一个脚本,统计调用200次接口的请求时长. # -*- coding=utf-8 -*-import osimport requestsimport time url = "http: ...

  10. 数据存储 csv

    # # 保存csv格式的数据import csv csvFile = open('test.csv','w+',newline='') #文本方式可读写 try: writer = csv.write ...