聪明文档网

聪明文档网

最新最全的文档下载
当前位置: 首页> SSM框架详细阐述

SSM框架详细阐述

时间:    下载该word文档
.

持久层: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层这两个层次都可以单独开发,互相的耦合度很低,完全可以独立进行,这样的一种模式在开发大项目的过程中尤其有优势

ControllerView层因为耦合度比较高,因而要结合在一起开发,但是也可以看作一个整体独立于前两个层进行开发。这样,在层与层之前我们只需要知道接口的定义,调用接口即可完成所需要的逻辑单元应用,一切显得非常清晰简单。

Service逻辑层设计

Service层是建立在DAO层之上的,建立了DAO层后才可以建立
Service层,而Service层又是在Controller层之下的,因而Service层应该既调用DAO层的接口,又要提供接口给Controller层的类来进行调用,它刚好
.

.
处于一个中间层的位置。每个模型都有一个Service接口,每个接口分别封装各自的业务处理方法。SSM框架整合说明整合Dao
MyBatis配置文件sqlMapConfig.xml

配置别名:用于批量扫描Pojo
不需要配置mappers标签,但一定要保证mapper.java文件与mapper.xml文件同名。

mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases><packagename="cn.itcast.ssm.po"/>typeAliases>configuration>

1234
.

.

56789

Spring配置文件applicationContext-dao.xml

主要配置容

数据源
SqlSessionFactorymapper扫描器


这里使用sqlSessionFactoryBeanName属性是因为如果配
置的是sqlSessionFactory属性,将不会先加载数据库配置文件及数据源配置<beansxmlns=".springframework.org/schema/beans"
xmlns:xsi=".w3.org/2001/XMLSchema-instance"xmlns:mvc=".springframework.org/schema/mvc"
xmlns:context=".springframework.org/schema/context"
xmlns:aop=".springframework.org/schema/aop"xmlns:tx=".springframework.org/schema/tx"
xsi:schemaLocation=".springframework.org/schema/beans
.

.
.springframework.org/schema/beans/spring-beans-3.2.xsd.springframework.org/schema/mvc
.springframework.org/schema/mvc/spring-mvc-3.2.xsd.springframework.org/schema/context
.springframework.org/schema/context/spring-context-3.2.xsd.springframework.org/schema/aop
.springframework.org/schema/aop/spring-aop-3.2.xsd.springframework.org/schema/tx
.springframework.org/schema/tx/spring-tx-3.2.xsd">

<context:property-placeholderlocation="classpath:db.properties"/>

<beanid="dataSource"class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close">
<propertyname="driverClassName"value="${jdbc.driver}"/><propertyname="url"value="${jdbc.url}"/>
<propertyname="username"value="${jdbc.username}"/>
.

.
<propertyname="password"value="${jdbc.password}"/><propertyname="maxActive"value="30"/><propertyname="maxIdle"value="5"/>bean>

<beanid="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean">

<propertyname="dataSource"ref="dataSource"/>
<propertyname="configLocation"value="classpath:mybatis/sqlMapConfig.xml"/>bean>

<beanclass="org.mybatis.spring.mapper.MapperScannerConfigurer"><propertyname="basePackage"value="cn.itcast.ssm.mapper">property>
<propertyname="sqlSessionFactoryBeanName"value="sqlSessionFactory"/>
.

.
bean>beans>
.
12345678910111213141516171819
20

.
.
212223242526272829303132333435363738394041
42

.

4344

创建所需的Mapper.java

