[TOC]
# 整合步驟
本例演示從0開始逐一整合SSM的步驟,要學習本知識,需要具備Spring, SpringMVC, Mybatis 的基礎,如果沒有這些基礎,請把基礎掌握之后再學習,不要跳躍學習,欲速則不達。
> 基于框架的程序要成功運行,對于JAR包的版本,配置文件的正確性有著苛刻的要求,任何一個地方出錯了,都會導致框架程序運行失敗。
>
> 技巧: 學習框架,**務必嚴格按照教程的指導,完全模仿操作**,直到成功看到運行效果。 第一次成功之后,信心,思路都會有較好的鋪墊,然后再根據自己的疑惑,在“成功”的代碼上做原本想做的改動和調整,這樣可以大大節約學習的時間,提高效率,**切勿一來就擅自改動**,給自己的學習制造障礙。
>
## 步驟 1 :準備數據庫
1. 創建數據庫 ssm_integration
`create database ssm_integration;`
2. 創建表
接著準備表category,只有2個字段id和name
~~~
CREATE TABLE `category` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(30) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
~~~
## 步驟 4 : 準備數據
~~~
insert into category values(null,"category1");
insert into category values(null,"category2");
insert into category values(null,"category3");
insert into category values(null,"category4");
insert into category values(null,"category5");
select * from category;
~~~
## 步驟 5 : JDK版本
本教程是通過JDK8編譯的,如果您機器上運行環境是低版本JDK,將無法識別其中的類,請使用JDK8,勿使用JDK9 或者更高版本,有兼容性風險。
## 步驟 6 : 先運行,再學習
SSM整合需要做不少步驟,任何一步部做漏了,做錯了,都有可能失敗,這樣會影響學習的信心。
所以將完整的 SSM 項目(向老師要相關資料),解壓后導入到eclipse中,啟動Tomcat,觀察是否正常運行。確定可以運行,確定教程是可以跑得起來的,再學習下面的內容。
部署成功之后,測試地址
`http://127.0.0.1:8080/ssm/listCategory`

## 步驟 7 : 新建項目
在eclipse中新建項目ssm,使用dynamic web project的方式。
注意,next點擊,選擇web.xml,不要直接finish。

