搭建ssm框架项目基本原理和主要的配置文件小结

原创
2018/09/06 07:35
阅读数 119

搭建ssm框架项目基本原理和主要的配置文件小结

 

 

1.springmvc是spring框架的一个模块,springmvc和spring无需通过中间整合层进行整合。springmvc是一个基于mvc的web框架。mvc的思想大家已经很熟悉了,简称“Model-View-Controller”。

下面先简单介绍下我对spring-mvc的理解。

 

上面这张图大概说明了springmvc的运行过程,看起来可能有点云里雾里的,总结起来就是下面这些:

  1. 客户端发起请求到前端控制器(DispatcherServlet).

  2. 前端控制器请求HandlerMappering 查找Handler,可以根据xml配置、注解进行查找。

  3. DispatcherServlet将请求提交到Controller;

  4. Controller调用业务逻辑处理后,返回ModelAndView;

  5. DispatcherServlet查询一个或多个ViewResoler视图解析器,找到ModelAndView指定的视图;

  6. 视图负责将结果显示到客户端。

这里稍微解释下常用的几个组件名称和作用。

  • 前端控制器(DispatcherServlet):用于接收请求,响应结果

  • 处理器映射器(HandlerMapping):根据请求的url查找Handler(三大核心组件之一)

  • 处理器适配器(HandlerAdapter):按照特定的规则去执行Handler(三大核心组件之一)

  • 处理器(Handler):编写Handler时按照HandlerAdapter的要求去做,这样适配器才可以去正确执行Handler

  • 视图解析器(View resolver):进行视图解析(三大核心组件之一)

  • 视图(View):包括jsp、pdf等

    在了解了上面的基础原理后下面来讲下几个主要的配置文件。项目开发前所需要的jar包提前要导入项目工程里面去。

 

 

有几个主要的配置文件,先了解下每个配置文件的作用。

1. web.xml:当服务启动时首先会去加载web.xml这个资源文件,里面包括了对前端控制器、乱码问题等配置。

2.applicatonContext.xml : 一般配置数据源,事物,注解 等。

在这里我使用的是applicatonContext-*.xml的形式将DAO、Service、Transaction层分开配置,这样便于管理

分别为applicatonContext-dao.xml、applicatonContext-service.xml、applicatonContext-transaction.xml

分开配置时,需要在web.xml中配置上下文位置

3.springmvc.xml: 里面配置的是控制层的 ,如视图解析器静态资源, mvc 文件上传,拦截器等。

4.SqlMapConfig.xml: 该配置文件为MyBatis的配置文件,里面无需配置,一切交给spring管理,但是xml文件基础配置要有。

 

以下是配置文件代码:

