SpringMvc--CRUD

目录

一.什么是SpringMvc--CRUD

二.前期准备

公共页面跳转(专门用来处理页面跳转)

三.ssm之CRUD后端实现

配置pom.xml

双击mybatis-generator:generate自动生成mapper

编写generatorConfig.xml

项目结构 

编写PagerAspect切面类

编写hpjyBiz接口类

编写hpjyBizImpl接口实现类

编写Spring-mybatias.xml

编写Spring.mvc.xml

在重新配置pom.xml文件

编写page tag类

编写hpjyController类

四.ssm之CRUD前端实现

编写jsp/hpjy/list.jsp

测试页面

测试查询

测试新增

 ​编辑

测试修改 

测试删除

​编辑 


一.什么是SpringMvc--CRUD

什么是SpringMvc--CRUD

Spring MVC是一个基于Java的Web应用程序框架,它提供了一种MVC(模型-视图-控制器)的架构模式来开发Web应用程序。CRUD是指创建(Create)、读取(Read)、更新(Update)和删除(Delete)这四个基本的数据操作。在Spring MVC中,你可以使用相关的注解和配置方式来实现CRUD操作,简化了Web应用程序的开发过程。

二.前期准备

公共页面跳转(专门用来处理页面跳转)

package com.xy.web;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author bing人
 * @site
 * @company xy集团
 * @create 2023-09-07 14:53
 */
//专门用来处理页面跳转
@Controller
public class PageController {
    @RequestMapping("/page/{page}")
    public  String toPage(@PathVariable("page") String page){
        return page;
    }

    @RequestMapping("/page/{Dir}{page}")
    public  String toDirPage(@PathVariable("Dir") String dir,
                             @PathVariable("page") String page){
        return dir + "/" + page;
    }


    }

三.ssm之CRUD后端实现

配置pom.xml




  4.0.0

  com.zking
  xyzyssm
  1.0-SNAPSHOT
  war

  xyzyssm 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
    3.2.0
    1.7.13

    
    4.12
    4.0.0
    1.18.2

    
    1.2
    1.1.2
    
    5.0.2.RELEASE

    2.9.3
  


  
    
    
      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}
      
        
          commons-pool2
          org.apache.commons
        
      
    
    
      org.apache.commons
      commons-pool2
      ${commons.pool2.version}
    

    

    
    
    
    
      org.slf4j
      slf4j-api
      ${slf4j.version}
    
    
      org.slf4j
      jcl-over-slf4j
      ${slf4j.version}
      runtime
    

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

    
    
      com.lmax
      disruptor
      ${log4j2.disruptor.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}
    
    
      jstl
      jstl
      ${jstl.version}
    
    
      taglibs
      standard
      ${standard.version}
    
    
      com.fasterxml.jackson.core
      jackson-databind
      ${jackson.version}
    
    
      com.fasterxml.jackson.core
      jackson-core
      ${jackson.version}
    
    
      com.fasterxml.jackson.core
      jackson-annotations
      ${jackson.version}
    
  

  
    xyzyssm

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

    
      
        org.apache.maven.plugins
        maven-compiler-plugin
        ${maven.compiler.plugin.version}
        
          ${maven.compiler.source}
          ${maven.compiler.target}
          ${project.build.sourceEncoding}
        
      
      
        org.mybatis.generator
        mybatis-generator-maven-plugin
        1.3.2
        
          
          
            mysql
            mysql-connector-java
            ${mysql.version}
          
        
        
          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
      
    
  

双击mybatis-generator:generate自动生成mapper

(注:只要双击一次就可以了,如果多次双击会生成多个mapper)

SpringMvc--CRUD_第1张图片

编写generatorConfig.xml




    
    

    
    

    
    
        
        
            
             
        

        
        

        
        
            
            
        

        
        
        
        
            
            
            
            
            
            
            
            
        

        
        
            
            
        

        
        
        
        
        
            
            
        

        
        
        
        
        
        
        
        
        
        
        
        
        

        

项目结构 

 SpringMvc--CRUD_第2张图片

编写PagerAspect切面类

package com.xy.aspect;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xy.utils.PageBean;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author bing人
 * @site
 * @company xy集团
 * @create 2023-08-25 16:33
 */
@Aspect //代表当前类为切面类
@Component //代表当前类交给spring进行管理
public class PagerAspect {
    @Around("execution(* *..*Biz.*Pager(..))")
    public Object invoke(ProceedingJoinPoint args) throws Throwable{

        PageBean pageBean=null;
        //获取目标方法中的所有参数
        Object[] ags = args.getArgs();
        for (Object param : ags) {
            if(param instanceof PageBean){
            pageBean = (PageBean) param;
            break;
            }
        }
        if(pageBean !=null && pageBean.isPagination())
            PageHelper.startPage(pageBean.getPage(),pageBean.getRows());
        //执行目标方法
        Object lst = args.proceed();
        if(pageBean !=null && pageBean.isPagination()){
            PageInfo info = new PageInfo((List) lst);
            pageBean.setTotal((int)info.getTotal());
        }
        return lst;
    }

}

