描述:首页就是查询出来的全部结果,默认就是controller层返回到查询全部的页面作为首页。
图片:
JSP代码:
<div id="listContainer">
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>No.</th>
<th>書名</th>
<th>著者</th>
<th>出版社</th>
<th>貸出状況</th>
<th> </th>
</tr>
</thead>
<tbody>
<c:forEach items="${cha}" var="item" varStatus="status">
<tr>
<td class="numeric">${status.index+1}</td>
<td class="text">${item.title}</td>
<td class="text">${item.author}</td>
<td class="text">${item.publisher}</td>
<td class="status">${item.status}</td>
<td class="operation">
<a href="../book/selectbyid?id=${item.id}" class="btn btn-info" >照会</a>
<a href="../book/borrow?id=1" class="btn btn-info">貸出/返却</a>
<a href="../book/updateselect?id=${item.id}" class="btn">編集</a>
<a href="../book/delete?id=${item.id}" class="btn">削除</a>
</td>
</tr>
</c:forEach>
</tbody>
</table>
<div class="buttons">
<a href="/book/tiaozhuan" class="btn">新規</a>
</div>
</div>
代码说明:页面显示结果通过EL表达式显示出来,使用<c:forEach items="${cha}" var="item" varStatus="status"></forEach>表示循环。items里面的表示controller层数据返回的别称。var里的item表示${cha}的别称。varStatus="status"表示显示的时候自动编号。${item.title} 调取内容对应着controller返回的集合中对象的属性。
controller层:
@Controller
@RequestMapping(value = "/book/*")
public class SelectAllController {
@Autowired
private SelectAllService selectallservice; //实例化service
//查询的全部的方法
@RequestMapping("/selectall")
public ModelAndView selectAll()
{
ModelAndView modelandview = new ModelAndView("book/bookList");//需要返回jsp页面的路径
List<Book> selectall = selectallservice.getSelectAll();//调用service层查询全部的方法
modelandview.addObject("cha",selectall);//把查询到的结果集给到前端界面
return modelandview;//返回
}
}
代码说明:
1.需要加@controller注解
2.需要加@Autowired注解 帮助创建实体类
3.@RequestMapping("/selectall")前端jsp需要跳转到controller层的名字
service层:
@Service
public class SelectAllService {
@Autowired
private SelectAllDao selectdao;//实例化dao
//调用dao层的查询全部方法
public List<Book> getSelectAll ()
{
return selectdao.selectall();
}
}
代码说明:
1.需要加@service注解
2.需要加@Autowired注解 帮助创建实体类
dao层:
@Mapper
//查询全部sql语句
public interface SelectAllDao {
List<Book> selectall();
}
代码说明:
1.需要加Mapper注解
2.对应建立一个xml文件
mapper.xml
<?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.smt.book.dao.SelectAllDao">
<resultMap id="SelectAll" type="com.smt.book.entity.Book">
<id property="id" column="id"></id>
<result property="title" column="title"></result>
<result property="author" column="author"></result>
<result property="publisher" column="publisher"></result>
<result property="status" column="status"></result>
<result property="summary" column="summary"></result>
<result property="releaseDate" column="release_Date"></result>
<result property="categoryId" column="category_Id"></result>
<result property="version" column="version"></result>
<association property="category" resultMap="category"></association>
</resultMap>
<resultMap id="category" type="com.smt.book.entity.Category">
<id property="id" column="id"></id>
<result property="name" column="name"></result>
<result property="version" column="version"></result>
</resultMap>
<!--查询全部-->
<select id="selectall" resultMap="SelectAll" >
select * from books b join category c on b.category_id = c.id
</select>
</mapper>
代码说明:
1.resultMap 对应的实体类 id是dao层里面的方法。type是实体类的路径
2.property对应的是类的属性名称,column对应的是数据库的字段名称
3.实体类相关联
<association property="category" resultMap="category"></association>
4.一个dao层对应一个xml文件,xml文件写sql命令语句。
mapper配置文件:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!--每次要新建一个标签,对应着mapper文件-->
<mappers>
<mapper resource="dao/books-mapping.xml"/>
<mapper resource="dao/category-mapping.xml"/>
<mapper resource="dao/selectalldao-mapper.xml"/>
</mappers>
</configuration>
描述:点击照会进入到改条数据的详情,也就是根据id查询进行查询。
图片:
JSP代码:
<div id="listContainer">
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>No.</th>
<th>書名</th>
<th>著者</th>
<th>出版社</th>
<th>貸出状況</th>
<th> </th>
</tr>
</thead>
<tbody>
<c:forEach items="${cha}" var="item" varStatus="status">
<tr>
<td class="numeric">${status.index+1}</td>
<td class="text">${item.title}</td>
<td class="text">${item.author}</td>
<td class="text">${item.publisher}</td>
<td class="status">${item.status}</td>
<td class="operation">
<a href="../book/selectbyid?id=${item.id}" class="btn btn-info" >照会</a>
<a href="../book/borrow?id=1" class="btn btn-info">貸出/返却</a>
<a href="../book/updateselect?id=${item.id}" class="btn">編集</a>
<a href="../book/delete?id=${item.id}" class="btn">削除</a>
</td>
</tr>
</c:forEach>
</tbody>
</table>
<div class="buttons">
<a href="/book/tiaozhuan" class="btn">新規</a>
</div>
</div>
代码说明:
点击照会跳转到controller层的selectbyid方法,并且传参id
JSP代码:(详情页)
<%--
Created by IntelliJ IDEA.
User: SMT-DN-1004
Date: 2019/07/04
Time: 17:47
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"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<html>
<head>
<meta charset="utf-8">
<link href="../../common/lib/bootstrap/css/bootstrap.min.css" rel="stylesheet" media="screen">
<link href="../../common/css/book-assignment-common.css" rel="stylesheet" media="screen">
<title>照会 | 図書管理システム</title>
</head>
<body>
<div id="header" class="navbar navbar-inverse navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<a class="brand" href="#">図書管理システム</a>
<div class="nav-collapse">
<ul class="nav">
<li><a href="list.html">Home</a></li>
</ul>
</div>
</div>
</div>
</div>
<div id="contents" class="container">
<h2>照会</h2>
<form id="form" method="post" class="form form-horizontal">
<table id="bookItem" class="tableDetail tableLabel">
<tr>
<th>書名</th>
<td>${chaid[0].title}</td>
</tr>
<tr>
<th>著名</th>
<td>${chaid[0].author}</td>
</tr>
<tr>
<th>出版社</th>
<td>${chaid[0].publisher}</td>
</tr>
<tr>
<th>カテゴリ</th>
<td>${chaid[0].category.name}</td>
</tr>
<tr>
<th>発売日</th>
<td><fmt:formatDate value="${chaid[0].releaseDate}" pattern="yyyy-MM-dd"/>
</td>
</tr>
<tr>
<th>貸出状況</th>
<td>${chaid[0].summary}</td>
</tr>
<tr>
<th>概要</th>
<td>${chaid[0].summary}</td>
</tr>
</table>
<button type="submit" name="backSearch" class="btn" onclick="location.href='./list.html';return false;">戻る</button>
</form>
</div>
</body>
</html>
代码说明:
JSP界面日期的显示问题可以使用<fmt:formatDate value="${chaid[0].releaseDate}" pattern="yyyy-MM-dd"/> 来解决前端日期显示的问题。
EL表达式不是循环,并且后台传来的是List集合就使用下标的形式${chaid[0].name}显示出来。
controller层:
@Controller
@RequestMapping(value = "/book/*")
public class SelectAllController {
@Autowired
private SelectAllService selectallservice;//实例化service
//按照id查询,详情页面
@RequestMapping("/selectbyid")
public ModelAndView selectById(Integer id)//传参
{
ModelAndView modelandview = new ModelAndView("book/detail");//设置jsp跳转页面
List<Book> selectbyid = selectallservice.getSelectByid(id);//按照id查询详情页面
modelandview.addObject("chaid",selectbyid);//查询到的结果给到jsp页面
return modelandview;//返回
}
}
service层:
@Service
public class SelectAllService {
@Autowired
private SelectAllDao selectdao;//实例化dao层
//调用dao层根据id查询
public List<Book> getSelectByid(Integer id)
{
return selectdao.selectbyid(id);
}
}
dao层:
@Mapper
public interface SelectAllDao {
List<Book> selectbyid(Integer id);
}
mapper.xml
<?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.smt.book.dao.SelectAllDao">
<resultMap id="SelectAll" type="com.smt.book.entity.Book">
<id property="id" column="id"></id>
<result property="title" column="title"></result>
<result property="author" column="author"></result>
<result property="publisher" column="publisher"></result>
<result property="status" column="status"></result>
<result property="summary" column="summary"></result>
<result property="releaseDate" column="release_Date"></result>
<result property="categoryId" column="category_Id"></result>
<result property="version" column="version"></result>
<association property="category" resultMap="category"></association>
</resultMap>
<resultMap id="category" type="com.smt.book.entity.Category">
<id property="id" column="id"></id>
<result property="name" column="name"></result>
<result property="version" column="version"></result>
</resultMap>
<!--根据id查询-->
<select id="selectbyid" resultMap="SelectAll">
select * from books b,category c where b.category_id = c.id and b.id=#{id}
</select>
</mapper>
描述:点击编辑进入到编辑界面,相当于根据id查询,把查询到的内容放到表单里。
点击更新确认,跳转到确认界面,相当于把JSP表单的内容提交到另一个JSP页面上。
在点击更新,把当前JSP界面的内容提交到数据库,修改完成,并返回controller查询方法。
图片:
JSP代码:
<div id="listContainer">
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>No.</th>
<th>書名</th>
<th>著者</th>
<th>出版社</th>
<th>貸出状況</th>
<th> </th>
</tr>
</thead>
<tbody>
<c:forEach items="${cha}" var="item" varStatus="status">
<tr>
<td class="numeric">${status.index+1}</td>
<td class="text">${item.title}</td>
<td class="text">${item.author}</td>
<td class="text">${item.publisher}</td>
<td class="status">${item.status}</td>
<td class="operation">
<a href="../book/selectbyid?id=${item.id}" class="btn btn-info" >照会</a>
<a href="../book/borrow?id=1" class="btn btn-info">貸出/返却</a>
<a href="../book/updateselect?id=${item.id}" class="btn">編集</a>
<a href="../book/delete?id=${item.id}" class="btn">削除</a>
</td>
</tr>
</c:forEach>
</tbody>
</table>
<div class="buttons">
<a href="/book/tiaozhuan" class="btn">新規</a>
</div>
</div>
代码说明:
点击编辑跳转到controller层的updateselect方法中,传递一个参数id。
JSP代码:
<html>
<head>
<meta charset="utf-8">
<link href="../../common/lib/bootstrap/css/bootstrap.min.css" rel="stylesheet" media="screen">
<link href="../../common/css/book-assignment-common.css" rel="stylesheet" media="screen">
<title>編集 | 図書管理システム</title>
</head>
<body>
<div id="header" class="navbar navbar-inverse navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<button type="button" class="btn btn-navbar">
<span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span>
</button>
<a class="brand" href="#">図書管理システム</a>
<div class="nav-collapse collapse">
<ul class="nav">
<li><a href="list.html">Home</a></li>
</ul>
</div>
</div>
</div>
</div>
<div id="contents" class="container">
<h2>編集</h2>
<div class="error">Error Message!</div>
<form id="form" method="post" class="form form-horizontal" action="/book/queren">
<table id="bookItem" class="tableDetail tableInput">
<input type="text" name="id" value="${upcha[0].id}" style="display:none;">
<tr>
<th><span class="required">書名</span></th>
<td><input type="text" name="title" id="bookName" class="input-block-level" placeholder="本の名前を入力してください" value="${upcha[0].title}" /></td>
</tr>
<tr>
<th>著名</th>
<td><input type="text" name="author" id="author" class="input-block-level" placeholder="著者名を入力してください" value="${upcha[0].author}" /></td>
</tr>
<tr>
<th>出版社</th>
<td><input type="text" name="publisher" id="publisher" class="input-block-level" placeholder="出版社を入力してください" value="${upcha[0].publisher}" /></td>
</tr>
<tr>
<th>カテゴリ</th>
<td><select id="category" name="categoryId" >
<option>-- 選択してください --</option>
<c:forEach items="${cate}" var="aa">
<option value="${aa.id}"> ${aa.name} </option>
</c:forEach>
</select></td>
</tr>
<tr>
<th>発売日</th>
<td><input type="text" name="releaseDate" id="releaseDate" class="input-block-level" value="<fmt:formatDate value="${upcha[0].releaseDate}" pattern="yyyy-MM-dd"/>" /></td>
</tr>
<tr>
<th>概要</th>
<td><textarea rows="5" name="summary" id="summary" class="input-block-level" value="${upcha[0].summary}">${upcha[0].summary}</textarea></td>
</tr>
</table>
<button type="submit" name="backSearch" class="btn" onclick="location.href='/book/selectall';return false;">戻る</button>
<button type="submit" name="goUpdateConfirm" class="btn btn-info" >更新確認</button>
</form>
</div>
</body>
</html>
代码说明:下拉列表的位置显示的是分类名称,但是value传递的是categoryID。name的名字要跟实体类的属性名一致,这样会自动创建实体类。
JSP代码:
<html>
<head>
<meta charset="utf-8">
<link href="../../common/lib/bootstrap/css/bootstrap.min.css" rel="stylesheet" media="screen">
<link href="../../common/css/book-assignment-common.css" rel="stylesheet" media="screen">
<title>編集確認 | 図書管理システム</title>
</head>
<body>
<div id="header" class="navbar navbar-inverse navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<button type="button" class="btn btn-navbar">
<span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span>
</button>
<a class="brand" href="#">図書管理システム</a>
<div class="nav-collapse collapse">
<ul class="nav">
<li><a href="list.html">Home</a></li>
</ul>
</div>
</div>
</div>
</div>
<div id="contents" class="container">
<h2>編集確認</h2>
<form id="form" method="post" class="form form-horizontal" action="/book/update">
<table id="bookItem" class="tableDetail tableLabel">
<input type="text" name="id" value="${selectall.id}" style="display:none;">
<tr>
<th>書名</th>
<td>${selectall.title}</td> <input type="text" name="title" value="${selectall.title}" style="display:none;">
</tr>
<tr>
<th>著名</th>
<td>${selectall.author}</td> <input type="text" name="author" value="${selectall.author}" style="display:none;">
</tr>
<tr>
<th>出版社</th>
<td>${selectall.publisher}</td> <input type="text" name="publisher" value="${selectall.publisher}" style="display:none;">
</tr>
<tr>
<th>カテゴリ</th>
<td>${categoryresult[0].name}</td><input type="text" name="categoryId" value="${categoryresult[0].id}" style="display:none;">
</tr>
<tr>
<th>発売日</th>
<td><fmt:formatDate value="${selectall.releaseDate}" pattern="yyyy-MM-dd"/></td><input type="text" name="releaseDate" value="<fmt:formatDate value="${selectall.releaseDate}" pattern="yyyy-MM-dd"/>" style="display:none;">
</tr>
<tr>
<th>概要</th>
<td>${selectall.summary}</td><input type="text" name="summary" value="${selectall.summary}" style="display:none;">
</tr>
</table>
<button type="submit" name="backUpdate" class="btn" onclick="location.href='/book/selectall';return false;">戻る</button>
<button type="submit" name="doUpdate" class="btn btn-info" >更新</button>
</form>
</div>
</body>
</html>
代码说明:分类内容使用controller查询出来的结果传到JSP,EL表达式。
controller层:
@Controller
@RequestMapping(value = "/book/*")
public class SelectAllController {
@Autowired
private SelectAllService selectallservice;//实例化service
//按照id查询信息放到编辑页面
@RequestMapping("/updateselect")
public ModelAndView updateSelect(Integer id)//传参
{
ModelAndView modelandview = new ModelAndView("book/update");//要跳转到的jsp页面
List<Book> updateSelect = selectallservice.updateSelect(id);//查询book的所有内容
List<Category> selectCategory = selectallservice.selectCategory();//查询分类的全部内容
modelandview.addObject("upcha", updateSelect);//返回前端JSP
modelandview.addObject("cate",selectCategory);//返回前端JSP
return modelandview;//返回
}
//确认更新,获取jsp传到jsp
@RequestMapping("/queren")
public ModelAndView queren(Book book,HttpServletRequest request,HttpServletResponse response)//把JSP的内容封装成实体类 作为参数传递
// public String queren(HttpServletRequest request,HttpServletResponse response)
{
ModelAndView modelandview = new ModelAndView("book/updateConfirm");
modelandview.addObject("selectall",book);//查询到的book类结果返回给JSP
Category category = new Category();//实例化category
List<Category> categoryResult = selectallservice.selectCategoryByIdService(book.getCategoryId());//根据编号查询category的结果
modelandview.addObject("categoryresult",categoryResult);//查询到的category的结果给到jsp页面
return modelandview;//返回
// String bid =request.getParameter("id");
// String title = request.getParameter("title");
// String author = request.getParameter("author");
// String publisher = request.getParameter("publishern");
// String category = request.getParameter("category");
// String releasedate = request.getParameter("releaseDate");
// String summar = request.getParameter("summary");
//
//
// request.setAttribute("xtitle",title);
// request.setAttribute("xauthor",author);
// request.setAttribute("xpublisher",publisher);
// request.setAttribute("xcategor",category);
// request.setAttribute("xreleasedate",releasedate);
// request.setAttribute("xsummar",sumar);
// request.setAttribute("bid",bid);
/*return "book/updateConfirm";*/
}
//修改,根据jsp传来的内容进行修改
@RequestMapping("/update")
public String updateById(Book book)//前端的JSP生成book对象作为参数
{
Integer updat =selectallservice.updateById(book);//调用修改的方法
return "redirect:/book/selectall";//重定向返回controller的selectall方法
}
}
services层:
@Service
public class SelectAllService {
//根据id查询全部
public List<Book> updateSelect(Integer id){return selectdao.updateSelect(id);}
//查询全部分类
public List<Category> selectCategory(){return selectdao.selectCategory();}
//根据编号查找分类
public List<Category> selectCategoryByIdService(Integer id){
return selectdao.selectCategoryByIdDao(id);
}
//修改方法
public Integer updateById(Book b){return selectdao.updateById(b);}
}
dao层:
@Mapper
public interface SelectAllDao {
List<Book> updateSelect(Integer id);
List<Category> selectCategory();
Integer updateById (Book book);
List<Category> selectCategoryByIdDao(Integer id);
}
mapper.xml
<?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.smt.book.dao.SelectAllDao">
<resultMap id="SelectAll" type="com.smt.book.entity.Book">
<id property="id" column="id"></id>
<result property="title" column="title"></result>
<result property="author" column="author"></result>
<result property="publisher" column="publisher"></result>
<result property="status" column="status"></result>
<result property="summary" column="summary"></result>
<result property="releaseDate" column="release_Date"></result>
<result property="categoryId" column="category_Id"></result>
<result property="version" column="version"></result>
<association property="category" resultMap="category"></association>
</resultMap>
<resultMap id="category" type="com.smt.book.entity.Category">
<id property="id" column="id"></id>
<result property="name" column="name"></result>
<result property="version" column="version"></result>
</resultMap>
<!--根据id查询内容-->
<select id="updateSelect" resultMap="SelectAll">
select * from books b,category c where b.category_id = c.id and b.id=#{id}
</select>
<!--查询全部分类-->
<select id="selectCategory" resultMap="category">
select * from category
</select>
<!--按照分类的id查询分类内容-->
<select id="selectCategoryByIdDao" resultMap="category">
select * from category where id=#{id}
</select>
<!--修改-->
<update id="updateById" parameterType="com.smt.book.entity.Book">
UPDATE books s
SET s.title = #{title},
s.AUTHOR = #{author},
s.PUBLISHER = #{publisher},
s.CATEGORY_ID = #{categoryId},
s.RELEASE_DATE = #{releaseDate},
s.SUMMARY = #{summary}
WHERE
s.id = #{id}
</update>
</mapper>
描述:点击新规插入一条数据,表单输入内容后点击新增,加入到数据库中,在跳转到controller层的selectall方法。
图片:
JSP代码:
<div id="listContainer">
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>No.</th>
<th>書名</th>
<th>著者</th>
<th>出版社</th>
<th>貸出状況</th>
<th> </th>
</tr>
</thead>
<tbody>
<c:forEach items="${cha}" var="item" varStatus="status">
<tr>
<td class="numeric">${status.index+1}</td>
<td class="text">${item.title}</td>
<td class="text">${item.author}</td>
<td class="text">${item.publisher}</td>
<td class="status">${item.status}</td>
<td class="operation">
<a href="../book/selectbyid?id=${item.id}" class="btn btn-info" >照会</a>
<a href="../book/borrow?id=1" class="btn btn-info">貸出/返却</a>
<a href="../book/updateselect?id=${item.id}" class="btn">編集</a>
<a href="../book/delete?id=${item.id}" class="btn">削除</a>
</td>
</tr>
</c:forEach>
</tbody>
</table>
<div class="buttons">
<a href="/book/tiaozhuan" class="btn">新規</a>
</div>
</div>
JSP代码:
<html>
<head>
<meta charset="utf-8">
<link href="../../common/lib/bootstrap/css/bootstrap.min.css" rel="stylesheet" media="screen">
<link href="../../common/css/book-assignment-common.css" rel="stylesheet" media="screen">
<title>新增</title>
</head>
<body>
<div id="header" class="navbar navbar-inverse navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<button type="button" class="btn btn-navbar">
<span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span>
</button>
<a class="brand" href="#">図書管理システム</a>
<div class="nav-collapse collapse">
<ul class="nav">
<li><a href="list.html">Home</a></li>
</ul>
</div>
</div>
</div>
</div>
<div id="contents" class="container">
<h2>新增</h2>
<div class="error">Error Message!</div>
<form id="form" method="post" class="form form-horizontal" action="/book/insert">
<table id="bookItem" class="tableDetail tableInput">
<tr>
<th><span class="required">書名</span></th>
<td><input type="text" name="title" id="bookName" class="input-block-level" placeholder="本の名前を入力してください" /></td>
</tr>
<tr>
<th>著名</th>
<td><input type="text" name="author" id="author" class="input-block-level" placeholder="著者名を入力してください" /></td>
</tr>
<tr>
<th>出版社</th>
<td><input type="text" name="publisher" id="publisher" class="input-block-level" placeholder="出版社を入力してください" /></td>
</tr>
<tr>
<th>カテゴリ</th>
<td><select id="category" name="categoryId">
<option>-- 選択してください --</option>
<c:forEach items="${opt}" var="aa">
<option value="${aa.id}"> ${aa.name} </option>
</c:forEach>
</select></td>
</tr>
<tr>
<th>発売日</th>
<td><input type="text" name="releaseDate" id="releaseDate" class="input-block-level" placeholder="2013/12/11" ></td>
</tr>
<tr>
<th>概要</th>
<td><textarea rows="5" name="summary" id="summary" class="input-block-level"></textarea></td>
</tr>
</table>
<button type="submit" name="backSearch" class="btn" onclick="location.href='/book/selectall';return false;">返回</button>
<button type="submit" name="goUpdateConfirm" class="btn btn-info" >新增</button>
</form>
</div>
</body>
</html>
controller层:
@Controller
@RequestMapping(value = "/book/*")
public class SelectAllController {
@Autowired
//查询全部的service层
private SelectAllService selectallservice;
//跳转jsp页面
@RequestMapping("tiaozhuan")
public ModelAndView tiaoZhuan()
{
ModelAndView modelandview = new ModelAndView("book/insert");
List<Category> see = selectallservice.selectCategory();
modelandview.addObject("opt",see);
return modelandview;
}
//新增内容
@RequestMapping("/insert")
public String insert(Book book)
{
Integer insert = selectallservice.insertAll(book);
return "redirect:/book/selectall";
}
}
service层:
@Service
public class SelectAllService {
@Autowired
private SelectAllDao selectdao;
//调用新增的dao层
public Integer insertAll(Book book){return selectdao.insertall(book);}
}
dao层:
@Mapper
public interface SelectAllDao {
Integer insertall(Book book);
}
mapper.xml
<?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.smt.book.dao.SelectAllDao">
<resultMap id="SelectAll" type="com.smt.book.entity.Book">
<id property="id" column="id"></id>
<result property="title" column="title"></result>
<result property="author" column="author"></result>
<result property="publisher" column="publisher"></result>
<result property="status" column="status"></result>
<result property="summary" column="summary"></result>
<result property="releaseDate" column="release_Date"></result>
<result property="categoryId" column="category_Id"></result>
<result property="version" column="version"></result>
<association property="category" resultMap="category"></association>
</resultMap>
<resultMap id="category" type="com.smt.book.entity.Category">
<id property="id" column="id"></id>
<result property="name" column="name"></result>
<result property="version" column="version"></result>
</resultMap>
<!--添加一个新的内容-->
<insert id="insertall" parameterType="com.smt.book.entity.Book">
insert into books
select max(id)+1 as id,
#{title},
#{author},
#{publisher},
1,
#{releaseDate},
#{summary},
#{categoryId},
2
from books
</insert>
}
描述:点击删除,删掉该条记录,根据id删除内容
图片:
JSP代码:
<div id="listContainer">
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>No.</th>
<th>書名</th>
<th>著者</th>
<th>出版社</th>
<th>貸出状況</th>
<th> </th>
</tr>
</thead>
<tbody>
<c:forEach items="${cha}" var="item" varStatus="status">
<tr>
<td class="numeric">${status.index+1}</td>
<td class="text">${item.title}</td>
<td class="text">${item.author}</td>
<td class="text">${item.publisher}</td>
<td class="status">${item.status}</td>
<td class="operation">
<a href="../book/selectbyid?id=${item.id}" class="btn btn-info" >照会</a>
<a href="../book/borrow?id=1" class="btn btn-info">貸出/返却</a>
<a href="../book/updateselect?id=${item.id}" class="btn">編集</a>
<a href="../book/delete?id=${item.id}" class="btn">削除</a>
</td>
</tr>
</c:forEach>
</tbody>
</table>
<div class="buttons">
<a href="/book/tiaozhuan" class="btn">新規</a>
</div>
</div>
controller层:
@Controller
@RequestMapping(value = "/book/*")
public class SelectAllController {
@Autowired
//查询全部的service层
private SelectAllService selectallservice;
//删除内容
@RequestMapping("delete")
public String deleAll(Integer id)
{
Integer dele = selectallservice.deleAll(id);
return "redirect:/book/selectall";
}
}
service层:
@Service
public class SelectAllService {
//调用删除的dao层
public Integer deleAll(Integer id){return selectdao.deleAll(id);}
}
dao层:
@Mapper
public interface SelectAllDao {
Integer deleAll(Integer id);
}
mapper.xml
<?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.smt.book.dao.SelectAllDao">
<resultMap id="SelectAll" type="com.smt.book.entity.Book">
<id property="id" column="id"></id>
<result property="title" column="title"></result>
<result property="author" column="author"></result>
<result property="publisher" column="publisher"></result>
<result property="status" column="status"></result>
<result property="summary" column="summary"></result>
<result property="releaseDate" column="release_Date"></result>
<result property="categoryId" column="category_Id"></result>
<result property="version" column="version"></result>
<association property="category" resultMap="category"></association>
</resultMap>
<resultMap id="category" type="com.smt.book.entity.Category">
<id property="id" column="id"></id>
<result property="name" column="name"></result>
<result property="version" column="version"></result>
</resultMap>
<!--删除一条记录-->
<delete id="deleAll">
DELETE FROM books WHERE id = #{id}
</delete>
</mapper>