SSM整合开发
SpringMVC + Spring + Mybatis
SpringMVC:视图层,界面层,负责接收请求,显示处理结果1
Spring:业务层,管理service,dao,工具类
Mybatis:持久层,访问数据库
工作流程:
用户发起请求--------使用SpringMVC接收--------Spring中的Service对象--------Mybatis处理数据
SSM整合也叫SSI,整合中有2个容器
1. SpringMVC的容器,管理Controller控制器对象
2. Spring容器,管理Service,Dao,工具类对象的
我们要做的就是把使用的对象交给合适的容器创建,管理。把Controller还有web开发的相关对象交给SpringMVC
这些web用的对象写在springmvc的配置文件中
service,dao对象写在spring配置文件中,让spring管理这些对象。
springmvc和spring容器有关系,关系已经确定了
springmvc和spring容器的子容器,类似java中的继承,就可以实现controller使用的service对象
实现步骤:
0. 使用springdb的数据库,表使用student(id,name,email,age)其中id自增
1. 新建maven项目
2. 加入依赖
springmvc、spring、mybatis三个框架的依赖,jackson依赖,mysql驱动,druid连接池
jsp,servlet依赖
3. 写web.xml
①注册DispatchServlet 目的:1. 创建springmvc容器对象,才能创建Controller类对象
2. 创建的是Servlet,才能接收用户的请求
②注册spring的监听器:ContextLoaderListener 目的:创建spring容器,才能创建service,到对象
③注册字符集过滤器,解决post请求的乱码问题
4. 创建包,Controller包,service,dao,实体类包名创建好
5. 写springmvc,spring,mybatis的配置文件
①springmvc的配置文件
②spring的配置文件
③mybatis的主配置文件
④数据库的属性配置文件
6. 写代码,dao接口和mapper文件,service文件
导入依赖
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.11version>
<scope>testscope>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>javax.servlet-apiartifactId>
<version>3.1.0version>
dependency>
<dependency>
<groupId>javax.servlet.jspgroupId>
<artifactId>jsp-apiartifactId>
<version>2.2.1-b03version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>5.3.16version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>5.2.5.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-txartifactId>
<version>5.2.5.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jdbcartifactId>
<version>5.2.5.RELEASEversion>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-coreartifactId>
<version>2.9.8version>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-databindartifactId>
<version>2.9.8version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>3.5.9version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatis-springartifactId>
<version>1.3.1version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.47version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druidartifactId>
<version>1.1.12version>
dependency>
- 导入文件的资源的加载
<build>
<resources>
<resource>
<directory>src/main/javadirectory>
<includes>
<include>**/*.propertiesinclude>
<include>**/*.xmlinclude>
includes>
<filtering>falsefiltering>
resource>
<resource>
<directory>src/main/resourcesdirectory>
<includes>
<include>**/*.propertiesinclude>
<include>**/*.xmlinclude>
includes>
<filtering>falsefiltering>
resource>
resources>
build>
编写Web.xml文件
- 注册中央调度器
<servlet>
<servlet-name>springmvcservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:conf/dispatcherServlet.xmlparam-value>
init-param>
<load-on-startup>1load-on-startup>
servlet>
<servlet-mapping>
<servlet-name>springmvcservlet-name>
<url-pattern>/url-pattern>
servlet-mapping>
- 注册监听器
<context-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:conf/applicationContext.xmlparam-value>
context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
listener>
- 注册字符集过滤器
<filter>
<filter-name>characterEncodingFilterfilter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
<init-param>
<param-name>encodingparam-name>
<param-value>utf-8param-value>
init-param>
<init-param>
<param-name>forceRequestEncodingparam-name>
<param-value>trueparam-value>
init-param>
<init-param>
<param-name>forceResponseEncodingparam-name>
<param-value>trueparam-value>
init-param>
filter>
<filter-mapping>
<filter-name>characterEncodingFilterfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
编写配置文件
springmvc的配置文件
<context:component-scan base-package="com.liar.controller"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/"/>
<property name="suffix" value=".jsp"/>
bean>
<mvc:annotation-driven/>
<mvc:default-servlet-handler/>
数据库连接资源配置文件
jdbc.url=jdbc:mysql://localhost:3306/springdb?characterEncoding=UTF8&useSSL=false
jdbc.username=root
jdbc.password=001204
jdbc.maxActive=20
spring的配置文件
<context:property-placeholder location="classpath:conf/jdbc.properties"/>
<bean id="myDataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="maxActive" value="${jdbc.maxActive}"/>
bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="myDataSource"/>
<property name="configLocation" value="classpath:conf/mybatis.xml"/>
bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<property name="basePackage" value="com.liar.dao"/>
bean>
<context:component-scan base-package="com.liar.service"/>
mybatis的主配置文件
<typeAliases>
<package name="com.liar.entity"/>
typeAliases>
<mappers>
<package name="com.liar.dao"/>
mappers>
写接口和实现类
dao包
- StudentDao接口
public interface StudentDao {
/**
* 添加学生
* @param student
* @return
*/
int insertStudent(Student student);
/**
* 查询学生
* @return
*/
List<Student> selectStudents();
}
- mapper文件
DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.liar.dao.StudentDao">
<select id="selectStudents" resultType="com.liar.entity.Student">
select id, name, email, age
from student
order by id desc
select>
<insert id="insertStudent">
insert into student(name, email, age)
values (#{name}, #{email}, #{age})
insert>
mapper>
service包
- StudentService接口
/**
* 添加学生
* @param student
* @return
*/
int addStudent(Student student);
/**
* 查找学生
* @return
*/
List<Student> findStudents();
- 实现类
package com.liar.service.impl;
import com.liar.dao.StudentDao;
import com.liar.entity.Student;
import com.liar.service.StudentService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
/**
* @author liar
* @date 编写时间: 2022/4/17 18:32
*/
@Service
public class StudentServiceImpl implements StudentService {
/**
* 引用类型的自动注入
*/
@Resource
private StudentDao studentDao;
@Override
public int addStudent(Student student) {
return studentDao.insertStudent(student);
}
@Override
public List<Student> findStudents() {
return studentDao.selectStudents();
}
}
controller包
package com.liar.controller;
import com.liar.entity.Student;
import com.liar.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 liar
* @date 编写时间: 2022/4/17 18:35
*/
@Controller
@RequestMapping("/student")
public class MyController {
@Resource
private StudentService service;
/**注册学生*/
@RequestMapping(value = "/addStudent.do")
public ModelAndView addStudent(Student student){
ModelAndView mv = new ModelAndView();
String message = "注册失败!";
//调用service
int nums = service.addStudent(student);
if(nums > 0){
//注册成功
message = student.getName() + "注册成功!";
}
//添加数据
mv.addObject("message",message);
//指定结果页面
mv.setViewName("result");
return mv;
}
/**学生查询*/
@RequestMapping(value = "/queryStudent.do")
@ResponseBody
public List<Student> queryStudent(Student student){
//参数的检查,简单的数据处理
List<Student> students = service.findStudents();
return students;
}
}
前端
- index主页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<% String basePath = request.getScheme() +"://"
+request.getServerName() + ":" + request.getServerPort() +
request.getContextPath() + "/";
%>
功能入口
SSM整合的例子
注册学生
浏览学生
- 添加学生页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<% String basePath = request.getScheme() +"://"
+request.getServerName() + ":" + request.getServerPort() +
request.getContextPath() + "/";
%>
添加学生
添加学生
- 查询学生页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<% String basePath = request.getScheme() +"://"
+request.getServerName() + ":" + request.getServerPort() +
request.getContextPath() + "/";
%>
查询学生页面
学号
姓名
邮件
年龄
- 结果页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
结果页面
结果页面
注册结果:${message}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)