前言:
小编讲述了参数传递,返回值以及页面跳转!为我们的CRUD提供了理论基础,接下来小编会通过SpringMvc实现CRUD来讲述在企业开发中必须要学会的CRUD
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}
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}
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
/
在book的类里面重写tostring方法!
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;
}
}
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);
}
List listPager(Book book);
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("");
// 分页执行的JS代码
sb.append("");
return sb.toString();
}
}
zking 1.1 core library
zking core
1.1
zking
http://jsp.veryedu.cn
page
com.lz.tag.PageTag
JSP
pageBean
true
true
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";
}
}
<%@ 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 }
修改
删除
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
书籍编辑界面