我们在使用ssm搭建框架的使用,首先要知道有哪几层,以及各层之间有什么作用,这样我们搭建框架才可以事半功倍。主要对下面的四层简单介绍一下:
1:持久层:dao层(mapper)
Dao层:主要是做数据持久层的工作,负责和数据库进行联络的一些任务在此封装,dao层的设计首先是dao的接口,配置数据源,以及有关数据库连接的参数在spring的配置文件中进行配置。
2:业务层:service层
Service层:主要负责业务模块的逻辑应用设计;先设计接了,在设计其实现的类,业务逻辑实现具体要调用已定义的dao层的接口。
Service层逻辑设计
Service层建立在dao层之上,建立dao层之后才可以建立service层,而service层又在controller层之下,因而service层应该既要调用dao层的接口,又要提供接口给controller层的类来调用,它正好处在一个中间的位置,每个模型都有一个service接口,每个接口分别封装各自的业务处理的方法。
3:表现层:controller层
Controller层:主要负责具体的业务模块流程的控制,在此层要调用service层的接口来控制业务流程。
4:View层
View层:主要和控制层紧密结合,主要负责前台jsp页面的表示。
各个层之间的联系:
DAO层,Service层这两个层次都可以单独开发,互相的耦合度很低,完全可以独立进行,这样的一种模式在开发大项目的过程中尤其有优势,Controller,View层因为耦合度比较高,因而要结合在一起开发,但是也可以看作一个整体独立于前两个层进行开发。这样,在层与层之前我们只需要知道接口的定义,调用接口即可完成所需要的逻辑单元应用,一切显得非常清晰简单。
下面结合ssm框架说明
1:整合dao层
Mybatis配置文件mybatis-config.xml
配置别名:用于批量扫描pojo包
不需要配置mapper标签,但一定要保证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>
2: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>
3:创建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}%'
4:整合service层
目标:让spring管理service接口,定义service接口eg:ItemsService
public interfae ItemsService{
public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo) throws Exception;
}
定义serviceImpl实现类 :因为在applicationContext-dao.xml中已经使用了mapper扫描器,这里就可以使用通过注解的方式将itemMapperCustoms自定注入
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"/>
5:事物控制
在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>
6:整合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>
同时在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>
8:编写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;
}
}
9:编写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>