栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SSM整合

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SSM整合

SSM:SpringMVC + Spring + MyBatis。

SpringMVC :视图层,界面层,负责接收请求,显示处理结果的。
Spring:业务层,管理service,dao,工具类对象的。
Mybatis:持久层,访问数据库的

用户发起请求 --> SpringMVC接收 --> Spring中的Service对象 --> Mybatis处理数据

SSM整合也叫作SSI(IBatis 也就是 Mybatis的前身),整合有容器。
        1.第一个容器SpringMVC容器,管理Controller控制器对象的。
        2.第二个容器Spring容器,管理Service,Dao,工具类对象的。

        我们需要把使用的对象给合适的容器创建,管理,如:把Controller还有web开发的相关对象交给springmvc容器,这些web用的对象写在springmvc配置文件中,service,dao对象定义在spring的配置文件中,让spring管理这些对象。

        SpringMVC容器和Spring容器是有关系的,关系已经确定好了,SpringMVC容器是Spring容器的子容器,类似Java中的继承。(子可以访问父的内容),在子容器中的Controller可以访问父容器中的Service对象,就可以实现controller使用service对象。

实现步骤:(只涉及到简单的整合,用于理解SSM结构)
1、使用mb的mysql库,表student,数据库mysql8.0

注意:id设置为自增。

 2、新建maven web项目,加入依赖
  springmvc,spring,mybatis三个框架的依赖,jackson依赖,mysql驱动,druid连接池,jsp,servlet依赖




  4.0.0

  com.km
  ssm
  1.0-SNAPSHOT
  war

  
    UTF-8
    1.8
    1.8
  

  
    
    
      junit
      junit
      4.11
      test
    
    
    
      javax.servlet
      javax.servlet-api
      3.1.0
      provided
    
    
    
      javax.servlet.jsp
      jsp-api
      2.2.1-b03
      provided
    
    
    
      org.springframework
      spring-webmvc
      5.2.5.RELEASE
    
    
    
      org.springframework
      spring-tx
      5.2.5.RELEASE
    
    
      org.springframework
      spring-jdbc
      5.2.5.RELEASE
    
    
    
      com.fasterxml.jackson.core
      jackson-core
      2.9.0
    
    
      com.fasterxml.jackson.core
      jackson-databind
      2.9.0
    
    
    
      org.mybatis
      mybatis-spring
      1.3.1
    
    
    
      org.mybatis
      mybatis
      3.5.1
    
    
    
      mysql
      mysql-connector-java
      8.0.22
    
    
    
      com.alibaba
      druid
      1.1.12
    
  

  
    
      
        src/main/java
        
          ***.xml
        
        false
      
    
  

3、创建包,Controller包,service,dao,实体类包名创建好,项目结构

 4、resources资源包下,创建文件夹conf,写springmvc,spring,mybatis的配置文件
    1)springmvc配置文件,dispatcherServlet.xml




    
    

    
        
        
    

    
    

    2)spring配置文件,applicationContext.xml




    
    

    
    
        
        
        
    

    
    
        
        
    

    
    
        
        
    

    
    

    

    3)mybatis主配置文件,mybatis.xml





    
    
        
        
    

    
    
        
        
    

    4)数据库的属性配置文件,jdbc.properties

jdbc.url=jdbc:mysql://localhost:3306/mb?useUnicode=true&&characterEncoding=utf8&&serverTimezone=UTC&&useSSL=false
jdbc.user=root
jdbc.password=root

5、写web.xml




  1)注册DispatcherServlet,目的:

        1.创建springmvc容器对象,才能创建Controller类对象。
        2.创建的是Servlet,才能接收用户的请求。

    
  
    myweb
    org.springframework.web.servlet.DispatcherServlet
    
      contextConfigLocation
      classpath:conf/dispatcherServlet.xml
    
    1
  
  
    myweb
    *.do
  

  2)注册spring的监听器:ContextLoaderListener,目的:创建spring的容器对象,才能创建service,dao等对象。

  
  
    contextConfigLocation
    classpath:conf/applicationContext.xml
  
  
    org.springframework.web.context.ContextLoaderListener
  

  3)注册字符集过滤器,解决post请求乱码的问题。

  
  
    characterEncodingFilter
    org.springframework.web.filter.CharacterEncodingFilter
    
      encoding
      utf-8
    
    
      forceRequestEncoding
      true
    
    
      forceResponseEncoding
      true
    
  
  
    characterEncodingFilter
    /*
  

6、写代码,dao接口和mapper文件,service,controller,实体类。

        根据数据库,创建实体类

package com.km.entity;

public class Student {
    private Integer id;
    private String name;
    private Integer age;
    // set  get方法
}

        dao层,dao接口和mapper文件

package com.km.dao;

import com.km.entity.Student;

import java.util.List;

public interface StudentDao {
    int insertStudent(Student student);
    List selectStudent();
}




    
    
        insert into student (name,age) values (#{name},#{age});
    

        service层,service接口及实现类

package com.km.service;

import com.km.entity.Student;
import java.util.List;

public interface StudentService {
    int addStudent(Student student);
    List findStudent();
}
package com.km.service.impl;

import com.km.dao.StudentDao;
import com.km.entity.Student;
import com.km.service.StudentService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {
    // 引用类型自动注入@Autowired , @Resource
    @Resource
    private StudentDao studentDao;
    @Override
    public int addStudent(Student student) {
        int nums = studentDao.insertStudent(student);
        return nums;
    }
    @Override
    public List findStudent() {
        return studentDao.selectStudent();
    }
}

        controller层

package com.km.controller;

import com.km.entity.Student;
import com.km.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;

@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 result = service.addStudent(student);
        if (result > 0){
            // 注册成功
            tips = "学生【" + student.getName() + "】注册成功";
        }
        mv.addObject("tips",tips); // 添加数据
        mv.setViewName("result"); // 指定结果页面
        return mv;
    }

    // 处理查询,响应ajax
    @RequestMapping("/queryStudent.do")
    @ResponseBody
    public List queryStudent(){
        // 参数查询,简单的数据处理
        List students = service.findStudent();
        return students;
    }
}

7、写jsp页面,/webapp目录结构,用到了JQuery库,ajax

 index.jsp页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    功能入口
    


    
        

SSM整合的例子

注册学生
浏览学生

addStudent.jsp页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    注册学生
    


    
        
姓名:
年龄:
     

listStudent.jsp页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    查询学生
    
    
    


    
        
学号 姓名 年龄

result.jsp添加学生结果页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    Title


    result.jsp 结果页面,注册结果:${tips}

测试输入url

http://localhost:8080/myweb/

注册学生,注册成功后回跳转到result.jsp页面。

浏览学生,会跳转到listStudent.jsp页面

 点击查询数据按钮,会加载ajax,显示学生信息。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/356020.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号