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

SSM整合,手把手教程,详解思路讲解

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

SSM整合,手把手教程,详解思路讲解

前言

一,工程创建

1.新建Maven项目,项目架构增加web支持

2.Maven本地仓库配置

3.集成Tomcat,搭建web环境

二,导入依赖

三,配置文件编写

1.web环境配置文件

2.spring整合环境配置文件

2.1 controller层(springmvc)----springmvc_controller配置文件

2.2 service层(spring)----application_service配置文件

2.3 dao层(spring整合mybatis)----applicationContext_dao配置文件

四,项目包结构

五,代码实现

1.controller层

DeptController类编写

2.doman层

Dept数据库映射类编写

3.mapper层

DeptMapper代理接口编写

DeptMapper.xml数据库映射文件编写

4.service层

DeptService接口编写

DeptServiceImpl实现类编写

5.最终项目结构

 六,运行测试


前言

SSM(Spring+SpringMVC+MyBatis)框架集由Spring、MyBatis两个开源框架整合而成(SpringMVC是Spring中的部分内容),常作为数据源较简单的web项目的框架。虽然现在后端主流开发API的框架是springboot,但ssm的学习依然重要,也是springboot的基石。内容有点长,耐心看完,看完就会,没有耐心的可以直接向我要源码,讲解非常细致。


很多人说ssm配置起来太麻烦了,不如springboot,看完我这篇,让你在ssm阶段像SpringBoot一样快速搭建,大大提高开发效率

一,工程创建

1.新建Maven项目,项目架构增加web支持

2.Maven本地仓库配置

 配置项目名与项目路径

 这里配置Maven,虽然Idea自带Maven,但尽量用自己的Maven,更好地管理项目的依赖,因为Maven是国外的,下载依赖时速度非常慢,自己maven的设置文件配一个镜像,淘宝或者阿里云的都可以,不知道如何配置的可以看我之前发的博客

 配置好后点击finish,之后项目像这样就表明项目创建成功(生成的maven项目需要手动创建java和resource包)

3.集成Tomcat,搭建web环境

二,导入依赖

约定大于配置!约定大于配置!约定大于配置!

简单来说,如果你所用工具的约定和你的期待相符,就可以省去配置;不符合的话,你就要通过相关的配置来达到你所期待的方式。

在Pom中导入相关的依赖,这些依赖包你管用,不管用再加,hh

只写了依赖,把依赖复制过去就可以,里面的依赖目前缺一不可!!!配置好后记得刷新,确保项目中存在相关依赖

  
    
      junit
      junit
      4.11
      test
    
    
    
      javax.servlet.jsp
      jsp-api
      2.1
    
    
      javax.servlet
      servlet-api
      2.5
    
    
    
      org.springframework
      spring-context
      5.3.16
    
    
    
      org.springframework
      spring-webmvc
      5.3.16
    
    
    
      com.fasterxml.jackson.core
      jackson-databind
      2.13.1
    
    
    
      mysql
      mysql-connector-java
      8.0.28
    
    
    
      org.example
      mybatis
      3.5.7
    
    
    
      org.projectlombok
      lombok
      1.18.22
    
    
    
      org.mybatis
      mybatis-spring
      2.0.6
    
    
    
      com.alibaba
      druid
      1.2.8
    
    
    
      org.springframework
      spring-jdbc
      5.3.16
    
  

三,配置文件编写

先搞定配置,因为,约定大于配置大于编码

先创建spring的几个配置文件

我会以数据库的一个部门表来做测试,所以请先创建一个部门表

DROp TABLE IF EXISTS `dept`;
CREATE TABLE `dept`  (
  `deptno` int(0) NOT NULL DEFAULT 0,
  `dname` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `db_source` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL
) ENGINE = MyISAM AUTO_INCREMENT = 6 CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '部门表' ROW_FORMAT = Dynamic;


INSERT INTO `dept` VALUES (1, '开发部', 'db01');
INSERT INTO `dept` VALUES (2, '人事部', 'db01');
INSERT INTO `dept` VALUES (3, '财务部', 'db01');
INSERT INTO `dept` VALUES (4, '市场部', 'db01');
INSERT INTO `dept` VALUES (5, '运维部', 'db01');

1.web环境配置文件

web.xml中的配置,代码缺一不可!!!




  Archetype Created Web Application

  
  
    contextConfigLocation
    classpath:applicationContext*.xml
  
  
  
    org.springframework.web.context.ContextLoaderListener
  
  
  
    springmvc
    org.springframework.web.servlet.DispatcherServlet
    
      contextConfigLocation
      classpath:springmvc-controller.xml
    
    1
  
  
    springmvc
    /
  
  

2.spring整合环境配置文件

由于springmvc配置文件需要用于当做前端核心控制器,所以不交与spring"扫描"

配置文件中报扫描路径一定要与创建的相关文件路径对应哦

2.1 controller层(springmvc)----springmvc_controller配置文件

    
    
    
    
    
    
    
    
        
        
    


​

2.2 service层(spring)----application_service配置文件

    
    

2.3 dao层(spring整合mybatis)----applicationContext_dao配置文件

里面的数据源改为自己的即可


    
    
        
        
        
        
    

    
    
        
    

    
    
        
    


四,项目包结构

创建对应的项目文件结构

五,代码实现

1.controller层

DeptController类编写
package com.lyj.controller;

import com.lyj.domain.Dept;
import com.lyj.service.DeptService;
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 org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

// @RestController//当这个控制器全是返回json数据,不进行页面跳转时,用这个
@Controller
@RequestMapping("/dept")
public class DeptController {
    @Autowired
    private DeptService deptService;

    @RequestMapping("/selectAll")
    @ResponseBody//此注解用于直接返回json数据,而不是跳转页面
    public List test(){
        List deptList = deptService.selectAll();
        return deptList;
    }

    @RequestMapping("/selectAll1")
    public String test1(Model model){
        List depts = deptService.selectAll();
        model.addAttribute("depts",depts);
        return "dept";//页面跳转,根据springmvc配置的前后缀跳转
    }
}

2.doman层

Dept数据库映射类编写

(此处用了lombok的@Data注解,省去生成get和set方法,注意使用lombok需要安装lombok插件)

package com.lyj.domain;

import lombok.Data;

@Data
public class Dept {
    private int deptno;
    private String dname;
    private String db_source;
}

3.mapper层

DeptMapper代理接口编写
package com.lyj.mapper;

import com.lyj.domain.Dept;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;
@Mapper
public interface DeptMapper {
    public List selectAll();
}

DeptMapper.xml数据库映射文件编写



    


4.service层

DeptService接口编写
package com.lyj.service;

import com.lyj.domain.Dept;

import java.util.List;

public interface DeptService {
    public List selectAll();
}

DeptServiceImpl实现类编写
package com.lyj.service.impl;

import com.lyj.domain.Dept;
import com.lyj.mapper.DeptMapper;
import com.lyj.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DeptServiceImpl implements DeptService {
    @Autowired
    private DeptMapper deptMapper;

    @Override
    public List selectAll() {
        return deptMapper.selectAll();
    }
}

5.最终项目结构

 六,运行测试

项目启动,跳到浏览器输入请求路径localhost:8080/dept/selectAll

总结

       以上就是今天要讲的内容,都是手撕的,创作不易,本文介绍了ssm整合的步骤,能使我们快速便捷地处理数据和页面跳转。搭建好之后,即可开始我们的项目业务处理,肆意的发挥吧!

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

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

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