学习SpringMvc第三战-利用SpringMvc实现CRUD

前言:

小编讲述了参数传递,返回值以及页面跳转!为我们的CRUD提供了理论基础,接下来小编会通过SpringMvc实现CRUD来讲述在企业开发中必须要学会的CRUD

一.前期环境搭建

1.替换pom.xml的内容

学习SpringMvc第三战-利用SpringMvc实现CRUD_第1张图片


        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
    

 学习SpringMvc第三战-利用SpringMvc实现CRUD_第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}
            test
        
        
            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}
        
    

2.导入配置文件(小编上传资源)

学习SpringMvc第三战-利用SpringMvc实现CRUD_第3张图片

 3.修改xml文件

学习SpringMvc第三战-利用SpringMvc实现CRUD_第4张图片



  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
    /
  

4.点击创建自动生成代码

在book的类里面重写tostring方法!

学习SpringMvc第三战-利用SpringMvc实现CRUD_第5张图片

5.写一个类用于处理页面跳转

package com.lz.controller;

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

/**
 * @author lz
 * @create 2023-09-07 16:21
 */
@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;
    }
}

二.正式启动SpringMVC的CRUD

1.建立接口,调用自动生成的接口

package com.lz.Biz;

import com.lz.model.Book;
import com.lz.utlis.PageBean;

import java.util.List;

public interface BookBiz {
    int deleteByPrimaryKey(Integer bid);

    int insert(Book record);

    int insertSelective(Book record);

    Book selectByPrimaryKey(Integer bid);

    int updateByPrimaryKeySelective(Book record);

    int updateByPrimaryKey(Book record);

    ListlistPager (Book  book, PageBean  pageBean);
}

实现接口的方法,并且用@serverce注解被spring管理,@Autowries调用Book.xml

文件

package com.lz.Biz;

import com.lz.mapper.BookMapper;
import com.lz.model.Book;
import com.lz.utlis.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author lz
 * @create 2023-09-08 9:32
 *
 */
@Service
public class BookBizImpl    implements   BookBiz{
    @Autowired
    private BookMapper  bookMapper;
    @Override
    public int deleteByPrimaryKey(Integer bid) {
        return bookMapper.deleteByPrimaryKey(bid) ;
    }

    @Override
    public int insert(Book record) {
        return bookMapper.insert(record);
    }

    @Override
    public int insertSelective(Book record) {
        return bookMapper.insertSelective(record);
    }

    @Override
    public Book selectByPrimaryKey(Integer bid) {
        return bookMapper.selectByPrimaryKey(bid);
    }

    @Override
    public int updateByPrimaryKeySelective(Book record) {
        return bookMapper.updateByPrimaryKey(record);
    }

    @Override
    public int updateByPrimaryKey(Book record) {
        return bookMapper.updateByPrimaryKey(record);
    }

    @Override
    public List listPager(Book book, PageBean pageBean) {
         return bookMapper.listPager(book);
}

2.构建分页代码

2.1书写BookMapper.xml中分页的方法


  

2.2调用分页的方法

List listPager(Book  book);

2.3导入一个工具类用于分页实现

学习SpringMvc第三战-利用SpringMvc实现CRUD_第6张图片

package com.lz.tag;

import com.lz.utlis.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(); } }

2.4导入一个tid文件用于分页实现

学习SpringMvc第三战-利用SpringMvc实现CRUD_第7张图片




    
  zking 1.1 core library
  zking core
  1.1
  zking
  http://jsp.veryedu.cn
  
  
  
    page
    com.lz.tag.PageTag
    JSP
    
        pageBean
        true
        true
    
  
  

3.实现后端功能

3.1构建功能实现代码

package com.lz.controller;

import com.lz.Biz.BookBiz;
import com.lz.model.Book;
import com.lz.utlis.PageBean;
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 lz
 * @create 2023-09-08 16:20
 */
@Controller
@RequestMapping("/book")
public class BookController {
    @Autowired
    private   BookBiz  bookBiz;
      //增加
    @RequestMapping("/add")
    public     String  add(Book  book){
        int i = bookBiz.insertSelective(book);
        return  "redirect:list";
    }
     //删除
     @RequestMapping("/del/{bid}")
     public     String  del(@PathVariable("bid")Integer bid){
         int i = bookBiz.deleteByPrimaryKey(bid);
         return  "redirect:/book/list";
     }
    //修改
    @RequestMapping("/edit")
    public   String edit(Book  book){
        int i = bookBiz.updateByPrimaryKeySelective(book);
        return  "redirect:list";
    }
     // 查询
    @RequestMapping("/list")
    //Book接收前台传递的参数
    public  String list(Book  book  , HttpServletRequest  request){
        PageBean  pageBean=new PageBean();
        pageBean.setRequest(request);
        List books = bookBiz.listPager(book, pageBean);
        request.setAttribute("lst",books);
        request.setAttribute("pageBean",pageBean);
        return "book/list";
    }
     //数据回显
    @RequestMapping("/preSave")
    public   String  preSave(Book  book, Model model){
      if (book!=null && book.getBid()!=null && book.getBid()!=0) {
          Book b = bookBiz.selectByPrimaryKey(book.getBid());
          model.addAttribute("b",b);
      }
      return   "book/edit";

    }

}


3.2在BookMapper.xml文件编写查询的方法(并且实现)


  

3.3构建list界面(展示数据)

<%@ 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.bid } ${b.bname } ${b.price } 修改 删除

3.4构建edit界面

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



    
    书籍编辑界面


书籍编号:
书籍名字:
书籍价格:

3.5效果展示

1.增加

学习SpringMvc第三战-利用SpringMvc实现CRUD_第8张图片

2.删除

3.修改

学习SpringMvc第三战-利用SpringMvc实现CRUD_第9张图片

4.查询 

你可能感兴趣的:(spring,mvc,学习,java,数据库,spring,mvc,tomcat,intellij-idea)