寫這篇文章主要的目的是出于自己學完了基礎的SSM架構以及自己實操完成SSM的整合後,對比網上一大堆的文章寫了關于SSM整合的文章,有寫的好的也有寫的很片面的,于是覺得需要寫一篇文章來記錄下自己的心得,希望這篇文章适合剛入門的新手去親自整合SSM,好了我們直接切入主題,主要分為如下幾個操作:
(1)準備所需要的JAR包
(2)編寫配置檔案
(3)編寫web.xml
(4)整合測試應用
【步驟一】
在操作前首先為大家展示下項目結構如下
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLiATN381dsQWZ4lmZf1GLlpXazVmcvwFciV2dsQXYtJ3bm9CX9s2RkBnVHFmb1clWvB3MaVnRtp1XlBXe0xCMy81dvRWYoNHLwEzX5xCMx8FesU2cfdGLwMzX0xiRGZkRGZ0Xy9GbvNGLpZTY1EmMZVDUSFTU4VFRR9Fd4VGdsYTMfVmepNHLrJXYtJXZ0F2dvwVZnFWbp1zczV2YvJHctM3cv1Ce-cmbw5iNzIzNxMWO2IzNhJzMkRDZyYzX4MzNyETM4EzLcdDMyIDMy8CXn9Gbi9CXzV2Zh1WavwVbvNmLvR3YxUjLyM3Lc9CX6MHc0RHaiojIsJye.png)
【步驟二】
>準備需要的JAR包目錄如下(總共29個)
PS:簡單說明下裡面的JAR分别為:Spring、Spring MVC、Mybatis、MySQL驅動包如圖所示:
這些JAR包我為大家準備好了裡面也包含了我們這個案例所需要的資料庫連結如下:
SSM整合所需的JAR包:https://pan.baidu.com/s/1Aat7qg4Ab7UO32vze1EX6g 提取碼:8a3v
【步驟三】
>編寫配置檔案,我們需要建立一個名為config的源檔案夾(Source Folder)如圖所示:
>編寫db.properties配置檔案(主要就是連接配接資料庫所需要的配置的一些參數)
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis
jdbc.username=root
jdbc.password=1234
jdbc.maxTotal=30
jdbc.maxIdle=10
jdbc.initialSize=5
>編寫applicationContext.xml配置檔案(主要就是Spring相關的配置)
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
<!--讀取properties配置檔案 -->
<context:property-placeholder
location="classpath:db.properties" />
<!--配置資料源 -->
<bean id="dataSource"
class="org.apache.commons.dbcp2.BasicDataSource">
<!--資料庫驅動 -->
<property name="driverClassName" value="${jdbc.driver}" />
<!--連接配接資料庫的url -->
<property name="url" value="${jdbc.url}" />
<!-- 連接配接資料庫的使用者名 -->
<property name="username" value="${jdbc.username}" />
<!--連接配接資料庫的密碼 -->
<property name="password" value="${jdbc.password}" />
<!--最大連接配接數 -->
<property name="maxTotal" value="${jdbc.maxTotal}" />
<!--最大空閑數 -->
<property name="maxIdle" value="${jdbc.maxIdle}" />
<!--初始化連接配接數 -->
<property name="initialSize" value="${jdbc.initialSize}" />
</bean>
<!--事務管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!--開啟基于注解的事務管理 -->
<tx:annotation-driven
transaction-manager="transactionManager" />
<!--配置mybatis工廠 SqlSessionFactory -->
<bean id="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean">
<!--資料源 -->
<property name="dataSource" ref="dataSource" />
<!--mapper檔案所在的位置 -->
<property name="configLocation"
value="classpath:mybatis-config.xml" />
</bean>
<!--配置mapper掃描器 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.ssm.dao" />
</bean>
<!--開啟自動掃描,将加注解的bean放入容器 -->
<context:component-scan
base-package="com.ssm.service">
<!--排除controller重複掃描 -->
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>
</beans>
>編寫mybatis-config.xml配置檔案(主要就是mapper接口掃描器)
<?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="com.ssm.po"/>
</typeAliases>
</configuration>
>編寫springmvc-config.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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!--配置包掃描 -->
<context:component-scan
base-package="com.ssm.controller" />
<!--加載注解驅動 -->
<mvc:annotation-driven />
<!--視圖解析器 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 字首 -->
<property name="prefix" value="/WEB-INF/jsp/" />
<!-- 字尾 -->
<property name="suffix" value=".jsp" />
</bean>
</beans>
【步驟四】
>編寫web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:jsp="http://java.sun.com/xml/ns/javaee/jsp" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<!--配置加載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-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-config.xml</param-value>
</init-param>
<!--配置伺服器啟動後立即加載Spring MVC 配置檔案 -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!--/:攔截了所有請求(除了jsp) -->
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--配置編碼過濾器 -->
<filter>
<filter-name>encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
【步驟五】
>整合測試應用如圖所示:
>CustomerController.java
package com.ssm.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.ssm.po.Customer;
import com.ssm.service.CustomerService;
@Controller
public class CustomerController {
@Autowired
private CustomerService customerService;
@RequestMapping("/findCustomerById")
public String findCustomerById(Integer id,Model model) {
Customer customer = customerService.findCustomerById(id);
model.addAttribute(customer);
return "customer";
}
}
>CustomerDao.java
package com.ssm.dao;
import com.ssm.po.Customer;
public interface CustomerDao {
public Customer findCustomerById(Integer id);
}
>CustomerDao.xml
<?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.ssm.dao.CustomerDao">
<!--根據id查詢客戶資訊 -->
<select id="findCustomerById" parameterType="Integer"
resultType="Customer">
select * from t_customer where id = #{id}
</select>
</mapper>
>Customer.java
package com.ssm.po;
public class Customer {
private Integer id;
private String username;
private String jobs;
private String phone;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getJobs() {
return jobs;
}
public void setJobs(String jobs) {
this.jobs = jobs;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
@Override
public String toString() {
return "Customer [id=" + id + ", username=" + username + ", jobs=" + jobs + ", phone=" + phone + "]";
}
}
>CustomerService.java
package com.ssm.service;
import com.ssm.po.Customer;
public interface CustomerService {
public Customer findCustomerById(Integer id);
}
>CustomerServiceImpl.java
package com.ssm.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ssm.dao.CustomerDao;
import com.ssm.po.Customer;
import com.ssm.service.CustomerService;
@Service
@Transactional
public class CustomerServiceImpl implements CustomerService {
//注解注入CustomerDao
@Autowired
private CustomerDao customerDao;
//查詢客戶
public Customer findCustomerById(Integer id) {
return this.customerDao.findCustomerById(id);
}
}
【步驟五】
>建立測試customer.jsp界面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>客戶資訊</title>
</head>
<body>
<table border=1>
<tr>
<td>編号</td>
<td>名稱</td>
<td>職業</td>
<td>電話</td>
</tr>
<tr>
<td>${customer.id }</td>
<td>${customer.username }</td>
<td>${customer.jobs }</td>
<td>${customer.phone }</td>
</tr>
</table>
</body>
</html>
【步驟六】
>測試,運作customer.jsp然後出現如下界面
>接下來我們在浏覽器中輸入http://localhost:8080/SSM/findCustomerById?id=1出現如下所示說明我們整合成功