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

SSM上手极简整合

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

SSM上手极简整合

ssm整合的思路:

  • SpringMVC负责实现MVC设计模式
  • Mybatis负责数据持久层
  • Spring负责管理SpringMVC和MyBatis相关对象的创建和依赖注入。

视频地址:https://zhuanlan.zhihu.com/p/389748056

项目结构:

1、在pom.xml中配置依赖


    4.0.0

    com.itheima
    ssm
    1.0-SNAPSHOT
    war

    
        8
        8
    

    
    
        org.springframework
        spring-webmvc
        5.3.8
    

    
    
        org.springframework
        spring-jdbc
        5.3.8
    

    
    
        org.springframework
        spring-aop
        5.3.8
    
    
        org.springframework
        spring-aspects
        5.3.8
    

    
    
    
        org.mybatis
        mybatis
        3.5.7
    

    
    
        org.mybatis
        mybatis-spring
        2.0.6
    

    
    
    
    
        com.microsoft.sqlserver
        mssql-jdbc
        8.2.2.jre8
    

    
    
        com.mchange
        c3p0
        0.9.5.5
    


    
    
        jstl
        jstl
        1.2
    


    
    
        javax.servlet
        javax.servlet-api
        4.0.1
        provided
    

    
        org.projectlombok
        lombok-maven-plugin
        1.18.20.0
        provided
    

    
       
            
                src/main/java
                
                    **/*.xml
                
            
            
                src/main/resources
                
                    *.xml
                    *.properties
                
            
        

        
            
                org.apache.maven.plugins
                maven-war-plugin
                2.2
                
                    
                        
                            web
                        
                    
                
            
        
    

2. 配置web.xml

SpringMVC管理视图层,即一定在web.xml中配置,Spring作为管理者也得需要配置。同时也要处理静态资源以及请求和响应的字符过滤。MyBatis管理持久层,即在web.xml中不需要配置。





    contextConfigLocation
    classpath:spring.xml


    org.springframework.web.context.ContextLoaderListener




    dispatcherServlet
    org.springframework.web.servlet.DispatcherServlet
    
        contextConfigLocation
        classpath:springmvc.xml
    
1


    dispatcherServlet
    /




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


    characterEncodingFilter
    /*




    default
    *.js


    default
    *.css


    default
    *.jpg


3. 配置 Spring.xml

在Spring.xml中配置MyBatis与Spring整合





    
    
    
    
    
    



    
    
    



    



4. 配置Mybatis-config.xml

配置MyBatis辅助信息,比如打印SQL等





    
        
        
    

    
        
        
    



5. 配置SpringMVC.xml









    
    



7. StudentRepository
package com.itheima.repository;
import com.itheima.pojo.Student;
import java.util.List;

public interface StudentRepository {
    List findAll();
}
8. StudentRepository.xml



    
        select *from student
    

9. StudentService
package com.itheima.service;
import com.itheima.pojo.Student;
import java.util.List;

public interface StudentService {
    List findAll();
}

10. StudentServiceImpl
package com.itheima.service.impl;
import com.itheima.pojo.Student;
import com.itheima.repository.StudentRepository;
import com.itheima.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentRepository stuRepository;

    @Override
    public List findAll() {
        return stuRepository.findAll();
    }
}
11. StudentController
package com.itheima.controller;
import com.itheima.pojo.Student;
import com.itheima.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import java.util.List;

@Controller
@RequestMapping("/user")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @RequestMapping("/findAll")
    public ModelAndView findAll(){
        ModelAndView mv=new ModelAndView();
        List list = studentService.findAll();
        mv.setViewName("index");
        mv.addObject("list",list);
        return mv;
    }
}

12. index.jsp
<%--
  Created by IntelliJ IDEA.
  User: E106689
  Date: 6/16/2022
  Time: 5:44 PM
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>


    Title



    
${student.id} ${student.name} ${student.score}
13. SQL准备

此处我们使用的是Sql Server数据库

CREATE TABLE student (
  id int primary key identity(1,1),
  name varchar(20) COLLATE Latin1_General_100_BIN2_UTF8 NOT NULL,
  score int DEFAULT NULL
)


insert into student(name,score) values('李白',88)
insert into student(name,score) values('赵云',92)
insert into student(name,score) values('孙策',89)
insert into student(name,score) values('甄姬',94)
insert into student(name,score) values('貂蝉',100)
14. 访问地址

http://localhost:8080/user/findAll

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

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

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