web.xml

 

 
  1. <?xml version="1.0" encoding="UTF-8"?>

  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  3. xmlns="http://java.sun.com/xml/ns/javaee"

  4. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

  5. id="WebApp_ID" version="2.5">

  6. <display-name>ssm_boot_crm</display-name>

  7. <welcome-file-list>

  8. <welcome-file>index.html</welcome-file>

  9. <welcome-file>index.htm</welcome-file>

  10. <welcome-file>index.jsp</welcome-file>

  11. <welcome-file>default.html</welcome-file>

  12. <welcome-file>default.htm</welcome-file>

  13. <welcome-file>default.jsp</welcome-file>

  14. </welcome-file-list>

  15. <!-- 上下文的位置 -->

  16. <context-param>

  17. <param-name>contextConfigLocation</param-name>

  18. <param-value>classpath:applicationContext-*.xml</param-value>

  19. </context-param>

  20. <!-- Spring的监听器 -->

  21. <listener>

  22. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

  23. </listener>

  24.  
  25.  
  26. <!-- POST提交过滤器 UTF-8 -->

  27. <filter>

  28. <filter-name>encoding</filter-name>

  29. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>

  30. <init-param>

  31. <param-name>encoding</param-name>

  32. <param-value>UTF-8</param-value>

  33. </init-param>

  34. </filter>

  35.  
  36. <filter-mapping>

  37. <filter-name>encoding</filter-name>

  38. <url-pattern>*.action</url-pattern>

  39. </filter-mapping>

  40. <!-- 前端控制器 -->

  41. <servlet>

  42. <servlet-name>crm</servlet-name>

  43. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

  44. <init-param>

  45. <param-name>contextConfigLocation</param-name>

  46. <!-- 此处不配置 默认找 /WEB-INF/[servlet-name]-servlet.xml -->

  47. <param-value>classpath:springmvc.xml</param-value>

  48. </init-param>

  49. <load-on-startup>1</load-on-startup>

  50. </servlet>

  51. <servlet-mapping>

  52. <servlet-name>crm</servlet-name>

  53. <!-- 1:*.do *.action 拦截以.do结尾的请求 (不拦截 jsp png jpg .js .css)

  54. 2:/ 拦截所有请求 (不拦截.jsp) 建议使用此种 方式 (拦截 .js.css .png) (放行静态资源)

  55. 3:/* 拦截所有请求(包括.jsp) 此种方式 不建议使用 -->

  56. <url-pattern>*.action</url-pattern>

  57. </servlet-mapping>

  58. </web-app>

 

applicationContext-dao.xml

 

 
  1. <?xml version="1.0" encoding="UTF-8"?>

  2. <beans xmlns="http://www.springframework.org/schema/beans"

  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"

  4. xmlns:context="http://www.springframework.org/schema/context"

  5. xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"

  6. xmlns:task="http://www.springframework.org/schema/task" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"

  7. xsi:schemaLocation="http://www.springframework.org/schema/beans

  8. http://www.springframework.org/schema/beans/spring-beans-4.0.xsd

  9. http://www.springframework.org/schema/mvc

  10. http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd

  11. http://www.springframework.org/schema/context

  12. http://www.springframework.org/schema/context/spring-context-4.0.xsd

  13. http://www.springframework.org/schema/aop

  14. http://www.springframework.org/schema/aop/spring-aop-4.0.xsd

  15. http://www.springframework.org/schema/tx

  16. http://www.springframework.org/schema/tx/spring-tx-4.0.xsd

  17. http://www.springframework.org/schema/task

  18. http://www.springframework.org/schema/task/spring-task-4.0.xsd

  19. http://code.alibabatech.com/schema/dubbo

  20. http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

  21.  
  22. <!-- srping框架 配置文件 用于管理数据库连接池 -->

  23. <!-- 配置 读取properties文件 db.properties -->

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

  25.  
  26. <!-- 配置 数据源 用于连接数据库 -->

  27. <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">

  28. <!-- 数据库驱动 -->

  29. <property name="driverClassName" value="${jdbc.driver}" />

  30. <!-- 连接地址 -->

  31. <property name="url" value="${jdbc.url}" />

  32. <!-- 用户名 -->

  33. <property name="username" value="${jdbc.username}" />

  34. <!-- 密码 -->

  35. <property name="password" value="${jdbc.password}" />

  36.  
  37. </bean>

  38. <!-- 配置 Mybatis的工厂 -->

  39. <bean class="org.mybatis.spring.SqlSessionFactoryBean">

  40. <!-- 绑定数据源 -->

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

  42. <!-- 配置Mybatis的核心 配置文件所在位置 -->

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

  44. <!-- 配置pojo别名 -->

  45. <property name="typeAliasesPackage" value="com.company.ssm.crm.pojo" />

  46. </bean>

  47.  
  48. <!-- 配置 1:原始Dao开发 接口实现类 Mapper.xml 三个

  49. 2:接口开发 接口 不写实现类 Mapper.xml 二个 (UserDao、ProductDao

  50. 、BrandDao。。。。。。。)

  51. 3:接口开发、并支持扫描 cn.itcast.core.dao(UserDao。。。。。) 写在此包下即可被扫描到

  52. -->

  53. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

  54. <property name="basePackage" value="com.company.ssm.crm.dao" />

  55. </bean>

  56.  
  57. </beans>

 

applicationContext-service.xml

 

 
  1. <?xml version="1.0" encoding="UTF-8"?>

  2. <beans xmlns="http://www.springframework.org/schema/beans"

  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"

  4. xmlns:context="http://www.springframework.org/schema/context"

  5. xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"

  6. xmlns:task="http://www.springframework.org/schema/task" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"

  7. xsi:schemaLocation="http://www.springframework.org/schema/beans

  8. http://www.springframework.org/schema/beans/spring-beans-4.0.xsd

  9. http://www.springframework.org/schema/mvc

  10. http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd

  11. http://www.springframework.org/schema/context

  12. http://www.springframework.org/schema/context/spring-context-4.0.xsd

  13. http://www.springframework.org/schema/aop

  14. http://www.springframework.org/schema/aop/spring-aop-4.0.xsd

  15. http://www.springframework.org/schema/tx

  16. http://www.springframework.org/schema/tx/spring-tx-4.0.xsd

  17. http://www.springframework.org/schema/task

  18. http://www.springframework.org/schema/task/spring-task-4.0.xsd

  19. http://code.alibabatech.com/schema/dubbo

  20. http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

  21.  
  22.  
  23. <!-- 配置扫描包 扫描 @Service spring代理管理业务层 -->

  24. <context:component-scan base-package="com.company.ssm.crm.service" />

  25. </beans>

 

applicationContext-transaction.xml

 

 
  1. <?xml version="1.0" encoding="UTF-8"?>

  2. <beans xmlns="http://www.springframework.org/schema/beans"

  3. xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"

  4. xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"

  5. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd

  7. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd

  8. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd

  9. http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

  10.  
  11.  
  12. <!-- spring 事务管理器 -->

  13. <bean id="transactionManager"

  14. class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

  15. <!-- 数据源 -->

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

  17. </bean>

  18.  
  19. <!-- 通知 -->

  20. <tx:advice id="txAdvice" transaction-manager="transactionManager">

  21. <tx:attributes>

  22. <!-- 传播行为 -->

  23. <tx:method name="save*" propagation="REQUIRED" />

  24. <tx:method name="insert*" propagation="REQUIRED" />

  25. <tx:method name="add*" propagation="REQUIRED" />

  26. <tx:method name="create*" propagation="REQUIRED" />

  27. <tx:method name="delete*" propagation="REQUIRED" />

  28. <tx:method name="update*" propagation="REQUIRED" />

  29. <tx:method name="find*" propagation="SUPPORTS" read-only="true" />

  30. <tx:method name="select*" propagation="SUPPORTS" read-only="true" />

  31. <tx:method name="get*" propagation="SUPPORTS" read-only="true" />

  32. </tx:attributes>

  33. </tx:advice>

  34.  
  35. <!-- AOP 切面 -->

  36. <aop:config>

  37. <aop:advisor advice-ref="txAdvice"

  38. pointcut="execution(* cn.itcast.core.service.*.*(..))" />

  39. </aop:config>

  40.  
  41. </beans>

 

 

springmvc.xml

 

 
  1. <beans xmlns="http://www.springframework.org/schema/beans"

  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"

  3. xmlns:context="http://www.springframework.org/schema/context"

  4. xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"

  5. xmlns:task="http://www.springframework.org/schema/task" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"

  6. xsi:schemaLocation="http://www.springframework.org/schema/beans

  7. http://www.springframework.org/schema/beans/spring-beans-4.0.xsd

  8. http://www.springframework.org/schema/mvc

  9. http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd

  10. http://www.springframework.org/schema/context

  11. http://www.springframework.org/schema/context/spring-context-4.0.xsd

  12. http://www.springframework.org/schema/aop

  13. http://www.springframework.org/schema/aop/spring-aop-4.0.xsd

  14. http://www.springframework.org/schema/tx

  15. http://www.springframework.org/schema/tx/spring-tx-4.0.xsd

  16. http://www.springframework.org/schema/task

  17. http://www.springframework.org/schema/task/spring-task-4.0.xsd

  18. http://code.alibabatech.com/schema/dubbo

  19. http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

  20.  
  21. <!-- 加载属性文件 -->

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

  23. <!-- 配置扫描 器 -->

  24. <context:component-scan base-package="com.company.ssm.crm.controller" />

  25. <!-- 配置处理器映射器 适配器 -->

  26. <mvc:annotation-driven />

  27.  
  28. <!-- 配置视图解释器 jsp -->

  29. <bean id="jspViewResolver"

  30. class="org.springframework.web.servlet.view.InternalResourceViewResolver">

  31. <property name="prefix" value="/WEB-INF/jsp/" />

  32. <property name="suffix" value=".jsp" />

  33. </bean>

  34.  
  35. </beans>

 

SqlMapConfig.xml

 

 
  1. <beans xmlns="http://www.springframework.org/schema/beans"

  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"

  3. xmlns:context="http://www.springframework.org/schema/context"

  4. xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"

  5. xmlns:task="http://www.springframework.org/schema/task" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"

  6. xsi:schemaLocation="http://www.springframework.org/schema/beans

  7. http://www.springframework.org/schema/beans/spring-beans-4.0.xsd

  8. http://www.springframework.org/schema/mvc

  9. http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd

  10. http://www.springframework.org/schema/context

  11. http://www.springframework.org/schema/context/spring-context-4.0.xsd

  12. http://www.springframework.org/schema/aop

  13. http://www.springframework.org/schema/aop/spring-aop-4.0.xsd

  14. http://www.springframework.org/schema/tx

  15. http://www.springframework.org/schema/tx/spring-tx-4.0.xsd

  16. http://www.springframework.org/schema/task

  17. http://www.springframework.org/schema/task/spring-task-4.0.xsd

  18. http://code.alibabatech.com/schema/dubbo

  19. http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

  20.  
  21. <!-- 加载属性文件 -->

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

  23. <!-- 配置扫描 器 -->

  24. <context:component-scan base-package="com.company.ssm.crm.controller" />

  25. <!-- 配置处理器映射器 适配器 -->

  26. <mvc:annotation-driven />

  27.  
  28. <!-- 配置视图解释器 jsp -->

  29. <bean id="jspViewResolver"

  30. class="org.springframework.web.servlet.view.InternalResourceViewResolver">

  31. <property name="prefix" value="/WEB-INF/jsp/" />

  32. <property name="suffix" value=".jsp" />

  33. </bean>

  34.  
  35. </beans>

 

db.properties

 

 
  1. jdbc.driver=com.mysql.jdbc.Driver

  2. jdbc.url=jdbc:mysql://localhost:3306/crm?characterEncoding=utf-8

  3. jdbc.username=root

  4. jdbc.password=root

 

 

以上就是主要的配置文件的配置,特别注意的是在web.xml中默认加载的资源文件再WEB_INF目录下,如果的你xml不在的话就要写清楚的文件路径,例如写在src目录下面就要写classpath,之前开发过程中忽略了这一点,所以启动一直报错找不到资源文件。

主要参考出处 https://blog.csdn.net/baidu_32739019/article/details/73928040

并结合自己的SSM搭建,小结SSM环境搭建,以便记忆,交流和学习,有什么不妥的地方,欢迎指正、交流

展开阅读全文
打赏
0 评论
4 收藏
0
分享
返回顶部
顶部