描述:首頁就是查詢出來的全部結果,默認就是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" οnclick="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" οnclick="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" οnclick="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" οnclick="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>