编写hpjyBiz接口类

package com.xy.biz;

import com.xy.model.hpjy;
import com.xy.utils.PageBean;

import java.util.List;

public interface hpjyBiz {
    int deleteByPrimaryKey(Integer id);

    int insert(hpjy record);

    int insertSelective(hpjy record);

    hpjy selectByPrimaryKey(Integer id);

    int updateByPrimaryKeySelective(hpjy record);

    int updateByPrimaryKey(hpjy record);

    List listPager(hpjy hpjy, PageBean pageBean);
}

编写hpjyBizImpl接口实现类

package com.xy.biz.impl;

import com.xy.biz.hpjyBiz;
import com.xy.mapper.hpjyMapper;
import com.xy.model.hpjy;
import com.xy.utils.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author bing人
 * @site
 * @company xy集团
 * @create 2023-09-07 16:33
 */
@Service
public class hpjyBizImpl implements hpjyBiz {
   @Autowired
    private hpjyMapper hpjymapper;
    @Override
    public int deleteByPrimaryKey(Integer id) {
        return hpjymapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(hpjy record) {
        return hpjymapper.insert(record);
    }

    @Override
    public int insertSelective(hpjy record) {
        return hpjymapper.insertSelective(record);
    }

    @Override
    public hpjy selectByPrimaryKey(Integer id) {
        return hpjymapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(hpjy record) {
        return hpjymapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(hpjy record) {
        return hpjymapper.updateByPrimaryKey(record);
    }

    @Override
    public List listPager(hpjy hpjy, PageBean pageBean) {
        return hpjymapper.SelectBycon(hpjy);
    }
}

编写Spring-mybatias.xml



    
    
    
    
    

    

    
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
    

    
    
    
        
        
        
        
        
        
        
        
            
                
                    
                        
                            helperDialect=mysql
                        
                    
                
            
        
    

    
    
    
        
        
        
        
    

    
        
    
    
    

编写Spring.mvc.xml



    
    

    
    

    
    
        
        
        
        
    



    



    
    
    

在重新配置pom.xml文件




  4.0.0

  org.example
  xyzy
  1.0-SNAPSHOT
  war

  xyzy 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
    3.2.0
    1.7.13
    
    4.12
    4.0.0
    1.18.2

    1.1.0
    2.10.0

    2.9.0
    1.7.1.RELEASE
    2.9.3
    1.2
    1.1.2
    8.0.47
    1.3.3
    5.0.2.Final

    1.3.2
  

  
    
    
      org.springframework
      spring-core
      ${spring.version}
    
    
      org.springframework
      spring-beans
      ${spring.version}
    
    
      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.springframework
      spring-context-support
      ${spring.version}
    

    
    
      org.mybatis.caches
      mybatis-ehcache
      ${mybatis.ehcache.version}
    
    
    
      net.sf.ehcache
      ehcache
      ${ehcache.version}
    

    
      redis.clients
      jedis
      ${redis.version}
    
    
      org.springframework.data
      spring-data-redis
      ${redis.spring.version}
    
    
      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.apache.commons
      commons-dbcp2
      ${commons.dbcp2.version}
      
        
          commons-pool2
          org.apache.commons
        
      
    
    
      org.apache.commons
      commons-pool2
      ${commons.pool2.version}
    

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

    

    
    
    
    
      org.slf4j
      slf4j-api
      ${slf4j.version}
    
    
      org.slf4j
      jcl-over-slf4j
      ${slf4j.version}
      runtime
    

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

    
    
      com.lmax
      disruptor
      ${log4j2.disruptor.version}
    

    
    
      junit
      junit
      ${junit.version}

    
    
      javax.servlet
      javax.servlet-api
      ${servlet.version}
      provided
    
    
      org.projectlombok
      lombok
      ${lombok.version}
      provided
    
    
      jstl
      jstl
      ${jstl.version}
    
    
      taglibs
      standard
      ${standard.version}
    
    
      org.apache.tomcat
      tomcat-jsp-api
      ${tomcat-jsp-api.version}
    
    
      commons-fileupload
      commons-fileupload
      ${commons-fileupload.version}
    

    
      org.hibernate
      hibernate-validator
      ${hibernate-validator.version}
    

    
    
      org.apache.shiro
      shiro-core
      ${shiro.version}
    
    
      org.apache.shiro
      shiro-web
      ${shiro.version}
    
    
      org.apache.shiro
      shiro-spring
      ${shiro.version}
    
  

  
    xyzy


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


      

        
          org.apache.maven.plugins
          maven-compiler-plugin
          ${maven.compiler.plugin.version}
          
            ${maven.compiler.source}
            ${maven.compiler.target}
            ${project.build.sourceEncoding}
          
        
        
          org.mybatis.generator
          mybatis-generator-maven-plugin
          1.3.2
          
            
            
              mysql
              mysql-connector-java
              ${mysql.version}
            
          
          
            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
        
      
  

编写page tag类

package com.xy.tag;

import com.xy.utils.PageBean;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;
import java.io.IOException;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class PageTag extends BodyTagSupport{
	private PageBean pageBean;// 包含了所有分页相关的元素
	
	public PageBean getPageBean() {
		return pageBean;
	}

	public void setPageBean(PageBean pageBean) {
		this.pageBean = pageBean;
	}

	@Override
	public int doStartTag() throws JspException {
//		没有标签体,要输出内容
		JspWriter out = pageContext.getOut();
		try {
			out.print(toHTML());
		} catch (IOException e) {
			e.printStackTrace();
		}
		return super.doStartTag();
	}

	private String toHTML() {
		StringBuffer sb = new StringBuffer();
//		隐藏的form表单---这个就是上一次请求下次重新发的奥义所在
//		上一次请求的URL
		sb.append("
"); sb.append(" "); // 上一次请求的参数 Map paramMap = pageBean.getMap(); if(paramMap != null && paramMap.size() > 0) { Set> entrySet = paramMap.entrySet(); for (Entry entry : entrySet) { // 参数名 String key = entry.getKey(); // 参数值 for (String value : entry.getValue()) { // 上一次请求的参数,再一次组装成了新的Form表单 // 注意:page参数每次都会提交,我们需要避免 if(!"page".equals(key)) { sb.append(" "); } } } } sb.append(""); // 分页条 sb.append("
    "); sb.append("
  • 首页
  • "); sb.append("
  • <
  • ");// less than 小于号 // sb.append("
  • 1
  • "); // sb.append("
  • 2
  • "); sb.append("
  • "+pageBean.getPage()+"
  • "); sb.append("
  • >
  • "); sb.append("
  • 尾页
  • "); sb.append("
  • 到第
  • "); sb.append("
  • 确定
  • "); sb.append("
  • 共"+pageBean.getTotal()+"条
  • "); sb.append("
"); // 分页执行的JS代码 sb.append(""); return sb.toString(); } }

编写hpjyController类

package com.xy.web;

import com.xy.biz.hpjyBiz;
import com.xy.model.hpjy;
import com.xy.utils.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

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

/**
 * @author bing人
 * @site
 * @company xy集团
 * @create 2023-09-08 19:13
 */
@Controller
@RequestMapping("/hpjy")
public class hpjyController {
    @Autowired
    private hpjyBiz hpjyBiz;
    //增
    @RequestMapping("/add")
    public  String add(hpjy hpjy){
        int i = hpjyBiz.insertSelective(hpjy);
        return  "redirect:list";
    }
    //删
    @RequestMapping("/del")
    public  String del(hpjy hpjy){
        hpjyBiz.deleteByPrimaryKey(hpjy.getId());
        return "redirect:list";
    }
    //改
    public String  edit(hpjy hpjy){
        hpjyBiz.updateByPrimaryKeySelective(hpjy);
        return "redirect:list";
    }
    //查
    @GetMapping("/list")
    public  String list(hpjy hpjy, HttpServletRequest request){
        PageBean pageBean =new PageBean();
        pageBean.setRequest(request);
        List hpjies = hpjyBiz.listPager(hpjy, pageBean);
        request.setAttribute("lst",hpjies);
        request.setAttribute("pageBean",pageBean);
        return "hpjy/list";
    }
    //数据回显
    @RequestMapping("/presave")
    public String presave(hpjy hpjy,HttpServletRequest request){
        if(hpjy != null && hpjy.getId() !=null && hpjy.getId() !=0){
            hpjy h = hpjyBiz.selectByPrimaryKey(hpjy.getId());
            request.setAttribute("h",h);
        }
         return "hpjy/edit";
    }
}

四.ssm之CRUD前端实现

编写jsp/hpjy/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.id } ${b.name } ${b.type } 修改 删除
${pageBean }

测试页面

SpringMvc--CRUD_第3张图片

测试查询

测试新增

 SpringMvc--CRUD_第4张图片

测试修改 

SpringMvc--CRUD_第5张图片

测试删除

SpringMvc--CRUD_第6张图片 

你可能感兴趣的:(java,前端,数据库)