SSM框架简介
SSM(Spring+SpringMVC+MyBatis)框架由Spring、MyBatis两个开源框架整合而成(SpringMVC是Spring中的部分内容)。
Spring框架
Spring贯穿于项目工程的控制层、业务层和持久层,主要解决的是业务逻辑层和其他各层的松耦合问题。它主要有两大理念:
(1)Spring IoC(控制反转)
IoC其实是一个容器,它会认为一切Java资源都是Java Bean,其目的就是管理这些Bean以及它们之间的依赖关系。容器中资源的添加、修改、删除以及资源间的依赖关系都可通过配置文件来定义完成。这样就不需要自己手动去找资源,只要向容器描述所需资源,IoC就会找到你所需要的资源。
(2)Spring AOP(面向切面编程)
AOP,其实就是把可重用的功能提取出来,然后将这些通用功能在合适的时候插入到应用程序中,比如事务管理、日志记录等。Spring AOP多用于数据库事务的编程。在Spring AOP实现的数据库事务管理中,是以异常作为消息的。只要事务(一组操作,里面包含许多个单一的逻辑)中有一个逻辑没有执行成功,Spring就会接收到异常信息,将数据库的事务回滚,从而保持数据的一致性。
Spring MVC框架
Spring MVC是一个基于MVC的Web层(控制层)框架。它是Spring框架的一个模块,Spring MVC和Spring无需通过中间整合层进行整合。其主要任务是将包含业务数据的模块与显示模块的视图解耦。
MyBatis框架
MyBatis是支持普通 SQL查询,存储过程和高级映射的优秀持久层框架。MyBatis 消除了几乎所有的JDBC代码和参数的手工设置以及结果集的检索。MyBatis 使用简单的 XML或注解用于配置和原始映射,将接口和 Java 的POJO(普通的 Java对象)映射成数据库中的记录。
SSM框架结构图

项目实现功能
在客户端查询数据库中的客户列表信息。
开发环境
- 开发工具:Eclipse
- JDK:JDK-9.0.4
- 数据库:MySQL-8.0.12
- 服务器:Apache-Tomcat-9.0.12
- SSM版本:Spring-4.2.4.RELEASE + MyBatis-3.2.7
1、创建动态web工程
(1)工程目录结构

(2)导入所需jar包

2、持久层实现
customer_manager数据库中customer表的构建:

