SSM(Spring+SpringMVC+MyBatis)框架集由Spring、MyBatis两个开源框架整合而(SpringMVC是Spring中的部分内容),常作为数据源较简单的web项目的框架。
一、 环境介绍
先创建一个web工程。这里使用SSM最终完成一个员工信息的增删改查。
项目配置
- Server.服务器版本: Apache Tomcat/8.5.86
- maven:3.8
- jdk:1.8
- SSM框架:spring+mybatis+springmvc
springMVC:视图层,界面层,负责接收请求,显示处理结果;
spring:业务层,管理service、dao,工具类对象的;
mybatis:持久层,访问数据库的;
用户发起请求———springmvc接收—-spring中的service对象—–mybatis处理数据
实现步骤:
(1)创建maven web项目
(2)加入依赖
springmvc,spring,mybatis三个框架的依赖,Jackson依赖,mysql驱动,druid连接池,thymeleaf-spring5整合依赖,servlet依赖等
(3)写web.xml文件
1、注册DispatcherServlet,目的:创建springmvc的容器对象,才能创建controller类对象;创建的是servlet,才能接收用户的请求;
2、注册spring的监听器:contextloaderlistener,目的:创建spring的容器对象,才能创建service,dao等对象
3、注册字符集过滤器,解决post请求乱码的问题
(4)、创建包 : controller包,service,dao,实体类包
(5)写springmvc、spring、mybatis的配置文件
resources:
springmvc配置文件
spring配置文件
mybatis配置文件
数据库的属性配置文件
(6)、写java代码
(7)、写前端页面
二、XML 方式整合ssm框架
2.1 先创建一个maven项目的web工程。
(1)在idea中,依次单击 File -> New -> Project -> New Project
新建maven项目,点击”Create”
(2)项目所有目录如图所示
2.2 添加maven依赖
(1)修改maven依赖,添加依赖包
pom.xml文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.example</groupId><artifactId>ssm</artifactId><version>1.0-SNAPSHOT</version><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><spring.version>5.3.1</spring.version></properties><packaging>war</packaging><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-aspects</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.5.7</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId><version>2.0.6</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.0.9</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.16</version></dependency><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.17</version></dependency><dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper</artifactId><version>5.2.0</version></dependency><dependency><groupId>ch.qos.logback</groupId><artifactId>logback-classic</artifactId><version>1.2.3</version></dependency><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>3.1.0</version><scope>provided</scope></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.12.1</version></dependency><dependency><groupId>commons-fileupload</groupId><artifactId>commons-fileupload</artifactId><version>1.3.1</version></dependency><dependency><groupId>org.thymeleaf</groupId><artifactId>thymeleaf-spring5</artifactId><version>3.0.12.RELEASE</version></dependency></dependencies></project>
各个依赖的详细功能已经写在了下面:
包 | javax.servlet-api |
spring-webmvc | 间接依赖 springioc的四个核心jar包 + spring-web包 |
jackson-databind | springmvc 在异步请求处理JSON数据的时候会使用 |
thymeleaf-spring5 | springmvc + thymeleaf 整合 |
spring-jdbc | 只使用里面的事务支持,其会为我们提供已经编写好的事务管理的切面类 |
spring-aspects | AOP功能的支持 |
mybatis | 持久层框架 |
pagehelper | 为mybatis 提供分页功能的插件 |
mybatis-spring | mybatis 和 spring 的整合包 : 其提供了一个SqlSessionFactoryBean,用于创建SqlSession |
druid | 德鲁伊连接池,提供数据源 |
mysql-connector-java | MYSQL驱动 |
spring-test | Spring提供的测试功能,可以直接在测试代码中注入容器当中的Bean |
log4j (druid/mybatis 选用) | 日志 (经验证发现 , Druid / mybatis 内部使用 log4j,而且是 provide的间接依赖,可能是检测到有包就用,当你在系统里面依赖 log4j ,不提供log4j的配置文件,则会发生提示 :log4j:WARN No appenders could be found for logger (com.alibaba.druid.pool.DruidDataSource). log4j:WARN Please initialize the log4j system properly.) 因此:要么就依赖 log4j.jar 且 配置 log4j.properties/log4j.xml 给 Druid 使用,要么就不提供依赖,druid也不会检测到系统有jar包,不会给出 WARN |
logback-classic(Spring /thymeleaf 整合包使用) | slf4j的日志实现 1. thymeleaf 使用了 sl4j 的依赖,因此需要提高 logback 实现,并且配置logback; 2. spring-context 会 间接依赖spring.jcl 包判断日志实现的时候会默认为 slfj 的具体实现(发现机制),因此可以配置个 slf4j 的具体实现使用Spring的日志。若系统内部没有 slf4j ,会搜寻 log4j2,还是没有则默认使用 JUL 日志输出。 |
javax.servlet-api | MVC的Controller中可能会使用到Servlet的API |
commons-fileupload | SpringMVC底层支持文件上传需要jar包 |
2.2 新建webapp ,配置tomcat8.5
(1) 在idea中,依次单击 Project Setting -> 选择 Modules -> ssm项目下选择Web
(2) 勾选Web选择➕ 号,添加 web.xml文件
(3) 修改当前的web路径 :添加 /src/main/webapp/WEB-INF/web.xml
(4)添加目录 ssm/src/main/webapp/WEB-INF/web.xml
如下:
(5)点击【apply】
(5)配置tomcat 8
2.3 配置 web.xml
web.xml配置四个东西 :
- 核心控制器 DispatcherServlet ;
- ContextLoaderListener ;
- 编码过滤器(放过滤器中最前);
- 请发方法处理器 HiddenMethodFilter
web.xml文件
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"><filter><filter-name>CharacterEncodingFilter</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><init-param><param-name>forceEncoding</param-name><param-value>true</param-value></init-param></filter><filter-mapping><filter-name>CharacterEncodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping><filter><filter-name>HiddenHttpMethodFilter</filter-name><filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class></filter><filter-mapping><filter-name>HiddenHttpMethodFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping><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><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>SpringMVC</servlet-name><url-pattern>/</url-pattern></servlet-mapping><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring-config.xml</param-value></context-param></web-app>
2.4 配置SpringMVC
- Controller包的扫描 (必选)
- thymeleaf 的视图解析器(必选)
- 静态资源处理 (必选)
- 视图控制器(可选)
- 注解驱动(必选)
(1) 新建 springmvc-config.xml
(2)配置 springmvc-config.xml 文件
springmvc-config.xml 文件
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd"><context:component-scan base-package="org.example.controller"></context:component-scan><bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver"><property name="order" value="1"/><property name="characterEncoding" value="UTF-8"/><property name="templateEngine"><bean class="org.thymeleaf.spring5.SpringTemplateEngine"><property name="templateResolver"><bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver"><property name="prefix" value="/WEB-INF/templates/"/><property name="suffix" value=".html"/><property name="templateMode" value="HTML5"/><property name="characterEncoding" value="UTF-8" /></bean></property></bean></property></bean><mvc:default-servlet-handler /><mvc:annotation-driven /><mvc:view-controller path="/" view-name="index"></mvc:view-controller><bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"></bean></beans>
测试:直接访问视图控制器配置的首页看是否正常即可。
2.5 配置Spring
名字自取也可以,后面 web.xml 文件是可以修改的。
(1) 新建 jdbc.properties文件
jdbc.properties文件
jdbc.driver=com.mysql.cj.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/ssm" />jdbc.url=jdbc:mysql://localhost:3306/ssm?useUnicode=true&characterEncoding=UTF-8
或者修改bean配置
<bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="driverClassName" value="com.mysql.jdbc.Driver"/><property name="url" value="jdbc:mysql://127.0.0.1:3306/ssm?useUnicode=true&characterEncoding=utf-8"/><property name="username" value="root"/><property name="password" value="root"/></bean>
(2) 新建 spring-config.xml文件
配置分为三大部分:
- 包扫描、排除@Controller
- 事务相关
- 事务管理器
- 事务注解驱动
3.MyBaits需要的配置:
- 引入 jdbc.properties
- druid 数据源(需要1)
- SqlSessionFactoryBean 注册到IOC(需要2)
- 产生的mapper代理类注册到IOC → 即配置 MapperScannerConfigurer
spring-config.xml文件
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"><context:component-scan base-package="org.example"><context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/></context:component-scan><context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder><bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"><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><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"></property></bean><tx:annotation-driven transaction-manager="transactionManager" /><bean class="org.mybatis.spring.SqlSessionFactoryBean"><property name="configLocation" value="classpath:mybatis-config.xml"></property><property name="dataSource" ref="dataSource"></property><property name="typeAliasesPackage" value="org.example.pojo"></property><property name="mapperLocations" value="classpath:mappers/*.xml"></property></bean><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="org.example.mapper"></property></bean></beans>
2.6 配置mybatis
- resources/mybatis-config.xml 中编写配置,其中关于数据源的配置交给
2.5
在Spring中进行配置,向SpringIOC容器中注入 SqlSessionFactory 对象。 - resources/ 下创建同包的路径的mappers目录,并编写mapper.xml 。
- 配置分页插件
mybatis-config.xml文件
<!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration><settings><setting name="mapUnderscoreToCamelCase" value="true"/></settings><plugins><plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin></plugins></configuration>
2.7 log4j日志文件(druid/mybatis 非必须)
非必须,但还是配下好。在 resources/ 下编写 log4j.xml,创建就能生效了。
‼️ mybatis 的日志也可以换成如 slf4j,如果使用直接看2.8即可啦,2.7 跳过即可。
log4j.xml文件
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"><log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"><appender name="STDOUT" class="org.apache.log4j.ConsoleAppender"><param name="Encoding" value="UTF-8"/><layout class="org.apache.log4j.PatternLayout"><param name="ConversionPattern" value="[log4j-output]%-5d %d{MM-dd HH:mm:ss,SSS} %m (%F:%L) \n"/></layout></appender><logger name="java.sql"><level value="debug"/></logger><logger name="org.apache.ibatis" ><level value="info"/></logger><root><level value="debug"/><appender-ref ref="STDOUT"/></root></log4j:configuration>
2.8 功能
只简单实现两个功能即可。
功能 | RESTful API | 数据 |
分页查询员工信息 | /employee/page/1 | employee/page/{页码} |
根据id查询某个员工信息 | /employee/1 |
① 创建表
-- auto-generated definitioncreate table t_emp(emp_id int auto_incrementprimary key,emp_name varchar(20) null,ageint null,gender charnull,email varchar(50) null)comment '员工表' auto_increment = 34;
② 创建pojo
package org.example.pojo;/** * 员工类 */public class Employee {private Integer empId;private String empName;private Integer age;private String gender;private String email;public Employee() {}public Employee(Integer empId, String empName, Integer age, String gender, String email) {this.empId = empId;this.empName = empName;this.age = age;this.gender = gender;this.email = email;}public Integer getEmpId() {return empId;}public void setEmpId(Integer empId) {this.empId = empId;}public String getEmpName() {return empName;}public void setEmpName(String empName) {this.empName = empName;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}public String getGender() {return gender;}public void setGender(String gender) {this.gender = gender;}public String getEmail() {return email;}public void setEmail(String email) {this.email = email;}@Overridepublic String toString() {return "Employee{" +"empId=" + empId +", empName='" + empName + '\'' +", age=" + age +", gender='" + gender + '\'' +", email='" + email + '\'' +'}';}}
③ 创建controller
package org.example.controller;import org.example.pojo.Employee;import org.example.service.EmployeeService;import com.github.pagehelper.PageInfo;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.ResponseBody;import java.util.List;@Controllerpublic class EmployeeController {@Autowiredprivate EmployeeService employeeService;@RequestMapping(value = "/employee/page/{pageNum}", method = RequestMethod.GET)public PageInfo<Employee> getEmployeePage(@PathVariable("pageNum") Integer pageNum, Model model){//获取员工的分页信息PageInfo<Employee> page = employeeService.getEmployeePage(pageNum);return page;}@RequestMapping(value = "/getAllEmployee", method = RequestMethod.GET)@ResponseBodypublic List<Employee> getAllEmployee(){//查询所有的员工信息List<Employee> list = employeeService.getAllEmployee();return list;}}
④ 创建service
package org.example.service;import org.example.pojo.Employee;import com.github.pagehelper.PageInfo;import java.util.List;/** * Service */public interface EmployeeService {/** * 查询所有的员工信息 * * @return */List<Employee> getAllEmployee();/** * 获取员工的分页信息 * * @param pageNum * @return */PageInfo<Employee> getEmployeePage(Integer pageNum);}
⑤ 创建impl
package org.example.service.impl;import org.example.mapper.EmployeeMapper;import org.example.pojo.Employee;import org.example.service.EmployeeService;import com.github.pagehelper.PageHelper;import com.github.pagehelper.PageInfo;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import org.springframework.transaction.annotation.Transactional;import java.util.List;@Service@Transactionalpublic class EmployeeServiceImpl implements EmployeeService {@Autowiredprivate EmployeeMapper employeeMapper;@Overridepublic List<Employee> getAllEmployee() {return employeeMapper.getAllEmployee();}@Overridepublic PageInfo<Employee> getEmployeePage(Integer pageNum) {//开启分页功能PageHelper.startPage(pageNum, 4);//查询所有的员工信息List<Employee> list = employeeMapper.getAllEmployee();//获取分页相关数据PageInfo<Employee> page = new PageInfo<>(list, 5);return page;}}
⑥ 创建mapper
package org.example.mapper;import org.example.pojo.Employee;import java.util.List;public interface EmployeeMapper {/** * 查询所有的员工信息 * @return */List<Employee> getAllEmployee();}
⑦ 创建mappers / mapper.xml
(1)resources/ 下创建同包的路径的mappers目录,并编写mapper.xml
mapper.xml
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="org.example.mapper.EmployeeMapper"><select id="getAllEmployee" resultType="Employee">select * from t_emp</select></mapper>
(2)设置映射文件的路径,只有映射文件的包和mapper接口的包不一致时需要设置
<bean class="org.mybatis.spring.SqlSessionFactoryBean"><property name="configLocation" value="classpath:mybatis-config.xml"></property><property name="dataSource" ref="dataSource"></property><property name="typeAliasesPackage" value="org.example.pojo"></property><property name="mapperLocations" value="classpath:mappers/*.xml"></property></bean>
2.9 注意点
使用上述方式快速搭建SSM环境的时候,需要注意扫描包路径、别名路径等都需要改,自行根据下方列出的地方进行检查。
- MyBatis :别名包路径。
- Spring:包扫描路径(注意排除 Controller)、MapperScannerConfigurer中指定Mapper的扫描路径。
- SpringMVC:Controller包扫描、视图解析器的prefix。
2.10 Tomcat启动运行项目
数据库数据:
请求路径 : http://localhost:8080/ssm/getAllEmployee
测试:
[{"empId":1001,"empName":"王铁柱","age":23,"gender":"男","email":"10086001@qq.com"},{"empId":1002,"empName":"田二妞","age":25,"gender":"女","email":"6666@163.com"}]