SpringMVC之CRUD

SpringMVC实现增删改查

1. 导入相关依赖

pom.xml




  4.0.0

  org.example
  xisslzy_ssm
  1.0-SNAPSHOT
  war

  xisslzy_ssm Maven Webapp
  
  http://www.example.com

  
    UTF-8
    1.8
    1.8
    3.7.0

    
    
    5.0.2.RELEASE
    
    3.4.5
    
    5.1.44
    
    5.1.2
    
    1.3.1
    
    2.1.1
    2.4.3
    
    2.9.1
    
    4.12
    4.0.0
    1.18.2
  


  
    
    
      org.springframework
      spring-context
      ${spring.version}
    
    
      org.springframework
      spring-orm
      ${spring.version}
    
    
      org.springframework
      spring-tx
      ${spring.version}
    
    
      org.springframework
      spring-aspects
      ${spring.version}
    
    
      org.springframework
      spring-web
      ${spring.version}
    
    
      org.springframework
      spring-test
      ${spring.version}
    

    
    
      org.mybatis
      mybatis
      ${mybatis.version}
    
    
    
      mysql
      mysql-connector-java
      ${mysql.version}
    
    
    
      com.github.pagehelper
      pagehelper
      ${pagehelper.version}
    
    
    
      org.mybatis
      mybatis-spring
      ${mybatis.spring.version}
    

    
    
      org.apache.commons
      commons-dbcp2
      ${commons.dbcp2.version}
    
    
      org.apache.commons
      commons-pool2
      ${commons.pool2.version}
    

    
    
    
      org.apache.logging.log4j
      log4j-core
      ${log4j2.version}
    
    
      org.apache.logging.log4j
      log4j-api
      ${log4j2.version}
    
    
    
      org.apache.logging.log4j
      log4j-web
      ${log4j2.version}
    

    
    
      junit
      junit
      ${junit.version}

    
    
      javax.servlet
      javax.servlet-api
      ${servlet.version}
      provided
    
    
      org.projectlombok
      lombok
      ${lombok.version}
      provided
    

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

    
    
      javax.servlet.jsp
      javax.servlet.jsp-api
      2.3.3
    
    
      jstl
      jstl
      1.2
    
    
      taglibs
      standard
      1.1.2
    
  


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

    
      
        org.mybatis.generator
        mybatis-generator-maven-plugin
        1.3.2
        
          
          
            mysql
            mysql-connector-java
            5.1.44
          
        
        
          true
        
      

      
          maven-clean-plugin
          3.1.0
        
        
        
          maven-resources-plugin
          3.0.2
        
        
          maven-compiler-plugin
          3.8.0
        
        
          maven-surefire-plugin
          2.22.1
        
        
          maven-war-plugin
          3.2.2
        
        
          maven-install-plugin
          2.5.2
        
        
          maven-deploy-plugin
          2.8.2
        
      
  

