SSM,即 SpringMVC、Spring 與 MyBatis 三個框架前端
它們在三層架構中所處的位置是不一樣的,即它們在三層架構中的功能各不相同,各司其職java
SpringMVC:做爲 View 層的實現者,完成用戶的請求接收功能。SpringMVC 的 Controllerweb
做爲整個應用的控制器,完成用戶請求的轉發及對用戶的響應spring
MyBatis:做爲 Dao 層的實現者,完成對數據庫的增、刪、改、查功能sql
Spring:以整個應用大管家的身份出現。整個應用中全部 Bean 的生命週期行爲,均由
Spring 來管理。即整個應用中全部對象的建立、初始化、銷燬,及對象間關聯關係的維
護,均由 Spring 進行管理數據庫
Controller,View層由於耦合度比較高,於是要結合在一塊兒開發,可是也能夠看做一個總體獨立於前兩個層進行開發。這樣,在層與層以前咱們只須要知道接口的定義,調用接口便可完成所須要的邏輯單元應用,一切顯得很是清晰簡單。apache
Service邏輯層設計spring-mvc
sqlMapConfig.xml
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>
applicationContext-dao.xml
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>
public interface ItemsMappperCustom{ public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo) throws Exception; }
<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}%'
public interfae ItemsService{ public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo) throws Exception; }
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); } }
applicationContext-service.xml
在此文件中配置service。
<bean id="itemsService" class="test.ssm.service.impl.ItemsSrviceImpl"/>
applicationContext-transaction.xml
中使用spring聲明式事務控制方法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.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
中加入以下內容*.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>
@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; } }
<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>
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:mybatis
http://blog.csdn.net/wj880627/article/details/52973854架構
http://blog.csdn.net/lutianfeiml/article/details/51864160