SSM框架的整合原理以及执行流程

article/2025/11/10 22:06:04

一、SSM框架的整合流程:

  1. 1 Spring与Mybatis整合 :关键在于spring-mybatis.xml配置文件,主要配置自动扫描、自动注入以及数据库等。前提是要配置好JDBC属性文件jdbc.properties.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.2.xsdhttp://www.springframework.org/schema/aop  http://www.springframework.org/schema/aop/spring-aop-3.2.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.2.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.2.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"><!-- 自动扫描注解的bean --><context:component-scan base-package="com.zheng.service" -><!-- 引入jdbc配置文件 -->  <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="locations"><list><value>classpath:properties/*.properties</value><!--要是有多个配置文件,只需在这里继续添加即可 --></list></property></bean><!-- 配置数据源 --><bean id="dataSource"class="org.springframework.jdbc.datasource.DriverManagerDataSource"><!-- 不使用properties来配置 --><!-- <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/learning" /> <property name="username" value="root" /> <property name="password" value="christmas258@" /> --><!-- 使用properties来配置 --><property name="driverClassName"><value>${jdbc_driverClassName}</value></property><property name="url"><value>${jdbc_url}</value></property><property name="username"><value>${jdbc_username}</value></property><property name="password"><value>${jdbc_password}</value></property></bean><!--dao接口所在的包名,Spring会自动查找其下的类--><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage"value="com.zheng.dao" /></bean><!--Spring与Mybatis整合,不需要Mybatis的配置映射文件--><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource" /><!--自动扫描mapper.xml文件,mapperLocations配置**Mapper.xml文件位置--><property name="mapperLocations" value="classpath:mapper/*.xml"/></bean><!-- 配置transactionManager事务管理器 --><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"></property></bean><!-- 配置事物通知属性 --><tx:advice id="txAdvice" transaction-manager="transactionManager"><!-- 定义事物传播特性 --><tx:attributes><tx:method name="insert*" propagation="REQUIRED"/><tx:method name="update*" propagation="REQUIRED" /><tx:method name="edit*" propagation="REQUIRED" /><tx:method name="save*" propagation="REQUIRED" /><tx:method name="add*" propagation="REQUIRED" /><tx:method name="new*" propagation="REQUIRED" /><tx:method name="set*" propagation="REQUIRED" /><tx:method name="remove*" propagation="REQUIRED" /><tx:method name="delete*" propagation="REQUIRED" /><tx:method name="change*" propagation="REQUIRED" /><tx:method name="check*" propagation="REQUIRED" /><tx:method name="get*" propagation="REQUIRED" read-only="true" /><tx:method name="find*" propagation="REQUIRED" read-only="true" /><tx:method name="load*" propagation="REQUIRED" read-only="true" /><tx:method name="*" propagation="REQUIRED" read-only="true" /></tx:attributes></tx:advice><!-- 配置事物切面 需要aspectjweaver.jar--><aop:config><aop:pointcut id="serviceOperation" expression="execution(* com.zheng.service.*.*(..))" /><aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation"/></aop:config>
</beans>
  1. 2 整合Spring MVC:springmvc的配置文件单独放,需要在web.xml里面配置整合,spring-mvc.xml主要配置自动扫描、视图模式、注解的启动等。web.xml主要配置spring-mybatis.xml文件以及springmvc的Servlet。

spring-mvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:p="http://www.springframework.org/schema/p"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.2.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.2.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"><!--自动扫描包 扫描controller(controller层注入)--><context:component-scan base-package="com.zheng.controller"></context:component-scan><!-- 视图解析器 对模型视图添加前后缀 --><bean id="viewResolver"class="org.springframework.web.servlet.view.InternalResourceViewResolver"p:prefix="/WEB-INF/view/" p:suffix=".jsp"/><mvc:default-servlet-handler/><!--解决@Controller注解的使用前提配置--><mvc:annotation-driven></mvc:annotation-driven>
</beans>

web.xml

 

<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>maven_springmvc_mybatis_demo</display-name> <!-- 编码过滤器 --> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <async-supported>true</async-supported> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 读取spring-mybatis整合的配置文件 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:application.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- springMVC核心配置 --> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <!--spingMVC的配置路径 --> <param-value>classpath:springmvc/spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- 拦截设置 --> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name

<url-pattern>/</url-pattern> </servlet-mapping> </web-app>

以上,完成了SSM框架的整合。