web.xml 



  Archetype Created Web Application
  
  
  
    contextConfigLocation
    classpath:spring-context.xml
  
  
  
    org.springframework.web.context.ContextLoaderListener
  
  

  
  
    encodingFilter
    org.springframework.web.filter.CharacterEncodingFilter
    true
    
      encoding
      UTF-8
    
  
  
    encodingFilter
    /*
  

  
  
    SpringMVC
    org.springframework.web.servlet.DispatcherServlet
    
    
      contextConfigLocation
      classpath:spring-mvc.xml
    
    1
    
    true
  
  
    SpringMVC
    /
  

 2. 添加配置文件

jdbc.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost/mybatis_ssm?useUnicode=true&characterEncoding=UTF-8
jdbc.username=root
jdbc.password=123456

 generatorConfig.xml




    
    

    
    

    
    
        
        
            
             
        

        
        

        
        
            
            
        

        
        
        
        
            
            
            
            
            
            
            
            
        

        
        
            
            
        

        
        
        
        
        
            
            
        


        

spring-mvc.xml



    
    

    
    

    
    
        
        
        
        
    



    

3. 逆向生成对应的类

实体类

package com.xissl.model;

import lombok.ToString;

@ToString
public class Student {
    private String sid;

    private String sname;

    private String sage;

    private String ssex;

    public Student(String sid, String sname, String sage, String ssex) {
        this.sid = sid;
        this.sname = sname;
        this.sage = sage;
        this.ssex = ssex;
    }

    public Student() {
        super();
    }

    public String getSid() {
        return sid;
    }

    public void setSid(String sid) {
        this.sid = sid;
    }

    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    public String getSage() {
        return sage;
    }

    public void setSage(String sage) {
        this.sage = sage;
    }

    public String getSsex() {
        return ssex;
    }

    public void setSsex(String ssex) {
        this.ssex = ssex;
    }
}

 mapper接口

package com.xissl.mapper;

import com.xissl.model.Student;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface StudentMapper {
    int deleteByPrimaryKey(String sid);

    int insert(Student record);

    int insertSelective(Student record);

    Student selectByPrimaryKey(String sid);

    int updateByPrimaryKeySelective(Student record);

    int updateByPrimaryKey(Student record);

    List listPager(Student student);
}

sql映射文件




  
    
      
      
      
      
    
  
  
    sid, sname, sage, ssex
  
  

  

  
    delete from t_mysql_student
    where sid = #{sid,jdbcType=VARCHAR}
  
  
    insert into t_mysql_student (sid, sname, sage, 
      ssex)
    values (#{sid,jdbcType=VARCHAR}, #{sname,jdbcType=VARCHAR}, #{sage,jdbcType=VARCHAR}, 
      #{ssex,jdbcType=VARCHAR})
  
  
    insert into t_mysql_student
    
      
        sid,
      
      
        sname,
      
      
        sage,
      
      
        ssex,
      
    
    
      
        #{sid,jdbcType=VARCHAR},
      
      
        #{sname,jdbcType=VARCHAR},
      
      
        #{sage,jdbcType=VARCHAR},
      
      
        #{ssex,jdbcType=VARCHAR},
      
    
  
  
    update t_mysql_student
    
      
        sname = #{sname,jdbcType=VARCHAR},
      
      
        sage = #{sage,jdbcType=VARCHAR},
      
      
        ssex = #{ssex,jdbcType=VARCHAR},
      
    
    where sid = #{sid,jdbcType=VARCHAR}
  
  
    update t_mysql_student
    set sname = #{sname,jdbcType=VARCHAR},
      sage = #{sage,jdbcType=VARCHAR},
      ssex = #{ssex,jdbcType=VARCHAR}
    where sid = #{sid,jdbcType=VARCHAR}
  

4. 后台代码编写

业务逻辑层

package com.xissl.biz;

import com.xissl.model.Student;
import com.xissl.utils.PageBean;

import java.util.List;

public interface StudentBiz {
    int deleteByPrimaryKey(String sid);

    int insert(Student record);

    int insertSelective(Student record);

    Student selectByPrimaryKey(String sid);

    int updateByPrimaryKeySelective(Student record);

    int updateByPrimaryKey(Student record);

    List listPager(Student student, PageBean pageBean);
}

实现接口

package com.xissl.biz.impl;

import com.xissl.biz.StudentBiz;
import com.xissl.mapper.StudentMapper;
import com.xissl.model.Student;
import com.xissl.utils.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author xissl
 * @create 2023-09-07 16:07
 */
@Service
public class StudentBizImpl implements StudentBiz {

    @Autowired
    private StudentMapper studentMapper;

    @Override
    public int deleteByPrimaryKey(String sid) {
        return studentMapper.deleteByPrimaryKey(sid);
    }

    @Override
    public int insert(Student record) {
        return studentMapper.insert(record);
    }

    @Override
    public int insertSelective(Student record) {
        return studentMapper.insertSelective(record);
    }

    @Override
    public Student selectByPrimaryKey(String sid) {
        return studentMapper.selectByPrimaryKey(sid);
    }

    @Override
    public int updateByPrimaryKeySelective(Student record) {
        return studentMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Student record) {
        return studentMapper.updateByPrimaryKey(record);
    }

    @Override
    public List listPager(Student student, PageBean pageBean) {
        return studentMapper.listPager(student);
    }
}

 controller层

package com.xissl.web;

import com.xissl.biz.StudentBiz;
import com.xissl.model.Student;
import com.xissl.utils.PageBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author xissl
 * @create 2023-09-07 16:59
 */
@Controller
@RequestMapping("/stu")
public class StudentController {
    @Autowired
    private StudentBiz studentBiz;
//    增
    @RequestMapping("/add")
    public String add(Student student){
        int i = studentBiz.insertSelective(student);
        return "redirect:list";
    }
//    删
    @RequestMapping("/del")
    public String del(String sid){
        studentBiz.deleteByPrimaryKey(sid);
        return "redirect:/stu/list";
    }
//    改
    @RequestMapping("/edit")
    public String edit(Student student){
        studentBiz.updateByPrimaryKey(student);
        return "redirect:list";

    }
//    查
    @RequestMapping("/list")
    public String list(Student student, HttpServletRequest request){
//        Student student是前台用来给后台传递参数的
        PageBean pageBean = new PageBean();
        pageBean.setRequest(request);
        List students = studentBiz.listPager(student, pageBean);
        request.setAttribute("list",students);
        request.setAttribute("pageBean",pageBean);
        return "stu/list";
    }
//    数据回显
    @RequestMapping("/preSave")
    public String preSave(Student student, Model model){
        if(student != null && student.getSid()!=null){
            Student s = studentBiz.selectByPrimaryKey(student.getSid());
            model.addAttribute("s",s);
        }
        return "stu/edit";
    }
}

 5. 前台代码编写

list.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
<%@ taglib uri="http://jsp.veryedu.cn" prefix="z"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>



    
    
    
    博客列表
    


<%-- --%>
新增
学生编号 学生姓名 学生年龄 学生性别 操作
${b.sid } ${b.sname } ${b.sage } ${b.ssex } 修改 删除

edit.jsp 

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


    Title


学生编号:
学生姓名:
学生年龄:
学生性别:

 

你可能感兴趣的:(java,数据库,开发语言,intellij-idea,spring)