ssm的架构及整合说明
SSM,即 SpringMVC、Spring 与 MyBatis 三个框架
它们在三层架构中所处的位置是不同的,即它们在三层架构中的功能各不相同,各司其职
SpringMVC:作为 View 层的实现者,完成用户的请求接收功能。SpringMVC 的 Controller
作为整个应用的控制器,完成用户请求的转发及对用户的响应
MyBatis:作为 Dao 层的实现者,完成对数据库的增、删、改、查功能
Spring:以整个应用大管家的身份出现。整个应用中所有 Bean 的生命周期行为,均由
Spring 来管理。即整个应用中所有对象的创建、初始化、销毁,及对象间关联关系的维
护,均由 Spring 进行管理

持久层:DAO层(mapper)
- DAO层:DAO层主要是做数据持久层的工作,负责与数据库进行联络的一些任务都封装在此,
- DAO层的设计首先是设计DAO的接口,
- 然后在Spring的配置文件中定义此接口的实现类,
- 然后就可在模块中调用此接口来进行数据业务的处理,而不用关心此接口的具体实现类是哪个类,显得结构非常清晰,
- DAO层的数据源配置,以及有关数据库连接的参数都在Spring的配置文件中进行配置。
业务层:Service层
- Service层:Service层主要负责业务模块的逻辑应用设计。
- 首先设计接口,再设计其实现的类
- 接着再在Spring的配置文件中配置其实现的关联。这样我们就可以在应用中调用Service接口来进行业务处理。
- Service层的业务实现,具体要调用到已定义的DAO层的接口,
- 封装Service层的业务逻辑有利于通用的业务逻辑的独立性和重复利用性,程序显得非常简洁。
表现层:Controller层(Handler层)
- Controller层:Controller层负责具体的业务模块流程的控制,
- 在此层里面要调用Service层的接口来控制业务流程,
- 控制的配置也同样是在Spring的配置文件里面进行,针对具体的业务流程,会有不同的控制器,我们具体的设计过程中可以将流程进行抽象归纳,设计出可以重复利用的子单元流程模块,这样不仅使程序结构变得清晰,也大大减少了代码量。
View层
- View层 此层与控制层结合比较紧密,需要二者结合起来协同工发。View层主要负责前台jsp页面的表示.
各层联系
- DAO层,Service层这两个层次都可以单独开发,互相的耦合度很低,完全可以独立进行,这样的一种模式在开发大项目的过程中尤其有优势
Controller,View层因为耦合度比较高,因而要结合在一起开发,但是也可以看作一个整体独立于前两个层进行开发。这样,在层与层之前我们只需要知道接口的定义,调用接口即可完成所需要的逻辑单元应用,一切显得非常清晰简单。
Service逻辑层设计
- Service层是建立在DAO层之上的,建立了DAO层后才可以建立Service层,而Service层又是在Controller层之下的,因而Service层应该既调用DAO层的接口,又要提供接口给Controller层的类来进行调用,它刚好处于一个中间层的位置。每个模型都有一个Service接口,每个接口分别封装各自的业务处理方法。
SSM框架整合说明
整合Dao层
MyBatis配置文件 sqlMapConfig.xml
- 配置别名:用于批量扫描Pojo包
- 不需要配置mappers标签,但一定要保证
mapper.java文件与mapper.xml文件同名。
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration>
<!-- 配置别名 -->
<typeAliases> <!-- 批量扫描别名 -->
<package name="cn.itcast.ssm.po"/>
</typeAliases>
</configuration>
Spring配置文件 applicationContext-dao.xml
- 主要配置内容
- 数据源
- SqlSessionFactory
- mapper扫描器
- 这里使用
sqlSessionFactoryBeanName属性是因为如果配置的是sqlSessionFactory属性,将不会先加载数据库配置文件及数据源配置
- 这里使用
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> <!-- 加载db.properties文件中的内容,db.properties文件中key命名要有一定的特殊规则 -->
<context:property-placeholder location="classpath:db.properties" />
<!-- 配置数据源 ,dbcp --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="maxActive" value="30" />
<property name="maxIdle" value="5" />
</bean> <!-- sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 数据库连接池 -->
<property name="dataSource" ref="dataSource" />
<!-- 加载mybatis的全局配置文件 -->
<property name="configLocation" value="classpath:mybatis/sqlMapConfig.xml" />
</bean> <!-- mapper扫描器 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 扫描包路径,如果需要扫描多个包,中间使用半角逗号隔开 -->
<property name="basePackage" value="cn.itcast.ssm.mapper"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>
</beans>
创建所需的Mapper.java
- 一般不动原始生成的po类,而是将原始类进行集成vo类
public interface ItemsMappperCustom{
public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo) throws Exception;
}
创建POJO类对应的mapper.xml
<mapper namespace="test.ssm.mapper.ItemsMappperCustom">
<select id="findItemsList" parameterTyep="test.ssm.po.ItemsQueryVo" resultType="test.ssm.po.ItemsCustom">
select items.* from items
where items.name like '%${itemsCustom.name}%'
整合service层
- 目标:让spring管理service接口。
定义service接口
- 一般在ssm.service包下定义接口 eg:ItemsService
public interfae ItemsService{
public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo) throws Exception;
}
定义ServiceImpl实现类
- 因为在
applicationContext-dao.xml中已经使用了mapper扫描器,这里可以直接通过注解的方式将itemsMapperCustom自动注入。public class ItemsServiceImpl implements ItemsService{ @Autowired
private ItemsMapperCustom itemsMapperCustom; @Override
public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo) throws Exception{
return itemsMapperCustom.findItemsList(itemsQueryVo);
}
}
在spring容器配置service
applicationContext-service.xml在此文件中配置service。<bean id="itemsService" class="test.ssm.service.impl.ItemsSrviceImpl"/>
事物控制
- 在
applicationContext-transaction.xml中使用spring声明式事务控制方法 - 对mybatis操作数据库事物控制,spring使用jdbc的事物控制类是
DataSourceTransactionManager - 因为操作了数据库需要事物控制,所以需要配置数据源
- 定义了切面
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> <!-- 事务管理器 对mybatis操作数据库事务控制,spring使用jdbc的事务控制类 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 数据源在 dataSource在applicationContext-dao.xml中已经配置-->
<property name="dataSource" ref="dataSource"/>
</bean> <!-- 通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 传播行为 -->
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="insert*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
<tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
<tx:method name="select*" propagation="SUPPORTS" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- aop -->
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* cn.itcast.ssm.service.impl.*.*(..))"/>
</aop:config> </beans>
整合springmvc
- 创建
springmvc.xml文件,配置处理器映射器 、 适配器、视图解析器<context:component-scan base-package="cn.itcast.ssm.controller"></context:component-scan> <!-- 使用 mvc:annotation-driven 加载注解映射器和注解适配器配置-->
<mvc:annotation-driven></mvc:annotation-driven> <!-- 视图解析器 解析jsp解析,默认使用jstl标签,classpath下的得有jstl的包
-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 配置jsp路径的前缀 -->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!-- 配置jsp路径的后缀 -->
<property name="suffix" value=".jsp"/>
</bean>
配置前端控制器
- 在
web.xml中加入如下内容 - contextConfigLocation配置springmvc加载的配置文件(配置处理器映射器、适配器等等)
- 如果不配置contextConfigLocation,默认加载的是/WEB-INF/servlet名称-serlvet.xml(springmvc-servlet.xml)
- 在url-pattern中
- 填入
*.action,表示访问以.action结尾 由DispatcherServlet进行解析 - 填入
/,所有访问的地址都由DispatcherServlet进行解析,对于静态文件的解析需要配置不让DispatcherServlet进行解析,使用此种方式可以实现RESTful风格的url
- 填入
<!-- springmvc前端控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/springmvc.xml</param-value>
</init-param>
</servlet> <servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
编写Controller(Handler)
@Congtroller
@RequestMapping("/items") //窄化路径
public class ItemsController {
@Autowired
private ItemsService itemsService;
//商品查询
@RequestMapping("/queryItems") //实际网址后面跟了.action
public ModelAndView queryItems(HttpServletRequest request) throws Exception {
List<ItemsCustom> itemsList = itemsService.findItemsList(null);
//返回ModelAndView
ModelAndView modelAndView = new ModelAndView();
//相当于request的setAttribute,在jsp页面中通过itemsList取数据
modelAndView.addObject("itemsList",itemsList);
return modelAndView;
}
}
编写JSP页面
<c:forEach items="${itemsList }" var="item">
<tr>
<td>${item.name }</td>
<td>${item.price }</td>
<td><fmt:formatDate value="${item.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/></td>
<td>${item.detail }</td>
<td><a href="${pageContext.request.contextPath }/items/editItems.action?id=${item.id}">修改</a></td>
</tr>
</c:forEach>
加载spring容器
- 在
web.xml中,添加spring容器监听器,加载spring容器<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/spring/applicationContext-*.xml</param-value>
</context-param> <listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
<listener>
参考bolg:
http://blog.csdn.net/wj880627/article/details/52973854
http://blog.csdn.net/lutianfeiml/article/details/51864160
ssm的架构及整合说明的更多相关文章
- SSM 框架-06-详细整合教程(IDEA版)(Spring+SpringMVC+MyBatis)
SSM 框架-06-详细整合教程(IDEA版)(Spring+SpringMVC+MyBatis) SSM(Spring.Spring MVC和Mybatis)如果你使用的是 Eclipse,请查看: ...
- SSM 框架-05-详细整合教程(Eclipse版)(Spring+SpringMVC+MyBatis)
SSM 框架-05-详细整合教程(Eclipse版)(Spring+SpringMVC+MyBatis) 如果你使用的是 Intellij IDEA,请查看: SSM的配置流程详细的写了出来,方便很少 ...
- SSM三大框架详细整合流程
1.基本概念 1.1.Spring Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-One ...
- ssm框架搭建和整合流程
Spring + SpringMVC + Mybatis整合流程 1 需求 1.1 客户列表查询 1.2 根据客户姓名模糊查询 2 整合思路 第一步:整合dao层 ...
- SSM框架简介及整合教程
1.Spring Spring 框架是 Java 应用最广的框架,它的成功来源于理念,而不是技术本身,它的理念包括 IoC (控制反转) 和 A面向切面编程).Spring框架是个轻量级的Java E ...
- SSM Spring +SpringMVC+Mybatis 整合配置 及pom.xml
SSM Spring +SpringMVC+Mybatis 配置 及pom.xml SSM框架(spring+springMVC+Mybatis) pom.xml文件 maven下的ssm整合配置步骤
- 20 SSM三大框架的整合
1.SSM整合的相关概念 (1)整合说明:SSM整合可以使用多种方式,优先使用XML + 注解的方式(2)整合的思路 1.先搭建整合的环境 2.先把Spring的配置搭建完成 3.再使用Spring整 ...
- SSM三大框架的整合
好好学习,天天向上 本文已收录至我的Github仓库DayDayUP:github.com/RobodLee/DayDayUP,欢迎Star,更多文章请前往:目录导航 在Java后端开发领域,Spri ...
- SSM框架的配置整合(包含配置文件代码)
由于SSM框架学习都要去网上或者以前的项目拷贝相同的代码,所以我在此把自己用到的配置文件全放在这里,帮助自己,帮助别人 首先开始前导入依赖和处理静态资源导出问题 <dependencies> ...
随机推荐
- hdu 2159 FATE (二维完全背包)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=2159 思路: dp[j][k] 代表消耗耐久度j,干掉k个敌人获得的经验值. 状态转移方程为: dp[j] ...
- 二叉搜索树(BST)详解
前言:平衡树的前置知识吧 二叉搜索树的定义: 二叉搜索树或者是一棵空树,或者是具有下列性质的二叉树: (1)若左子树不空,则左子树上所有结点的值均小于或等于它的根节点的值: (2)若右子树不空,则右子 ...
- php 部署在iis HTTP 错误 500.0 - Internal Server Error 无法在<fastCGI>应用程序配置中找到<handler> scriptProcessor
原因,从A服务器复制一个部署在IIS上的PHP项目,根节点指向 publc/web.config 把里面涉及的 php路径改成正确的即可
- 正睿 2019 省选附加赛 Day10
A 核心就是一个公式 \[\sum_{i = 0}^{k} S(k, i) \tbinom{x}{i} i\] S是第二类斯特林数 递推公式 \(S_2(n,k)=S_2(n−1,k−1)+kS_2( ...
- 【Tsinsen A1339】JZPLCM (树状数组)
Description 原题链接 给定一长度为\(~n~\)的正整数序列\(~a~\),有\(~q~\)次询问,每次询问一段区间内所有数的\(~LCM~\)(即最小公倍数).由于答案可能很大,输出 ...
- AES解密后多了\0
AES加密解密之后发现多了几个空格,不知道原因.在调试时发现多了\0这种东西 不知道为什么会多这些.后来.replace("\0","")这样做了事
- [FJOI2016]神秘数(脑洞+可持久化)
题目描述 一个可重复数字集合S的神秘数定义为最小的不能被S的子集的和表示的正整数.例如S={1,1,1,4,13}, 1 = 1 2 = 1+1 3 = 1+1+1 4 = 4 5 = 4+1 6 = ...
- 初入react-redux (基于webpack babel的react应用框架)
react这么热门的框架也不介绍了,redux是一个单项数据流的小框架,当然不只配合react,它起初是为react而配的,现在面向所有了,比如ng-redux的项目.redux做为react的标准搭 ...
- 洛谷P2805 植物大战僵尸
题意:给你一张图,每个节点保护若干节点. 当一个节点不被保护的时候,你就可以gay掉它. gay每个节点都有收益(可能为负),求最大总收益. 解:首先发现是一个最大权闭合子图. 把保护关系变成被保护, ...
- JavaScript深入之词法作用域和动态作用域
作用域 作用域是指程序源代码中定义变量的区域. 作用域规定了如何查找变量,也就是确定当前执行代码对变量的访问权限. JavaScript 采用词法作用域,也就是静态作用域. 静态作用域与动态作用域 因 ...