二、SSM框架中各层级间的作用及关系

  • 表现层(springMVC):Controller层(Handler层)

    • 负责具体的业务模块流程的控制
    • Controller层通过要调用Service层的接口来控制业务流程,控制的
      配置也在Spring配置文件里面。
  • 业务层(Spring):Service层

    • Service层:负责业务模块的逻辑应用设计。
    • 首先设计其接口,然后再实现他的实现类。
    • 通过对Spring配置文件中配置其实现的关联,完成此步工作,我们
      就可以通过调用Service的接口来进行业务处理。
    • 最后通过调用DAO层已定义的接口,去实现Service具体的 实现类。
  • 持久层(Mybatis):Dao层(Mapper层)

    • Dao层:负责与数据库进行交互设计,用来处理数据的持久化工作。
    • DAO层的设计首先是设计DAO的接口,
    • 然后在Spring的配置文件中定义此接口的实现类,就可在其他模块中
      调用此接口来进行数据业务的处理,而不用关心接口的具体实现类是
      哪个类,这里用到的就是反射机制, DAO层的数据源配置,以及有
      关数据库连接的参数都在Spring的配置文件中进行配置。
  • 视图层:View层

    • 负责前台jsp页面的展示。
    • 此层需要与Controller层结合起来开发。
  • 各层间的联系:

    • Service层是建立在DAO层之上的,建立了DAO层后才可以建立Service层,而Service层又是在Controller层之下的,因而Service层应该既调用DAO层的接口,又要提供接口给Controller层的类来进行调用,它刚好处于一个中间层的位置。每个模型都有一个Service接口,每个接口分别封装各自的业务处理方法。

三、执行流程

 


http://chatgpt.dhexx.cn/article/pXc2Tvt8.shtml

相关文章

SSM三大框架的运行流程、原理、核心技术详解

一、Spring部分 1、Spring的运行流程 第一步&#xff1a;加载配置文件ApplicationContext ac new ClassPathXmlApplicationContext("beans.xml"); &#xff0c;ApplicationContext接口&#xff0c;它由BeanFactory接口派生而来&#xff0c;因而提供了BeanFactory所…

SSM框架整合以及运行流程

最近工作中开发的项目使用了Spring-springMVC-Mybatis框架进行开发&#xff0c;今天来总结写这段时间对SSM框架的理解。在对SSM框架整合前&#xff0c;先对框架的每层的作用以及它们之间的关系做一个简单的介绍。 一、SSM框架中各层级间的作用及关系 表现层&#xff08;sprin…

SSM框架原理及使用方法

作用&#xff1a; SSM框架是spring MVC &#xff0c;spring和mybatis框架的整合&#xff0c;是标准的MVC模式&#xff0c;将整个系统划分为表现层&#xff0c;controller层&#xff0c;service层&#xff0c;DAO层四层 使用spring MVC负责请求的转发和视图管理 spring实现业…

SSM框架运行原理

ssm框架&#xff1a;包括&#xff0c;springMVC -- spring -- mybatis springMVC 是基于MVC的框架 属于MVC框架的还有&#xff1a;Struts1 Struts2 SpringMVC 获取值得方式&#xff1a; Struts1 actionForm(javabean中继承) Struts2 通过 set get 方法 SpringMVC …

SSM框架工作原理、作用及使用方法

作用&#xff1a; SSM框架是spring MVC &#xff0c;spring和mybatis框架的整合&#xff0c;是标准的MVC模式&#xff0c;将整个系统划分为表现层&#xff0c;controller层&#xff0c;service层&#xff0c;DAO层四层 使用spring MVC负责请求的转发和视图管理 spring实现业…

SSM框架原理,作用及使用方法

作用: SSM框架是spring MVC ,spring和mybatis框架的整合,是标准的MVC模式,将整个系统划分为表现层,controller层,service层,DAO层四层 使用spring MVC负责请求的转发和视图管理 spring实现业务对象管理,mybatis作为数据对象的持久化引擎 原理: SpringMVC: 1.客户…

SSM框架原理以及流程简略

SSM框架原理以及流程 Spring:spring实现业务对象管理,也是各个组件的粘合剂&#xff0c;可以很好的管理各层之间的框架。 SpringMvc:负责请求的转发和视图管理,springmvc是spring的模块之一&#xff0c;所有整合的时候基本上可以实现100%零配置。 mybatis&#xff1a;作为数据…

SSM三大框架的运行流程、原理、核心技术详解!

一、Spring部分 1、Spring的运行流程 第一步&#xff1a;加载配置文件ApplicationContext ac new ClassPathXmlApplicationContext("beans.xml");&#xff0c;ApplicationContext接口&#xff0c;它由BeanFactory接口派生而来&#xff0c;因而提供了BeanFactory所…

深入浅出SSM框架流程以及原理

