[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;
~~~
## 步驟 2 : 準備數據
~~~
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;
~~~
## 步驟 3 : JDK版本
本教程是通過JDK8編譯的,如果您機器上運行環境是低版本JDK,將無法識別其中的類,請使用JDK8,勿使用JDK9 或者更高版本,有兼容性風險。
## 步驟 4 : 先運行,再學習
SSM整合需要做不少步驟,任何一步部做漏了,做錯了,都有可能失敗,這樣會影響學習的信心。
所以將完整的 SSM 項目(向老師要相關資料),解壓后導入到eclipse中,啟動Tomcat,觀察是否正常運行。確定可以運行,確定教程是可以跑得起來的,再學習下面的內容。
部署成功之后,測試地址
`http://127.0.0.1:8080/ssm/listCategory`

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

## 步驟 6 : 導入jar包
拿到lib.rar(向老師要相關資料),并解壓拷貝jar放到WEB-INF/lib文件夾中;
## 步驟 7 : 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 + "]";
}
}
~~~
## 步驟 8: 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();
}
~~~
## 步驟 9: 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>
~~~
## 步驟 10: CategoryService
~~~
package com.dodoke.service;
import java.util.List;
import com.dodoke.pojo.Category;
public interface CategoryService {
List<Category> list();
}
~~~
## 步驟 11: 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();
}
}
~~~
## 步驟 12: 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;
}
}
~~~
## 步驟 13: 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>
~~~
## 步驟 14: 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>
~~~
## 步驟 15: 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>
~~~
## 步驟 16 : 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>
~~~
## 步驟 17: 部署在tomcat中,重啟測試
部署在Tomcat中,重啟tomcat,然后訪問地址,觀察效果
`http://127.0.0.1:8080/ssm/listCategory`

## 步驟 18: 思路圖
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 中顯示數據

## 步驟 19: 刪掉,從頭開始,這次全部自己做
從頭到尾自己再做一遍,把這個知識變成自己的東西。
- 數據庫
- 數據庫介紹
- MySQL的安裝
- SQL
- 表基本操作
- 修改數據語句
- 數據檢索操作
- 多表數據操作
- 練習題
- JAVA
- JAVA 介紹
- JAVA 運行原理
- JDK 配置
- 類和對象
- 數據類型
- 變量
- 直接量
- 運算符
- 流程控制
- 數組結構
- 面向對象
- 隱藏和封裝
- 深入構造器
- 類的繼承
- 多態
- 包裝類
- final 修飾符
- 抽象類
- 接口
- 集合框架
- 常用類學習
- 設計模式-單例模式
- 異常處理
- JDBC
- JSP&Servlet
- Web應用
- Tomcat
- JSP
- Scriptlet
- Page 指令
- 包含指令
- 跳轉指令
- 用戶注冊實例
- JSP練習
- 內置對象
- Servlet
- 過濾器
- Web分層思想
- EL表達式
- JSTL
- 分頁實現
- AJAX&JSON
- 開發步驟
- 路徑問題
- Log4j
- 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項目
- 教學管理
- 學員名錄
- 周測統計
- 20180608
- 20180706
- 20180721
- 課堂作業
- 練習