## 步驟 8 : 導入jar包
拿到lib.rar(向老師要相關資料),并解壓拷貝jar放到WEB-INF/lib文件夾中;
## 步驟 9 : pojo
~~~
package com.dodoke.pojo;
public class Category {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Category [id=" + id + ", name=" + name + "]";
}
}
~~~
## 步驟 10 : CategoryMapper
~~~
package com.dodoke.mapper;
import java.util.List;
import com.dodoke.pojo.Category;
public interface CategoryMapper {
public int add(Category category);
public void delete(int id);
public int update(Category category);
public Category get(int id);
public List<Category> list();
}
~~~
## 步驟 11 : Category.xml
Category.xml需要和CategoryMapper放在同一個包下面,并且namespace必須寫CategoryMapper的完整類名
~~~
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.dodoke.mapper.CategoryMapper">
<insert id="add" parameterType="Category">
insert into category(name) values(#{name})
</insert>
<delete id="delete" parameterType="Category">
delete from category where id=#{id}
</delete>
<update id="update" parameterType="Category">
update category set name=#{name} where id=#{id}
</update>
<select id="get" parameterType="int" resultType="Category">
select * from category where id=#{id}
</select>
<select id="list" resultType="Category">
select * from category
</select>
</mapper>
~~~
## 步驟 12 : CategoryService
~~~
package com.dodoke.service;
import java.util.List;
import com.dodoke.pojo.Category;
public interface CategoryService {
List<Category> list();
}
~~~
## 步驟 13 : CategoryServiceImpl
CategoryServiceImpl被注解@Service標示為一個Service
并且裝配了categoryMapper
~~~
package com.dodoke.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dodoke.mapper.CategoryMapper;
import com.dodoke.pojo.Category;
import com.dodoke.service.CategoryService;
@Service
public class CategoryServiceImpl implements CategoryService {
@Autowired
CategoryMapper categoryMapper;
@Override
public List<Category> list() {
// TODO Auto-generated method stub
return categoryMapper.list();
}
}
~~~
## 步驟 14 : CategoryController
CategoryController被@Controller標示為了控制器
@Autowired自動裝配了categoryService
通過@RequestMapping映射訪問路徑/listCategory路徑到方法listCategory()。
在listCategory()方法中,通過categoryService獲取后,然后存放在"cs"這個key上。
~~~
package com.dodoke.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.dodoke.pojo.Category;
import com.dodoke.service.CategoryService;
//告訴spring mvc這是一個控制器類
@Controller
@RequestMapping("")
public class CategoryController {
@Autowired
CategoryService categoryService;
@RequestMapping("listCategory")
public ModelAndView listCategory() {
ModelAndView mav = new ModelAndView();
List<Category> cs = categoryService.list();
// 放入轉發參數
mav.addObject("cs",cs);
// 放入jsp路徑
mav.setViewName("listCategory");
return mav;
}
}
~~~
## 步驟 15 : web.xml
web.xml有兩個作用:
1. 通過ContextLoaderListener在web app啟動的時候,獲取contextConfigLocation配置文件的文件名applicationContext.xml,并進行Spring相關初始化工作
2. 有任何訪問,都被DispatcherServlet所攔截,這就是Spring MVC那套工作機制了。
~~~
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>ssm</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- spring的配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- spring mvc核心:分發servlet -->
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- spring mvc的配置文件 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springMVC.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
~~~
## 步驟 16 : applicationContext.xml
在src目錄下新建applicationContext.xml文件,這是Spring的配置文件,其作用
1. 通過注解,將Service的生命周期納入Spring的管理
~~~
<context:annotation-config />
<context:component-scan base-package="com.dodoke.service" />
~~~
2. 配置數據源
~~~
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
~~~
3. 掃描存放SQL語句的Category.xml
~~~
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean">
~~~
4. 掃描Mapper,并將其生命周期納入Spring的管理
~~~
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
~~~
applicationContext.xml 完整代碼:
~~~
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- Spring配置文件 -->
<!-- 支持注解 -->
<context:annotation-config />
<!-- 自動掃描包,將Service的生命周期納入Spring的管理 -->
<context:component-scan base-package="com.dodoke.service" />
<!-- 配置數據源 -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/ssm_integration?characterEncoding=UTF-8</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value></value>
</property>
</bean>
<!-- MyBatis的配置 -->
<!-- 掃描存放SQL語句的xml映射文件 -->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="typeAliasesPackage" value="com.dodoke.pojo" />
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations" value="classpath:com/dodoke/mapper/*.xml" />
</bean>
<!-- 掃描Mapper,并將其生命周期納入Spring的管理 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.dodoke.mapper" />
</bean>
</beans>
~~~
## 步驟 17 : springMVC.xml
在src目錄下新建springMVC.xml
1. 掃描Controller,并將其生命周期納入Spring管理
~~~
<context:component-scan base-package="com.dodoke.controller">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>
~~~
2. 注解驅動,以使得訪問路徑與方法的匹配可以通過注解配置
`<mvc:annotation-driven />`
3. 靜態頁面,如html,css,js,images可以訪問
`<mvc:default-servlet-handler />`
4. 視圖定位到/WEB/INF/jsp 這個目錄下
~~~
<!-- 視圖定位到/WEB/INF/jsp 這個目錄下 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
~~~
springMVC.xml 完整代碼:
~~~
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<!-- SpringMvc配置文件 -->
<!-- 支持注解 -->
<context:annotation-config />
<!-- 自動掃描包 -->
<context:component-scan base-package="com.dodoke.controller">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<!-- 注解驅動,以使得訪問路徑與方法的匹配可以通過注解配置 -->
<mvc:annotation-driven />
<!-- 靜態頁面,如html,css,js,images可以訪問 -->
<mvc:default-servlet-handler />
<!-- 視圖定位到/WEB/INF/jsp 這個目錄下 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
~~~
## 步驟 18 : listCategory.jsp
在WEB-INF下創建jsp目錄,并創建文件listCategory.jsp。
在這個jsp文件中,通過forEach標簽,遍歷CategoryController傳遞過來的集合數據。
~~~
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<table border='1' cellspacing='0'>
<tr>
<td>id</td>
<td>name</td>
</tr>
<c:forEach items="${cs}" var="c" varStatus="st">
<tr>
<td>${c.id}</td>
<td>${c.name}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
~~~
## 步驟 19 : 部署在tomcat中,重啟測試
部署在Tomcat中,重啟tomcat,然后訪問地址,觀察效果
`http://127.0.0.1:8080/ssm/listCategory`

## 步驟 20 : 思路圖
1. 首先瀏覽器上訪問路徑 /listCategory
2. tomcat根據web.xml上的配置信息,攔截到了/listCategory,并將其交由DispatcherServlet處理。
3. DispatcherServlet 根據springMVC的配置,將這次請求交由CategoryController類進行處理,所以需要進行這個類的實例化
4. 在實例化CategoryController的時候,注入CategoryServiceImpl。 (自動裝配實現了CategoryService接口的的實例,只有CategoryServiceImpl實現了CategoryService接口,所以就會注入CategoryServiceImpl)
5. 在實例化CategoryServiceImpl的時候,又注入CategoryMapper
6. 根據ApplicationContext.xml中的配置信息,將CategoryMapper和Category.xml關聯起來了。
7. 這樣拿到了實例化好了的CategoryController,并調用 list 方法
8. 在listCategory方法中,訪問CategoryService,并獲取數據,并把數據放在"cs"上,接著服務端跳轉到listCategory.jsp去
9. 最后在listCategory.jsp 中顯示數據

## 步驟 21 : 刪掉,從頭開始,這次全部自己做
從頭到尾自己再做一遍,把這個知識變成自己的東西。
## 常見問題
1. `<property name="typeAliasesPackage" value="com.dodoke.pojo" />`用處是什么?
> 在Mapper文件里面就可以直接寫對應的類名,而不用寫全路徑名了,即Mybatis中的別名配置。
- 前言
- 計算機概論
- MySQL數據庫
- 數據庫介紹
- MySQL的安裝
- SQL
- 表基本操作
- 修改數據語句
- 數據檢索操作
- 多表數據操作
- 表結構設計
- 綜合應用
- SQL-SERVER
- 數據庫介紹
- SQL-SERVER安裝
- SQL
- 表基本操作
- JAVA
- JAVA 介紹
- JAVA 運行原理
- JDK 配置
- 類和對象
- 數據類型
- 變量
- 直接量
- 運算符
- 流程控制
- 數組結構
- 面向對象
- 隱藏和封裝
- 深入構造器
- 類的繼承
- 多態
- 包裝類
- final 修飾符
- 抽象類
- 接口
- 集合框架
- 常用類學習
- 異常處理
- 設計模式-單例模式
- JDBC
- HTML基礎
- Web原理和HTML簡介
- Web原理
- HTML概念
- HTML標簽
- 標簽
- HTML固定基本結構
- 第一個HTML頁面
- 工具的使用
- 標題
- hr和p標簽
- 路徑概念
- 超級鏈接
- 列表
- 表格
- 表單的設計與使用
- 表單域的原理
- 文本框和密碼框
- 單選框和復選框
- 下拉列表框
- 多行文本和上傳
- 提交按鈕和重置按鈕
- 為CODING COFFEE加入在線購買頁
- HTML5
- 定位服務
- CSS基礎
- CSS的基礎使用
- CSS簡介
- CSS樣式規則和加載方式
- 內聯元素和區塊元素介紹
- 選擇器
- 偽類
- CSS顏色
- 背景圖片
- 文本
- CSS列表
- DIV+CSS布局
- 盒子模型的邊距和邊框
- Display屬性
- 浮動和清除浮動
- 用Position屬性進行定位
- 專題:居中和對齊
- CSS新特性
- CSS3邊框
- 動畫
- JavaScript基礎
- Hello World!
- 語句和變量
- 一切皆對象
- 標識符、注釋和區塊
- 基本數據類型和引用數據類型
- 語句
- 條件語句
- 循環語句
- 數據類型
- typeof
- number
- 字符串
- 布爾類型
- 函數
- 數組
- 運算符
- 加法運算符
- 算術、賦值、比較運算符
- 布爾運算符
- DOM模型
- DOM和DOM節點
- 特征相關屬性
- 節點對象的方法
- Element對象
- Attribute對象
- Text節點和CSS操作
- 事件模型
- 標準庫
- Number對象
- String對象
- Array對象
- Date、Boolean和Math對象
- JSON對象
- 面向對象編程中的 this
- Web Storage
- 錯誤處理機制
- Error對象和try..catch語句
- javascript的原生錯誤類型
- BOM模型
- window對象
- 計時事件
- jQuery基礎
- 認識jQuery
- jQuery對象和DOM對象
- jQuery選擇器
- jQuery Dom操作
- 查找節點和創建節點
- 插入節點和刪除節點
- 復制節點和替換節點
- 包裹節點和屬性操作
- 樣式操作
- 設置和獲取HTML、文本和值
- 遍歷節點和CSS操作
- jQuery 事件和動畫
- 事件綁定與冒泡處理
- jQuery動畫
- jQuery 插件
- validate 插件
- jQuery與Ajax的應用
- Ajax簡介
- jquery中的Ajax
- Flex布局
- Flexbox介紹
- 伸縮容器屬性介紹
- dispaly屬性
- flex-direction屬性
- flex-wrap屬性
- flex-flow屬性
- align-content屬性
- justify-content屬性
- align-items屬性
- 伸縮項目屬性介紹
- order屬性
- grow屬性
- basis屬性
- shrink屬性
- flex屬性簡寫
- align-self屬性
- Bootstrap基礎
- 起步
- 柵格系統
- 排版樣式
- 表格和按鈕
- 表單和圖片
- 輔助類和響應式工具
- 圖標菜單按鈕組件
- 輸入框和導航組件
- 路徑、分頁、標簽和徽章組件
- 巨幕、頁頭、縮略圖和警告框
- 進度、條媒體對象和Well組件
- 列表組和嵌入組件
- Canvas
- Canvas坐標體系
- Canvas畫布大小設置
- Canvas畫直線
- Canvas畫圓和矩形
- Canvas描邊與填充
- Canvas圖形變換
- Canvas線性漸變
- Canvas徑向漸變
- Canvas中的文字
- Canvas圖片繪制
- Canvas圖形畫刷
- Canvas剪輯區域
- Canvas繪制陰影
- Canvas繪制曲線
- Canvas動畫
- Canvas離屏操作
- 微信小程序
- 起步
- 小程序目錄
- 小程序配置
- 新建頁面
- WXML
- 組件
- 視圖容器
- 基礎內容
- 表單組件
- button
- checkbox
- form
- input
- label
- picker
- picker-view
- radio
- slider
- switch
- textarea
- 導航
- 媒體組件
- audio
- image
- video
- camera
- live-player
- live-pusher
- 地圖
- 畫布
- 數據綁定
- 運算
- 組合
- 列表渲染
- 條件渲染
- 模板
- 事件
- WXSS
- JS
- JSP&Servlet
- Web應用
- Tomcat
- JSP
- Scriptlet
- Page 指令
- 包含指令
- 跳轉指令
- 用戶注冊實例
- JSP練習
- 內置對象
- Servlet
- 過濾器
- Web分層思想
- EL表達式
- JSTL
- 分頁實現
- AJAX&JSON
- 開發步驟
- 路徑問題
- Log4j
- 電子書城
- 案例分析
- 核心代碼
- Java高級
- 文件操作
- 泛型
- 類加載機制和反射
- 注解 Annotation
- Mybatis框架
- 框架介紹
- Mybatis簡單實現
- 表基本操作
- 優化配置文件
- 表字段名與實體類屬性名不同的解決方案
- 一對一關聯
- 一對多關聯
- Spring框架
- IOC/DI
- 注入對象
- 注解方式 IOC/DI
- AOP
- 注解方式AOP
- 注解方式測試
- Spring MVC框架
- Hello SpringMVC
- 視圖定位
- 注解方式
- 接受表單數據
- 客戶端跳轉
- Session
- 中文問題
- 上傳文件
- SSM整合
- 整合步驟
- 分頁
- PageHelper
- 連接池
- CRUD
- 事務管理
- JSON
- Maven
- 介紹
- 下載與配置MAVEN
- MAVEN倉庫
- ECLIPSE中的MAVEN設置
- ECLIPSE下創建MAVEN風格的JAVA項目
- 添加JAR包
- 創建MAVEN風格的JAVA WEB項目
- 創建SSM項目
- 使用ECLIPSE導入一個MAVEN風格的SSM項目
- 教學管理模版
- 學員名錄
- 周測統計
- 2017-10-27
- 課堂作業
- 班會紀要
- 2017-10-24
- 缺勤記錄
- 班級備忘錄
- 違紀統計