目录
一、整体思路
SSM: SpringMVC + Spring + MyBatis.
SpringMVC:视图层,界面层,负责接收请求,显示处理结果的。
Spring:业务层,管理service,dao,工具类对象的。
MyBatis:持久层, 访问数据库的
用户发起请求--SpringMVC接收--Spring中的Service对象--MyBatis处理数据
SSM整合也叫做SSI (IBatis也就是mybatis的前身), 整合中有容器。
- 第一个容器SpringMVC容器, 管理Controller控制器对象的。
- 第二个容器Spring容器,管理Service,Dao,工具类对象的
我们要做的把使用的对象交给合适的容器创建,管理。 把Controller还有web开发的相关对象交给springmvc容器, 这些web用的对象写在springmvc配置文件中
service,dao对象定义在spring的配置文件中,让spring管理这些对象
springmvc容器和spring容器是有关系的,关系已经确定好了
springmvc容器是spring容器的子容器, 类似java中的继承。 子可以访问父的内容
在子容器中的Controller可以访问父容器中的Service对象, 就可以实现controller使用service对象
实现步骤:
- 使用ssm的mysql库, 表使用student(id auto_increment, name, age)
- 新建maven web项目
- 加入依赖springmvc,spring,mybatis三个框架的依赖,jackson依赖,mysql驱动,druid连接池jsp,servlet依赖
- 写web.xml注册DispatcherServlet ,目的:1.创建springmvc容器对象,才能创建Controller类对象。2.创建的是Servlet,才能接受用户的请求。注册spring的监听器:ContextLoaderListener,目的: 创建spring的容器对象,才能创建service,dao等对象。注册字符集过滤器,解决post请求乱码的问题
- 创建包, Controller包, service ,dao,实体类包名创建好
- 写springmvc,spring,mybatis的配置文件springmvc配置文件spring配置文件mybatis主配置文件数据库的属性配置文件
- 写代码, dao接口和mapper文件, service和实现类,controller, 实体类。
- 写jsp页面
二、SSM整合注解开发
需求:完成学生的注册和信息浏览
1. 建立Student
2. 建立Web工程
通过maven
3. maven依赖
<?xml version="1.0" encoding="UTF-8"?>4.0.0com.md 07-ssm 1.0-SNAPSHOTwarUTF-81.81.8junit junit 4.11testjavax.servlet javax.servlet-api 3.1.0providedjavax.servlet.jsp jsp-api 2.2.1-b03providedorg.springframework spring-webmvc 5.2.5.RELEASEorg.springframework spring-tx 5.2.5.RELEASEorg.springframework spring-jdbc 5.2.5.RELEASEcom.fasterxml.jackson.core jackson-core 2.9.0com.fasterxml.jackson.core jackson-databind 2.9.0org.mybatis mybatis-spring 1.3.1org.mybatis mybatis 3.5.1mysql mysql-connector-java 5.1.9com.alibaba druid 1.1.12src/main/java**/*.properties**/*.xmlfalse
4. 定义程序结构
5. 编写配置文件
jdbc属性配置文件jdbc.properties
jdbc.url=jdbc:mysql://localhost:3306/ssmjdbc.username=rootjdbc.password=123456jdbc.maxActive=20
Spring配置文件applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
SpringMVC的配置文件,dispatcherServlet.xml
<?xml version="1.0" encoding="UTF-8"?>
mybatis的配置文件 , mybatis.xml
<?xml version="1.0" encoding="UTF-8" ?>
6. 定义web.xml
由于自动生成的是这样的,版本太低,需要换成现在高的版本,直接把之前写好的复制过来就行
换成这样
- 注册 ContextLoaderListener
- 注册 DisatcherServlet
- 注册字符集过滤器
- 同时创建 Spring 的配置文件和 SpringMVC 的配置文件(上面已经创建好了)
<?xml version="1.0" encoding="UTF-8"?>springmvcorg.springframework.web.servlet.DispatcherServletcontextConfigLocationclasspath:conf/dispatcherServlet.xml1springmvc*.docontextConfigLocationclasspath:conf/applicationContext.xmlorg.springframework.web.context.ContextLoaderListenercharacterEncodingFilterorg.springframework.web.filter.CharacterEncodingFilterencodingutf-8forceRequestEncodingtrueforceResponseEncodingtruecharacterEncodingFilter/*
7. 实体类
package com.md.domain;/** * @author MD * @create 2020-08-13 20:21 */public class Student { private Integer id; private String name; private Integer age; public Student() { } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "Student{" + "id=" + id + ", name='" + name + ''' + ", age=" + age + '}'; }}
8. Dao接口和映射文件
都在是dao包下,且文件名相同
package com.md.dao;import com.md.domain.Student;import java.util.List;/** * @author MD * @create 2020-08-13 20:22 */public interface StudentDao { int insertStudent(Student student); List selectStudents();}
StudentDao.xml
<?xml version="1.0" encoding="UTF-8" ?> insert into student(name,age) values(#{name} , #{age}) select id, name , age from student
9. Service接口和实现类
package com.md.service;import com.md.domain.Student;import java.util.List;/** * @author MD * @create 2020-08-13 20:31 */public interface StudentService { int addStudent(Student student); List findStudents();}
实现类
package com.md.service.impl;import com.md.dao.StudentDao;import com.md.domain.Student;import com.md.service.StudentService;import org.springframework.stereotype.Service;import javax.annotation.Resource;import java.util.List;/** * @author MD * @create 2020-08-13 20:32 */@Servicepublic class StudentServiceImpl implements StudentService { // 引用数据类型的自动注入@Resource、@Autowired @Resource private StudentDao studentDao; @Override public int addStudent(Student student) { int nums = studentDao.insertStudent(student); return nums; } @Override public List findStudents() { return studentDao.selectStudents(); }}
10. 处理器
在controller包下
package com.md.controller;import com.md.domain.Student;import com.md.service.StudentService;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.servlet.ModelAndView;import javax.annotation.Resource;import java.util.List;/** * @author MD * @create 2020-08-13 20:37 */@Controller@RequestMapping("/student")public class StudentController { // 自动注入 @Resource private StudentService service; // 注册学生 @RequestMapping("/addStudent.do") public ModelAndView addStudent(Student student){ ModelAndView mv = new ModelAndView(); String tips = "注册失败"; // 调用service处理student int nums = service.addStudent(student); if (nums > 0){ tips = "注册成功"; } // 添加数据,指定页面 mv.addObject("tips",tips); mv.setViewName("result"); return mv; } // 处理查询,响应ajax @RequestMapping("/queryStudent.do") @ResponseBody public List queryStudent(){ List students = service.findStudents(); // 会被框架转为json的数组 return students; }}
11. 首页
index.jsp
功能入口
SSM整合
学生注册 学生信息
12. 注册页面
addStudent.jsp
学生注册
姓名: 年龄:
13. 注册结果
在/WEB-INF/jsp/result.jsp
Title
注册结果:${tips}
14. 浏览页面
listStudent.jsp
学生信息
学号 姓名 年龄
此时再配置Tomcat运行即可,之后再添加新的功能