Customer:
package com.ming.ssm.pojo;
import java.io.Serializable;
/**
* 数据库(customer_manager)中表(customer)所对应的实体类(Customer)
* @author Mr.F
*
*/
public class Customer implements Serializable{
private static final long serialVersionUID = 1L;
private Long c_id;
private String c_name;
private String c_password;
private String c_address;
private String c_phone;
private String c_email;
public Long getC_id() {
return c_id;
}
public void setC_id(Long c_id) {
this.c_id = c_id;
}
public String getC_name() {
return c_name;
}
public void setC_name(String c_name) {
this.c_name = c_name;
}
public String getC_password() {
return c_password;
}
public void setC_password(String c_password) {
this.c_password = c_password;
}
public String getC_address() {
return c_address;
}
public void setC_address(String c_address) {
this.c_address = c_address;
}
public String getC_phone() {
return c_phone;
}
public void setC_phone(String c_phone) {
this.c_phone = c_phone;
}
public String getC_email() {
return c_email;
}
public void setC_email(String c_email) {
this.c_email = c_email;
}
@Override
public String toString() {
return "Customer [c_id=" + c_id + ", c_name=" + c_name + ", c_password=" + c_password + ", c_address="
+ c_address + ", c_phone=" + c_phone + ", c_email=" + c_email + "]";
}
}
CustomerMapper:
package com.ming.ssm.mapper;
import java.util.List;
import com.ming.ssm.pojo.Customer;
/**
* 持久层实现Mybatis框架中的Mapper接口,声名对数据库的操作方法
* @author Mr.F
*
*/
public interface CustomerMapper {
List<Customer> findAllCustomer(); //查询所有客户信息
}
CustomerMapper.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接口对应的映射文件
@author Mr.F
-->
<!--nameapce:名称空间,用于隔离sql语句,必须是所对应Mapper接口的全路径名(包名.接口名)-->
<mapper namespace="com.ming.ssm.mapper.CustomerMapper">
<!-- id:必须是所对应接口中相应的方法名;
resultType:方法返回的结果数据类型。
-->
<select id="findAllCustomer" resultType="Customer">
SELECT
`c_id`,
`c_name`,
`c_password`,
`c_address`,
`c_phone`,
`c_email`
FROM
`customer`
</select>
</mapper>
applicationContext-dao.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- 加载访问数据库的属性文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 配置dbcp数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 配置Session工厂 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 引入数据源 -->
<property name="dataSource" ref="dataSource" />
<!-- 引入mybatis的映射配置文件 -->
<property name="configLocation" value="classpath:SqlMapConfig.xml" />
<!-- 采用自动包扫描的方式来引入实体类 -->
<property name="typeAliasesPackage" value="com.ming.ssm.pojo" />
</bean>
<!-- 采用自动包扫描的方式来注册Mapper -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.ming.ssm.mapper" />
</bean>
</beans>
SqlMapConfig.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>
<!-- 控制台打印sql语句 -->
<settings>
<setting name="logImpl" value="STDOUT_LOGGING" />
</settings>
</configuration>
jdbc.properties:
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost/customer_manager?serverTimezone=CTT
jdbc.username=root
jdbc.password=1314
log4j.properties:
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
log4j.rootLogger= debug, stdout
3、业务层实现
CustomerService:
package com.ming.ssm.service;
import java.util.List;
import com.ming.ssm.pojo.Customer;
/**
* 定义业务层接口
* @author Mr.F
*
*/
public interface CustomerService {
List<Customer> findAllCustomer(); //查询所有客户信息
}
CustomerServiceImpl:
package com.ming.ssm.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ming.ssm.mapper.CustomerMapper;
import com.ming.ssm.pojo.Customer;
import com.ming.ssm.service.CustomerService;
/**
* 实现业务层的CustomerService接口
* @author Mr.F
*
*/
@Service
public class CustomerServiceImpl implements CustomerService{
@Autowired
private CustomerMapper customerMapper;
@Override
public List<Customer> findAllCustomer() {
return customerMapper.findAllCustomer();
}
}
applicationContext-service.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- 扫描包下的注解 -->
<context:component-scan base-package="com.ming.ssm.service"/>
</beans>
applicationContext-trans.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- 配置事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 引入数据源 -->
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 配置通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 传播行为:约束事务行为 -->
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="delete*" 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="query*" propagation="SUPPORTS" read-only="true" />
</tx:attributes>
</tx:advice>
<!-- 配置切面 -->
<aop:config>
<aop:advisor advice-ref="txAdvice"
pointcut="execution(* com.ming.ssm.service.*.*(..))" />
</aop:config>
</beans>
4、控制层实现
CustomerController:
package com.ming.ssm.controller;
import java.util.List;
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.ming.ssm.pojo.Customer;
import com.ming.ssm.service.CustomerService;
@Controller
public class CustomerController {
@Autowired
private CustomerService customerService;
@RequestMapping("findAll")
public String findAll(Model model) {
List<Customer> list = customerService.findAllCustomer();
model.addAttribute("list", list);
return "index";
}
}
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:p="http://www.springframework.org/schema/p"
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.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 配置扫描器:扫描包下类上的注解(有了扫描器,可以不用配置注解驱动)-->
<context:component-scan base-package="com.ming.ssm.controller" />
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
web.xml:
<?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_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>ssm_customer</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:spring/applicationContext-*.xml</param-value>
</context-param>
<!-- 配置spring的监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 解决post乱码问题 -->
<filter>
<filter-name>encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<!-- 设置编码是UTF-8 -->
<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>
<!-- 配置springmvc的核心控制器 -->
<servlet>
<servlet-name>springmvc-web</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 加载springmvc的配置文件 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc-web</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
</web-app>
index.jsp:
<%@ 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>客户管理页面</title>
</head>
<body>
<h2 align="center">客户管理系统</h2>
<form action="${pageContext.request.contextPath }/findAll.action" method="post">
<table border="1px" align="center" width="700px">
<tr>
<td colspan="6" align="right">
<input type="submit" value="查询所有客户"/>
</td>
</tr>
<tr align="center">
<td>客户ID</td>
<td>客户姓名</td>
<td>客户密码</td>
<td>客户地址</td>
<td>客户手机</td>
<td>客户邮箱</td>
</tr>
<c:forEach items="${list}" var="c">
<tr align="center">
<td>${c.c_id}</td>
<td>${c.c_name}</td>
<td>${c.c_password}</td>
<td>${c.c_address }</td>
<td>${c.c_phone }</td>
<td>${c.c_email }</td>
</tr>
</c:forEach>
</table>
</form>
</body>
</html>
5、工程测试
将ssm_customer项目工程发布到本地tomcat服务器上,在浏览器地址栏访问http://localhost:8080/ssm_customer/,其结果如下:

然后点击 “查询所有客户” 按钮,其结果如下:
