目录
一、Springmvc简介及配置
什么是springMVC?
SpringMVC核心开发步骤
SpringMVC的组件
如何在项目中添加springmvc
添加相关依赖
二、Springmvc之helloword实现
三、SpringMVC的工作原理
四、crud之常用注释以及返回值
常用注解
Spring Web MVC是一种基于Java的实现了MVC设计模式的、请求驱动类型的、轻量级Web框架
DispatcherServlet在web.xml中的部署描述,从而拦截请求到springMVC
HandlerMapping的配置,从而将请求映射到处理器
HandlerAdapter的配置,从而支持多种类型的处理器
处理器(页面控制器)的配置,从而刊行功能处理
ViewResolver的配置,从而将逻辑视图名解析为具体的视图技术
前端控制器(DispatcherServlet)
请求到处理器映射(HandlerMapping)
处理器适配器(HandlerAdapter)
视图解析器(ViewResolver)
处理器或页面控制器(Controller)
验证器(Validator)
命令对象(Command 请求参数绑定到的对象就叫命令对象)
表单对象(Form Object提供给表单展示和提交到的对象就叫表单对象)
pom.xml
org.springframework
spring-webmvc
${spring.version}
JSTL依赖
jstl
jstl
1.2
taglibs
standard
1.1.2
在WEB-INF下添加springmvc-servlet.xml
修改web.xml
Archetype Created Web Application
contextConfigLocation
classpath:applicationContext-mybatis.xml
org.springframework.web.context.ContextLoaderListener
SpringMVC
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
/WEB-INF/springmvc-servlet.xml
1
true
SpringMVC
/
配置Tomcat
点击+选中Tomcat
点击+选择Atficat ,再选中自己的项目
HelloController.java
package com.maomao.ssm.controlle;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.xml.ws.RequestWrapper;
//被controller标记的类 会交给spring进行管理
/*
@Controller
@Service
@Repository
@Component
以上四个都代表 当前类 交给spring容器进行管理
*/
@Controller
public class HelloController {
// 浏览器发送请求
@RequestMapping("/helloReq")
public String hello(){
System.out.println("hello springmvc....");
return "hello";
}
}
1.浏览器发送请求到中央控制器(DispatcherServlet)
2.中央控制器通过处理器映射器(HandlerMapping)找到处理适配器(HandlerAdapter)
3.执行HandlerAdapter,返回值ModelAndView回传给中央控制器
4.视图解析器(ViewResolve)会对返回值ModelAndView进行渲染,得到view视图对象
5.最后中央控制器会将view视图对象返回给用户
BookMapper.xml
bid, bname, price
delete from t_mvc_book
where bid = #{bid,jdbcType=INTEGER}
insert into t_mvc_book (bid, bname, price
)
values (#{bid,jdbcType=INTEGER}, #{bname,jdbcType=VARCHAR}, #{price,jdbcType=REAL}
)
insert into t_mvc_book
bid,
bname,
price,
#{bid,jdbcType=INTEGER},
#{bname,jdbcType=VARCHAR},
#{price,jdbcType=REAL},
update t_mvc_book
bname = #{bname,jdbcType=VARCHAR},
price = #{price,jdbcType=REAL},
where bid = #{bid,jdbcType=INTEGER}
update t_mvc_book
set bname = #{bname,jdbcType=VARCHAR},
price = #{price,jdbcType=REAL}
where bid = #{bid,jdbcType=INTEGER}
BookMapper
package com.maomao.ssm.mapper;
import com.maomao.ssm.model.Book;
import com.maomao.ssm.model.vo.BookVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Map;
@Repository
public interface BookMapper {
int deleteByPrimaryKey(Integer bid);
int insert(Book record);
int insertSelective(Book record);
Book selectByPrimaryKey(Integer bid);
int updateByPrimaryKeySelective(Book record);
int updateByPrimaryKey(Book record);
// 通过in关键字进行查询,讲解foreach标签的使用
// 如果说参数是非实体类(book,Order,....),那么记得加上注解 @param,bookIds是对应collection属性的
List selectByIn(@Param("bookIds") List bookIds);
List selectBooksLike1(@Param("bname") String bname);
List selectBooksLike2(@Param("bname") String bname);
List selectBooksLike3(@Param("bname") String bname);
// list1 list2的结论是对于单表查询而言,可以用它resultType/resultMap接收,但是多表必须用resultMap接收
List list1();
List list2();
// 如果要传入多个查询参数,必须以对象的方式进行传递
List list3(BookVo vo);
// 如果是返回一条数据,那么返回值Map
// 如果是多条数据,那么返回值List
BookBiz
package com.maomao.ssm.biz;
import com.maomao.ssm.model.Book;
import com.maomao.ssm.model.vo.BookVo;
import com.maomao.util.PageBean;
import java.util.List;
import java.util.Map;
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);
List selectByIn(List bookIds);
public List selectBooksLike1(String bname);
public List selectBooksLike2(String bname);
public List selectBooksLike3(String bname);
List list1();
List list2();
List list3(BookVo vo);
List list4();
Map list5(Map map);
List listPager(PageBean pageBean, Map map);
List list6(BookVo vo);
List list7(BookVo vo);
}
BookBizImpl
package com.maomao.ssm.biz.impl;
import com.maomao.biz.BookBiz;
import com.maomao.ssm.mapper.BookMapper;
import com.maomao.ssm.model.Book;
import com.maomao.ssm.model.vo.BookVo;
import com.maomao.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
@Service
public class BookBizImpl implements BookBiz {
@Autowired
private BookMapper bookMapper;
//alt+insert 快速提供set/get/toString/构造方法
//alt+enter 快速构建实现类,填充代码的前半部分 Ctrl+1
// public BookMapper getBookMapper() {
// return bookMapper;
// }
//
// public void setBookMapper(BookMapper bookMapper) {
// this.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.updateByPrimaryKeySelective(record);
}
@Override
public int updateByPrimaryKey(Book record) {
return bookMapper.updateByPrimaryKey(record);
}
@Override
public List selectByIn(List bookIds) {
return bookMapper.selectByIn(bookIds);
}
@Override
public List listPager(PageBean pageBean, Map map){
return bookMapper.listPager(map);
}
// public List listPager(Map map, PageBean pageBean) {
// pageHelper分页插件相关代码
// if(pageBean != null && pageBean.isPagination()){
// PageHelper.startPage(pageBean.getPage(),pageBean.getRows());
// }
//
// List maps=bookMapper.listPager(map);
//
// if(pageBean != null && pageBean.isPagination()){
处理查询结果的前提,是需要分页的
// PageInfo info=new PageInfo(maps);
// pageBean.setTotal(info.getTotal()+"");
// }
//
// return maps;
// }
@Override
public List list6(BookVo vo) {
return bookMapper.list6(vo);
}
@Override
public List list7(BookVo vo) {
return bookMapper.list7(vo);
}
@Override
public List selectBooksLike1(String bname) {
return bookMapper.selectBooksLike1(bname);
}
@Override
public List selectBooksLike2(String bname) {
return bookMapper.selectBooksLike2(bname);
}
@Override
public List selectBooksLike3(String bname) {
return bookMapper.selectBooksLike3(bname);
}
@Override
public List list1() {
return bookMapper.list1();
}
@Override
public List list2() {
return bookMapper.list2();
}
@Override
public List list3(BookVo vo) {
return bookMapper.list3(vo);
}
@Override
public List list4() {
return bookMapper.list4();
}
@Override
public Map list5(Map map) {
return bookMapper.list5(map);
}
public BookBizImpl() {
super();
}
}
BookController
package com.maomao.ssm.controller;
import com.maomao.ssm.biz.BookBiz;
import com.maomao.ssm.model.Book;
import com.maomao.ssm.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Controller
@RequestMapping("/book")
public class BookController {
@Autowired
private BookBiz bookBiz;
// http://localhost:8080/book/list
@RequestMapping("/list")
public String hello(HttpServletRequest req){
System.out.println("hello springmvc...");
PageBean pageBean = new PageBean();
pageBean.setRequest(req);
Map map = new HashMap();
String bname = req.getParameter("bname");
map.put("bname",bname);
List maps = this.bookBiz.listPager(pageBean, map);
req.setAttribute("lst",maps);
return "hello";
}
@RequestMapping("/add")
public String add(Book book){
this.bookBiz.insertSelective(book);
return "hello";
}
@RequestMapping("/edit")
public String edit(Book book){
this.bookBiz.insertSelective(book);
return "hello";
}
@RequestMapping("/del/{bid}")
public String del(@PathVariable("bid") Integer bid){
this.bookBiz.deleteByPrimaryKey(bid);
return "hello";
}
}
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title
查询所有
新增
修改
删除
hello.jsp
<%--
Created by IntelliJ IDEA.
User: Lenovo
Date: 2022/8/16
Time: 22:45
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
hello
spring mvc 你好
${msg}
${lst}
${l.bid} : ${l.bname}
在刷新页面的时候就会报错
理由:因为a标签默认转发
解决:改成重定向
修改BookController
package com.maomao.ssm.controller;
import com.maomao.ssm.biz.BookBiz;
import com.maomao.ssm.model.Book;
import com.maomao.ssm.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Controller
@RequestMapping("/book")
public class BookController {
@Autowired
private BookBiz bookBiz;
// http://localhost:8080/book/list
@RequestMapping("/list")
public String hello(HttpServletRequest req){
System.out.println("hello springmvc...");
PageBean pageBean = new PageBean();
pageBean.setRequest(req);
Map map = new HashMap();
String bname = req.getParameter("bname");
map.put("bname",bname);
List maps = this.bookBiz.listPager(pageBean, map);
req.setAttribute("lst",maps);
return "hello";
}
@RequestMapping("/add")
public String add(Book book){
this.bookBiz.insertSelective(book);
return "redirect:/book/hello";
}
@RequestMapping("/edit")
public String edit(Book book){
this.bookBiz.insertSelective(book);
return "redirect:/book/hello";
}
@RequestMapping("/del/{bid}")
public String del(@PathVariable("bid") Integer bid){
this.bookBiz.deleteByPrimaryKey(bid);
return "redirect:/book/hello";
}
}
@Controller :被其标记的类会交给spring管理
@PathVariable:路径传参
@RequestMapping(@getMapping【查询】 @PostMapping【修改】 @DeleMapping【删除】 @PutMapping 【新增】的总称) 加在类上称窄化路径相当于包的概念