SpringMVC的增删改查的案例

SpringMVC的增删改查的案例_第1张图片

目录

前言:

1.总体思路:

2.前期准备

3.前台页面


前言:

我们今天来学习研究SpringMVC的增删改查,希望这篇博客能够帮助正在学习,工作的你们!!!

1.总体思路:

首先我们得有SpringMVC基础,http://t.csdn.cn/IODehicon-default.png?t=N7T8http://t.csdn.cn/IODehSpringMVC常用注解、参数传递、返回值,

http://t.csdn.cn/C2XPOicon-default.png?t=N7T8http://t.csdn.cn/C2XPO才能理解掌握到今天的SpringMVC的增删改查的案例。

步骤:

        1.通过maven插件逆向生成对应的(model,mapper.xml , mapper.java)

        2.biz层

        3.web层(这里我们使用自定义mvc的分页标签,pom依赖)

        

2.前期准备

2.1.建立表(t_mvc_student)

SpringMVC的增删改查的案例_第2张图片

2.2修改generatorConfig.xml文件




    
    

    
    

    
    
        
        
            
             
        

        
        

        
        
            
            
        

        
        
        
        
            
            
            
            
            
            
            
            
        

        
        
            
            
        

        
        
        
        
        
            
            
        

        
        
        
        
        
        
        
        
        
        
        
        
        

        

SpringMVC的增删改查的案例_第3张图片

pom.xml




    4.0.0

    org.example
    springmvc01
    1.0-SNAPSHOT
    war

    springmvc01 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}
        
    


    
        springmvc01
        
            
            
                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
            
        
    

ClazzMapper.xml




  
    
      
      
      
      
    
  
  
    sid, sname, sage, sinfo
  
  

  

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

ClazzMapper

package com.lyazyssm.mapper;

import com.lyazyssm.model.Clazz;
import com.lyazyssm.util.PageBean;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface ClazzMapper {
    int deleteByPrimaryKey(Integer sid);

    int insert(Clazz record);

    int insertSelective(Clazz record);

    Clazz selectByPrimaryKey(Integer sid);

    int updateByPrimaryKeySelective(Clazz record);

    int updateByPrimaryKey(Clazz record);

    List listPager(Clazz clazz, PageBean pageBean);
}

PageTag

package com.lyazyssm.tag;

import java.io.IOException;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;

import com.lyazyssm.util.PageBean;

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.getParamMap(); 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(); } }

 applicationContext.xml:



    

applicationContext-mybatis.xml



    
    
    
    
    

    

    
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
    

    
    
    
        
        
        
        
        
        
        
        
            
                
                    
                        
                            helperDialect=mysql
                        
                    
                
            
        
    

    
    
    
        
        
        
        
    

    
        
    
    
    

jdbc.properties:

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

log4j2.xml





    
        
        /root/workspace/lucenedemo/logs
        /root/workspace/lucenedemo/logs/error
        /root/workspace/lucenedemo/logs/warn
        %d{yyyy-MM-dd HH:mm:ss.SSS} [%t-%L] %-5level %logger{36} - %msg%n
    

    
        
        
            
            
            
            
            
        

        
        
        
            
        
        
        
            
            
            
            
                
                
                
                
                
                
            
        

        
            
            
            
                
                
            
            
            
        

        
            
            
            
                
                
                
            
        

    

    
    
        
        
        

        
        
        
        
        
        


        
        
            
            
            
            
        

    

web.xml



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

  
  
    org.springframework.web.util.IntrospectorCleanupListener
  

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

  
  
    SpringMVC
    org.springframework.web.servlet.DispatcherServlet
    
    
      contextConfigLocation
      /WEB-INF/spring-mvc.xml
    
    1
    
    true
  
  
    SpringMVC
    /
  

3.前台页面

 list.jsp

​
<%--
  Created by IntelliJ IDEA.
  User: 微微
  Date: 2023/9/8
  Time: 17:09
  To change this template use File | Settings | File Templates.
--%><%@ 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"%>



    
    
    
    
    


新增
ID 名称 年龄 描述
${b.sid } ${b.sname } ${b.sage } ${b.sinfo } 修改 删除

clzEdit.jsp

<%--
  Created by IntelliJ IDEA.
  User: 微微
  Date: 2023/9/8
  Time: 17:09
  To change this template use File | Settings | File Templates.
--%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>



    
    


id:
cname:
cteacher:

运行结果:

查询所有:

SpringMVC的增删改查的案例_第4张图片

分页效果:

SpringMVC的增删改查的案例_第5张图片

SpringMVC的增删改查的案例_第6张图片

你可能感兴趣的:(数据库,mysql,mybatis)