基于spring 6 和Servlet 5 的SSM整合

      由于Spring boot3.0的出现(要求JDK17以上),之前的JDK1.8被迫过渡到JDK17,由于教学的需要,最近尝试使用JDK17来创建SSM案例,发现好多需要注意(多数是版本冲突)的问题,现分享一下该案例,希望能够给正在或准备过渡到该版本的同学以提示,避免一些弯路。

     开发环境: JDK17  Tomcat10 idea2022.3  mysql8.0 maven3.8.4 Servlet5.0 Spring6 Thymeleaf 3

pom.xml


  4.0.0
  org.example
  ssm-spring6
  war
  1.0-SNAPSHOT
  ssm-spring6 Maven Webapp
  http://maven.apache.org

  
    17
    17
    UTF-8
    6.0.6
    2.14.2
  

  

    
      com.github.pagehelper
      pagehelper
      5.3.2
    
    
      com.alibaba
      druid
      1.2.16
    
    
      jakarta.servlet
      jakarta.servlet-api
      5.0.0
      provided
    
    
    
      org.springframework
      spring-core
      ${springframework.version}
    
    
      org.springframework
      spring-beans
      ${springframework.version}
    
    
      org.springframework
      spring-context
      ${springframework.version}
    

    
      org.springframework
      spring-test
      ${springframework.version}
      test
    

    
    
      org.springframework
      spring-web
      ${springframework.version}
    
    
      org.springframework
      spring-webmvc
      ${springframework.version}
    

    
      org.springframework
      spring-aop
      ${springframework.version}
    

    
    
      org.aspectj
      aspectjrt
      1.9.19
    

    
      org.aspectj
      aspectjweaver
      1.9.19
    

    
    
      org.hibernate
      hibernate-validator
      8.0.0.Final
    

    
    
      org.mybatis
      mybatis
      3.5.13
    
    
      org.mybatis
      mybatis-spring
      3.0.1
    

    
    
      mysql
      mysql-connector-java
      8.0.30
      runtime
    

    
    
      org.thymeleaf
      thymeleaf-spring6
      3.1.1.RELEASE
    

    
      org.thymeleaf.extras
      thymeleaf-extras-java8time
      3.0.4.RELEASE
    
    
    
      org.springframework
      spring-jdbc
      ${springframework.version}
    
   
    
      org.webjars
      bootstrap
      5.2.3
    
    
      org.webjars
      jquery
      3.6.4
    

    
    
      ch.qos.logback
      logback-classic
      1.4.6
    


    
      junit
      junit
      4.13.2
      test
    

    
    
      com.fasterxml.jackson.core
      jackson-databind
      ${jackson.version}
    

    
      com.fasterxml.jackson.core
      jackson-core
      ${jackson.version}
    
    
      com.fasterxml.jackson.core
      jackson-annotations
      ${jackson.version}
    

    
      org.projectlombok
      lombok
      1.18.26
      true
    
  

  
    ssm-spring6
    
      
        src/main/java
        
          **/*.xml
        
        false
      
    
  

项目结构:

基于spring 6 和Servlet 5 的SSM整合_第1张图片

 

web.xml



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

    
        EncodingFilter
        /*
    
       
           contextConfigLocation
           classpath:beans.xml
       
    
        org.springframework.web.context.ContextLoaderListener
    
    
      
          hello
          org.springframework.web.servlet.DispatcherServlet
          
              contextConfigLocation
              classpath:spring-mvc.xml
          
          1
      
    
        hello
        /
    

beans.xml




        
        

    

    
        
        
        
            
                
            
        
    
    

    
    

spring-mvc.xml




    

    

    

    
        
        
        
            
                
                    
                        
                        
                        
                    
                
            
        
    


控制器:

package com.tonysong.ssm.controller;

import com.tonysong.ssm.pojo.Dept;
import com.tonysong.ssm.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.*;

@Controller
public class DeptController {

    @Autowired
    private DeptService deptService;

    @GetMapping("/depts")
    public String findDepts(Model model,
                            @RequestParam(name = "offset",defaultValue = "1") Integer offset,
                            @RequestParam(name = "pagesize",defaultValue = "3") Integer pagesize){
        model.addAttribute("pager",this.deptService.page_depts(offset,pagesize));
//        model.addAttribute("dname",dept.getDname());
        return "depts";
    }

    @GetMapping("/del/{id}")
    public @ResponseBody boolean deleteById(@PathVariable int id){
         this.deptService.deleteById(id);
         return true;
    }

    @GetMapping("/add")
    public String toadd(){
        return "add";
    }

    @PostMapping("/add")
    public String add(Dept dept){
          this.deptService.saveDept(dept);
          return "redirect:/depts";
    }
}

业务:

package com.tonysong.ssm.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tonysong.ssm.pojo.Dept;
import com.tonysong.ssm.service.DeptService;
import com.tonysong.ssm.mapper.DeptMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 宋伟宁
* @description 针对表【dept(部门表)】的数据库操作Service实现
* @createDate 2023-03-30 09:56:20
*/
@Service
public class DeptServiceImpl implements DeptService{

    @Autowired
    private DeptMapper deptMapper;

    @Override
    public List findAll() {
        System.out.println("service.....service....");
        return this.deptMapper.findAll();
    }

    @Override
    public void deleteById(int id) {
        this.deptMapper.deleteById(id);
    }

    @Override
    public PageInfo page_depts(Integer offset,Integer pagesie) {
        PageHelper.startPage(offset,pagesie);
        PageInfo pageInfo = new PageInfo<>(this.deptMapper.findAll());

        return pageInfo;
    }

    @Override
    public void saveDept(Dept dept) {
         if(dept.getDeptno()== null || dept.getDeptno()==0) {
             this.deptMapper.addDept(dept);
         }else{
             this.deptMapper.updateDept(dept);
         }
    }
}

Mapper:

public interface DeptMapper {

     //@Select("select * from dept")
     List findAll();

     void deleteById(int id);

     void addDept(Dept dept);

     void updateDept(Dept dept);
}



    
         insert  into dept(dname, db_source) values (#{dname},#{db_source})
    
    
        update dept set dname=#{dname}, db_source=#{db_source} where deptno=#{deptno}
    


    
        delete from dept where deptno=#{id}
    

    

引入静态资源:




    
    Title
    
         
         
    




Git:https://gitee.com/yangyang1110_admin/spring6-ssm.git

你可能感兴趣的:(servlet,spring,java)