前言:学ssm框架已经有很长时间,今天来复习一下 SSM图示流程: Spring核心:Java反射 Mybatis:动态代理,而动态代理又是基于反射的,所以,ssm框架核心原理在反射。 (1)Spring(对象工厂): 平时开发接触最多的估计就是这个IOC容器,它可以装载bean(也就是Java中的类,…

SSM框架原理以及流程

SSM框架原理以及流程 一&#xff1a;原理二&#xff1a;开发流程1.新建maven项目2.配置整合文件2.1 配置pom.xml文件---引入依赖2.2 配置web.xml文件2.3 配置springmvc2.4 配置mybatis 3.java代码---测试 一&#xff1a;原理 1.springmvc&#xff1a; 1&#xff09;.客户端发送…

SSM框架架构,原理及整合流程(eclipse)

SSM框架整合 一.SSM框架1.1SSM四个分层架构的作用与联系1.2 SSM框架原理 二.SSM框架整合流程2.1SSM框架整合后完成一个功能的步骤 一.SSM框架 SSM&#xff1a;spring MVC &#xff0c;spring和mybatis框架的整合&#xff0c;是标准的MVC模式&#xff0c;将整个系统划分为view层…

SSM框架的原理和运行流程

SSM框架的工作原理及运行流程 SSM框架简介SpringMVCSpringMybatis参考文章 SSM框架简介 SSM&#xff08;SpringSpringMVCMyBatis&#xff09;框架集由Spring、MyBatis两个开源框架整合而成&#xff08;SpringMVC是Spring中的部分内容&#xff09;。SSM常作为数据源较简单的web…

完全二叉树与满二叉树的区别+计算二叉树深度

1、完全二叉树与满二叉树的区别&#xff1a; 满二叉树&#xff1a;深度为k且有2^k-1个结点的二叉树称为满二叉树。 完全二叉树&#xff1a;设二叉树的深度为h&#xff0c;除第 h 层外&#xff0c;其它各层 (1&#xff5e;h-1) 的结点数都达到最大个数&#xff0c;第 h 层所有…

数据结构—二叉树深度优先遍历

二叉树是一种常见的数据结构&#xff0c;理解二叉树对于理解AVL树、红黑树都有重要意义&#xff0c;索性再重新梳理一下思路&#xff0c;加深印象。本文重点介绍二叉查找树。 1.二叉树与二叉查找树 二叉树&#xff08;binary tree&#xff09;是树的一种特殊形式&#xff0c;…

4.13每日一题之二叉树深度(洛谷c++)|dfs遍历树

&#x1f344;前言 大家好哇&#xff0c;我是一勺黑猫。今天是每日一题的第十三天&#xff0c;欢迎更多小伙伴加入到我们的打卡计划中&#xff0c;希望和你们在学习算法的路上一起进步~ &#x1f64e;作者简介&#xff1a;一个正在努力学算法和后端的大三girl ⏳每日一题打卡地…

二叉树深度优先搜索算法

题目&#xff1a; 输入一颗二叉树和一个整数&#xff0c;打印出二叉树中结点值的和为输入整数的所有路径。路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径。 思路分析&#xff1a; 是图形相关的算法。首先考虑解决图形相关的广度搜索优先算法就是深度搜…

leetcode104---求二叉树深度

leetcode104—求二叉树深度 给定一个二叉树&#xff0c;找出其最大深度。 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。 思路 对于二叉树深度问题&#xff0c;深度为左右子树深度最大值加1 depth max(left_depth, right_depth)1二叉树问题考虑递归方法&…

二叉树深度优先遍历-递归实现

二叉树深度优先遍历的递归实现 一、深度优先遍历二、先序遍历1.算法思路2.代码实现 三、中序遍历1.算法思路2.代码实现 四、后序遍历1.算法思路2.代码实现 一、深度优先遍历 对每一个可能的分支路径深入到不能再深入为止&#xff0c;而且每个结点只能访问一次。 要特别注意的是…

二叉树深度和高度_二叉树的高度和深度

二叉树深度和高度 In this tutorial, we will learn how to find height and depth of binary tree with program implementation in C++. It is one of the most commonly used non-linear data structures. We will learn about: 在本教程中,我们将学习如何使用C ++中的程序…

【数据结构】计算二叉树深度完整C语言代码

【数据结构】二叉树深度的计算 二叉树的深度计算完整代码展示程序结果 二叉树的深度计算 我们先看一个深度为3的二叉树。想求得此二叉树深度&#xff0c;先计算左孩子深度&#xff0c;再计算右孩子深度&#xff0c;比较得出最大值&#xff0c;即二叉树深度。 通过先序序列键盘…