一般不动原始生成的po类,而是将原始类进行集成vopublicinterfaceItemsMappperCustom{
publicListfindItemsList(ItemsQueryVoitemsQueryVothrowsException;}

123

创建POJO类对应的mapper.xml
namespace="test.ssm.mapper.ItemsMappperCustom">
<selectid="findItemsList"parameterTyep="test.ssm.po.ItemsQueryVo"resultType="test.ssm.po.ItemsCustom">selectitems.*fromitems
whereitems.namelike'%${itemsCustom.name}%'

12
.

.

34


整合service

目标:让spring管理service接口。定义service接口

一般在ssm.service包下定义接口egItemsServicepublicinterfaeItemsService{
publicListfindItemsList(ItemsQueryVoitemsQueryVothrowsException;}

123

定义ServiceImpl实现类

因为在applicationContext-dao.xml中已经使用了mapper扫描器,这里可以直接通过注解的方式将itemsMapperCustom自动注入。publicclassItemsServiceImplimplementsItemsService{
.

.
Autowired
privateItemsMapperCustomitemsMapperCustom;
Override
publicListfindItemsList(ItemsQueryVoitemsQueryVothrowsException{
returnitemsMapperCustom.findItemsList(itemsQueryVo;}}

12345678910

spring容器配置service

.
applicationContext-service.xml在此文件中配置service

.
"itemsService"class="test.ssm.service.impl.ItemsSrviceImpl"/>

1

事物控制(不够熟悉)

applicationContext-transaction.xml中使用spring声明式事务控制方法

mybatis操作数据库事物控制,spring使用jdbc的事物控制类是DataSourceTransactionManager

因为操作了数据库需要事物控制,所以需要配置数据源定义了切面
<beansxmlns=".springframework.org/schema/beans"
xmlns:xsi=".w3.org/2001/XMLSchema-instance"xmlns:mvc=".springframework.org/schema/mvc"
xmlns:context=".springframework.org/schema/context"
xmlns:aop=".springframework.org/schema/aop"xmlns:tx=".springframework.org/schema/tx"
xsi:schemaLocation=".springframework.org/schema/beans.springframework.org/schema/beans/spring-beans-3.2.xsd.springframework.org/schema/mvc
.springframework.org/schema/mvc/spring-mvc-3.2.xsd.springframework.org/schema/context
.

.
.springframework.org/schema/context/spring-context-3.2.xsd.springframework.org/schema/aop
.springframework.org/schema/aop/spring-aop-3.2.xsd.springframework.org/schema/tx
.springframework.org/schema/tx/spring-tx-3.2.xsd">

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

<tx:adviceid="txAdvice"transaction-manager="transactionManager"><tx:attributes>
<tx:methodname="save*"propagation="REQUIRED"/><tx:methodname="delete*"propagation="REQUIRED"/><tx:methodname="insert*"propagation="REQUIRED"/>
.

.
<tx:methodname="update*"propagation="REQUIRED"/>
<tx:methodname="find*"propagation="SUPPORTS"read-only="true"/>
<tx:methodname="get*"propagation="SUPPORTS"read-only="true"/>
<tx:methodname="select*"propagation="SUPPORTS"read-only="true"/>
tx:attributes>tx:advice><aop:config>
<aop:advisoradvice-ref="txAdvice"pointcut="execution(*.itcast.ssm.service.impl.*.*(.."/>aop:config>
beans>

123456
.

.
.
789101112131415161718192021222324252627
28

.

293031323334353637383940

整合springmvc

创建springmvc.xml文件,配置处理器映射器适配器、视图解析器<context:component-scanbase-package="cn.itcast.ssm.controller">context:component-scan>
<mvc:annotation-driven>mvc:annotation-driven>
.

.

<beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver">

<propertyname="prefix"value="/WEB-INF/jsp/"/>
<propertyname="suffix"value=".jsp"/>bean>
.
1234567891011
12

.

13

配置前端控制器

web.xml中加入如下容
contextConfigLocation配置springmvc加载的配置文件(配置处理器映射器、适配器等等)

如果不配置contextConfigLocation,默认加载的是
/WEB-INF/servlet名称-serlvet.xmlspringmvc-servlet.xml

url-pattern

填入*.action,表示访问以.action结尾DispatcherServlet
进行解析

填入/,所有访问的地址都由DispatcherServlet进行解析,对于
静态文件的解析需要配置不让DispatcherServlet进行解析,使用此种方式可以实现RESTful风格的url<servlet>
<servlet-name>springmvcservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
.

.
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:spring/springmvc.xmlparam-value>init-param>servlet>
<servlet-mapping>
<servlet-name>springmvcservlet-name><url-pattern>*.actionurl-pattern>servlet-mapping>
.
1234567891011
12

.

131415

编写ControllerHandlerCongtroller
RequestMapping("/items"//窄化路径publicclassItemsController{Autowired
privateItemsServiceitemsService;
//商品查询
RequestMapping("/queryItems"//实际网址后面跟了.action
publicModelAndViewqueryItems(HttpServletRequestrequestthrowsException{
ListitemsList=itemsService.findItemsList(null;
//返回ModelAndView
ModelAndViewmodelAndView=newModelAndView(;
.

.
//相当于requestsetAttribute,在jsp页面过itemsList取数据modelAndView.addObject("itemsList",itemsList;
returnmodelAndView;}}
.
123456789101112131415
16

.

1718192021

编写JSP页面
<c:forEachitems="${itemsList}"var="item"><tr>
<td>${item.name}td><td>${item.price}td>
<td><fmt:formatDatevalue="${item.createtime}"pattern="yyyy-MM-ddHH:mm:ss"/>td>
<td>${item.detail}td>
<td><ahref="${pageContext.request.contextPath}/items/editItems.action?id=${item.id}">修改a>td>tr>c:forEach>

.
12

.


加载spring容器

web.xml中,添加spring容器监听器,加载spring容器
.
34567891011

免费下载 Word文档免费下载: SSM框架详细阐述

  • 29.8

    ¥45 每天只需1.0元
    1个月 推荐
  • 9.9

    ¥15
    1天
  • 59.8

    ¥90
    3个月

选择支付方式

  • 微信付款
郑重提醒:支付后,系统自动为您完成注册

请使用微信扫码支付(元)

订单号:
支付后,系统自动为您完成注册
遇到问题请联系 在线客服

常用手机号:
用于找回密码
图片验证码:
看不清?点击更换
短信验证码:
新密码:
 
绑定后可用手机号登录
请不要关闭本页面,支付完成后请点击【支付完成】按钮
遇到问题请联系 在线客服