一、s pring MVC增删改查&@PathVariable注解
定义方法
package com.zking.ssm.mapper;
import com.zking.ssm.model.Book;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface BookMapper {
List<Book> queryPager(Book book);
Book querySingleBook(Integer bookId);
void update(Book book);
void delete(Integer bookId);
void insert(Book book);
}
编写sql语句
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.zking.ssm.mapper.BookMapper" >
<sql id="baseSql">
book_id,book_name,book_price
</sql>
<insert id="insert">
insert into tb_book(book_name,book_price)
values(#{bookName},#{bookPrice})
</insert>
<delete id="delete">
delete from tb_book where book_id=#{bookId}
</delete>
<update id="update">
update tb_book set book_name=#{bookName},book_price=#{bookPrice}
where book_id=#{bookId}
</update>
<select id="queryPager" resultType="com.zking.ssm.model.Book">
select <include refid="baseSql"/> from tb_book where true
<if test="null != bookName and '' != bookName">
and book_name like concat('%',#{bookName},'%')
</if>
order by book_id desc
</select>
<select id="querySingleBook" resultType="com.zking.ssm.model.Book">
select <include refid="baseSql"/>
from tb_book where book_id = #{bookId}
</select>
</mapper>
控制层
package com.zking.ssm.controller;
import com.zking.ssm.model.Book;
import com.zking.ssm.service.IBookService;
import com.zking.ssm.util.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 org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
@Controller
@RequestMapping("/book")
public class BookController {
@Autowired
public IBookService bookService;
@RequestMapping("/toIndex")
public String toIndex(){
return "index";
}
@RequestMapping("toBookList")
public String toBookList(){
return "book/bookList";
}
@RequestMapping("/queryPage")
public ModelAndView queryPage(Book book,HttpServletRequest request){
PageBean pageBean = new PageBean();
// pageBean.setRequest(request);
List<Book> bookList = bookService.queryPager(book, null);
ModelAndView mv = new ModelAndView();
mv.addObject("bookList",bookList);
mv.setViewName("book/bookList");
return mv;
}
//查询单个书本:修改和详情
//@PathVariable:将请求地址里面的占位符参数传到方法里面
@RequestMapping("/querySingleBook/{bookId}/{type}")
public String querySingleBook(Model model, @PathVariable("bookId") Integer bookId, @PathVariable("type") String type){
Book book = bookService.querySingleBook(bookId);
model.addAttribute("book",book);
//如果是修改,则跳转到修改页面,否则跳转到详情
if(type.equals("edit")){//编辑
return "book/editBook";
}else{//详情
return "book/bookInfo";
}
}
@RequestMapping("/update")
public String update(Book book){
bookService.update(book);
return "redirect:queryPage";
}
@RequestMapping("/delete")
public String delete(Integer bookId){
bookService.delete(bookId);
return "redirect:queryPage";
}
@RequestMapping("/insert")
public String insert(Book book){
// System.out.println("book = " + book);
bookService.insert(book);
return "redirect:queryPage";
}
@RequestMapping("/toAddBook")
public String toAddBook(Book book){
// System.out.println("book = " + book);
// bookService.insert(book);
return "book/addBook";
}
}
主界面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file="/static/comment/head.jsp"%>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>
时间戳:<%=System.currentTimeMillis()%>
</h1>
<hr/>
<a href="${path}/book/toBookList">书本展示页面</a>
<a href="${path}/book/toAddBook">书本新增页面</a>
</body>
</html>
绑定数据
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file="/static/comment/head.jsp"%>
<html>
<head>
<title>书本展示</title>
</head>
<body>
<center>
<h1>
书本展示:<%=System.currentTimeMillis()%>
</h1>
<hr/>
<form action="${path}/book/queryPage" method="post">
书本名称:<input type="text" name="bookName"/>
<button>查询</button>
</form>
<a href="${path}/book/toAddBook">书本新增页面</a>
</center>
<table border="1px" width="70%" align="center" style="text-align: center">
<tr>
<td>书本编号</td>
<td>书本名称</td>
<td>书本价格</td>
<td>操作</td>
</tr>
<%--开始绑定数据--%>
<c:forEach items="${bookList}" var="b">
<tr>
<td>${b.bookId}</td>
<td>${b.bookName}</td>
<td>${b.bookPrice}</td>
<td>
<a href="${path}/book/querySingleBook/${b.bookId}/edit">编辑</a>
<a href="${path}/book/querySingleBook?bookId=${b.bookId}&type=info">详细</a>
<a onclick="return confirm('确定要删除吗?')" href="${path}/book/delete?bookId=${b.bookId}">删除</a>
</td>
</tr>
</c:forEach>
</table>
</body>
</html>
增加
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>增加</title>
</head>
<%@include file="/static/comment/head.jsp"%>
<body>
<form action="${path}/book/insert" method="post">
书本名称:<input type="text" name="bookName" /><br/>
书本价格:<input type="text" name="bookPrice" /><br/>
<button>提交</button>
</form>
</body>
</html>
修改
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file="/static/comment/head.jsp" %>
<html>
<head>
<title>书本编辑</title>
</head>
<body>
<center>
<h1>书本编辑</h1>
<hr/>
<form action="${path}/book/update" method="post">
书本编号:<input type="text" readonly="readonly" name="bookId" value="${book.bookId}"/><br/>
书本名称:<input type="text" name="bookName" value="${book.bookName}"/><br/>
书本价格:<input type="text" name="bookPrice" value="${book.bookPrice}"/><br/>
<button>提交</button>
</form>
</center>
</body>
</html>
查看单个
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file="/static/comment/head.jsp" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<center>
<h1>书本详细</h1>
<hr/>
<form action="#" method="post">
书本编号:<input type="text" readonly="readonly" name="bookId" value="${book.bookId}"/><br/>
书本名称:<input type="text" readonly="readonly" name="bookName" value="${book.bookName}"/><br/>
书本价格:<input type="text" readonly="readonly" name="bookPrice" value="${book.bookPrice}"/><br/>
</form>
<a href="${path}/book/queryPage">返回</a>
</center>
</body>
</html>
运行结果