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

SSM整合

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

SSM整合

本次整合是基于Maven整合项目,首先放出整合后的文件目录:

整合的总体思路:先启动SpringMVC,然后在MVC中配置引入spring的配置文件从而整合spring,最后在spring配置文件中引入配置mybatis的sqlsessionfactorybean来整合启动mybatis   正式开始  1.引入依赖


  4.0.0

  org.example
  module2
  1.0-SNAPSHOT
  war

  
    11
    11
  
  
    
    
    
      org.springframework
      spring-webmvc
      5.3.1
    
    
    
      org.springframework
      spring-aop
      5.3.1
    
    
    
      org.springframework
      spring-web
      5.3.1
    
    

    
      org.springframework
      spring-tx
      5.2.6.RELEASE
    
    

    
      org.springframework
      spring-jdbc
      5.3.1
    

    
    

    
      javax.servlet
      javax.servlet-api
      3.1.0
      provided
    

    
      javax.servlet.jsp
      javax.servlet.jsp-api
      2.3.3
    
    
    
      mysql
      mysql-connector-java
      8.0.25
    

    
    
      org.thymeleaf
      thymeleaf-spring5
      3.0.12.RELEASE
    

    
      org.springframework
      spring-context
      5.2.5.RELEASE
    

    
    
    
      junit
      junit
      4.13.2
      test
    

    
    
      com.fasterxml.jackson.core
      jackson-databind
      2.13.0
    

    
      org.projectlombok
      lombok
      1.18.22
      compile
    

    
    
      org.mybatis
      mybatis
      3.5.7
    
    
    
      log4j
      log4j
      1.2.17
    
    
      org.slf4j
      slf4j-api
      1.7.32
    
    
      org.slf4j
      slf4j-log4j12
      1.7.32
      test
    
    
      org.slf4j
      slf4j-simple
      1.7.32
      test
    


    
      com.alibaba
      druid
      1.2.8
    
    
    
    
      org.mybatis
      mybatis-spring
      2.0.6
    

  


2.正常流程启动MVC

web.xml最终的配置文件如下,正常启动MVC时就是在下方的配置文件去掉前两个






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







    
        DispatcherServlet
        org.springframework.web.servlet.DispatcherServlet
        
            contextConfigLocation
            classpath:SpringMVC.xml
        
        1
    
    
        DispatcherServlet
        /
    


    
        characterEncodingFilter
        org.springframework.web.filter.CharacterEncodingFilter
        
            encoding
            UTF-8
        
        
            forceResponseEncoding
            true
        
    
    
        characterEncodingFilter
        

@Controller
@RequestMapping("/Employee")
public class EmployeeController {

    @Autowired
    private EmployeeServiceImpl employeeService;

    @RequestMapping("/")
    public String index() {
        System.out.println("这是控制层显示首页的方法");
        return "index";
    }

   @RequestMapping("/all")
    public ModelAndView findAllEmployees() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("list");
        List employeeList = employeeService.showAllEmployees();
        modelAndView.addObject("employeeList",employeeList);
        System.out.println("这是控制层查询所有账户的方法,查询到的内容为:"+employeeList);
        return modelAndView;
    }
}

FirstPageController

package cn.ideal.controller;

import cn.ideal.service.EmployeeServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;


@Controller
public class FirstPageController {

    @Autowired
private EmployeeServiceImpl employeeService;

    @RequestMapping("/")
    public String index(){
        System.out.println("进入首页控制器");
        return "index";
    }
}

EmployeeDao

package cn.ideal.dao;

import cn.ideal.domain.Employee;
import org.springframework.stereotype.Repository;

import java.util.List;


@Repository(value = "employeeDao")
public interface EmployeeDao {
    public List findAllEmployees();
}

Employee

使用了lombok简化了实体类创建

package cn.ideal.domain;

import lombok.Data;


@Data
public class Employee {
    private int id;
    private String name;
    private String gender;
    private String email;
    private  int dept_id;

}

EmployeeService

package cn.ideal.service;

import cn.ideal.domain.Employee;

import java.util.List;


public interface EmployeeService {
    public List showAllEmployees();
}

EmployeeServiceImpl

package cn.ideal.service;

import cn.ideal.dao.EmployeeDao;
import cn.ideal.domain.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    @Qualifier("employeeDao")
private EmployeeDao employeeDao;
    @Override
    public List showAllEmployees() {
        System.out.println("进入service层,查询所有员工的数据");
        List allEmployees = employeeDao.findAllEmployees();
        return allEmployees;
    }
}

index.html




    
    ssm


Welcome to SSM
点击查询所有employee

list.html




    
    employeeList


id name gender email dept_id
ssm整合算是基础了,以后有可能会把源码上传到托管网站

主要参考文章:SSM 框架整合完整流程讲解(IDEA + Maven) - 云+社区 - 腾讯云 (tencent.com)https://cloud.tencent.com/developer/article/1610326

待我们,别时相见时,都已有所成。

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

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

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