book pojo
package com.bw.pojo;
import org.springframework.format.annotation.DateTimeFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Book {
private int id;
private String name;
private String content; // 内容
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date time;
private String picture;
private static final SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
@Override
public String toString() {
return "Bike{" +
"id=" + id +
", name='" + name + '\'' +
", content='" + content + '\'' +
", time=" + time +
", picture='" + picture + '\'' +
'}';
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public Date getTime() {
return time;
}
public void setTime(Date time) {
this.time = time;
}
public String getPicture() {
return picture;
}
public void setPicture(String picture) {
this.picture = picture;
}
public Book() {
}
public Book(int id, String name, String content, Date time, String picture) {
this.id = id;
this.name = name;
this.content = content;
this.time = time;
this.picture = picture;
}
}
book dao
package com.bw.dao;
import com.bw.pojo.Book;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Map;
@Repository
@Mapper
public interface IBookDao {
//图书模糊查询,
List<Book> fuzzyBook(Map<String,Object> map);
//查询数据条数
int findCount(Map<String,Object> map);
//<!--图书添加-->
void addBook(Book book);
//图书批量删除
void batchesBook(String ids);
//根据id查询
Book selBookById(int id);
//-图书修改
void updateBook(Book book);
}
book serviceimpl
package com.bw.service.Impl;
import com.bw.dao.IBookDao;
import com.bw.pojo.Book;
import com.bw.service.IBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
@Service
public class BookServiceImpl implements IBookService {
@Autowired
private IBookDao iBookDao;
public List<Book> fuzzyBook(Map<String,Object> map) {
return iBookDao.fuzzyBook(map);
}
public int findCount(Map<String, Object> map) {
return iBookDao.findCount(map);
}
public void addBook(Book book) {
iBookDao.addBook(book);
}
public Book selBookById(int id) {
return iBookDao.selBookById(id);
}
public void updateBook(Book book) {
iBookDao.updateBook(book);
}
public void batchesBook(String ids) {
System.out.println("service-ids:"+ids);
iBookDao.batchesBook(ids);
}
}
book controller
package com.bw.controller;
import com.bw.pojo.Book;
import com.bw.service.IBookService;
import com.bw.util.FileUtils;
import com.bw.util.PageUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Controller
@RequestMapping("/book")
public class BookController {
@Resource
private IBookService iBookService;
//图书模糊查询, List<Book> fuzzyBook(Map<String,Object> map);
//查询数据条数 int findCount(Map<String,Object> map);
//<!--图书添加--> void addBook(Book book);
//图书批量删除 void batchesBook(String ids);
//根据id查询 Book selBookById(int id);
//-图书修改 void updateBook(Book book);
@RequestMapping(value = "/login",method = RequestMethod.GET)
public ModelAndView login(){
ModelAndView modelAndView=new ModelAndView();
modelAndView.setViewName("login");
return modelAndView;
}
//跳转到Book添加
@RequestMapping(value = "/addBookHtml",method = RequestMethod.GET)
public ModelAndView addBookHtml(){
ModelAndView modelAndView=new ModelAndView();
modelAndView.setViewName("addBook");
return modelAndView;
}
//跳转到用户添加
@RequestMapping(value = "/addUserHtml",method = RequestMethod.GET)
public ModelAndView addUserHtml(){
ModelAndView modelAndView=new ModelAndView();
modelAndView.setViewName("addUser");
return modelAndView;
}
//图书添加
@RequestMapping(value = "/addBook",method = RequestMethod.POST)
public String addBook(@RequestParam(value = "file",required = false) MultipartFile file, Book book) throws Exception{
if (!file.isEmpty()){
String picture= FileUtils.uploadFile(file);
book.setPicture(picture);
}else {
book.setPicture("");
}
iBookService.addBook(book);
return "redirect:/book/fuzzyBook";
}
//图书模糊查询
@RequestMapping(value = "/fuzzyBook",method = RequestMethod.GET)
public ModelAndView fuzzyBook(HttpServletRequest request){
Map<String,Object> map=new HashMap<String, Object>();
ModelAndView modelAndView=new ModelAndView();
String name=request.getParameter("name");
map.put("name",name);
int count=iBookService.findCount(map);
String pageNow=request.getParameter("pageNow");
if (pageNow!=null){
PageUtils pageUtils=new PageUtils(count,Integer.parseInt(pageNow),2);
map.put("startPos",pageUtils.getStartPos());
map.put("pageSize",2);
List<Book> bookList= iBookService.fuzzyBook(map);
modelAndView.addObject("bookList",bookList);
modelAndView.addObject("pageUtils",pageUtils);
}
else {
PageUtils pageUtils=new PageUtils(count,1,2);
map.put("startPos",pageUtils.getStartPos());
map.put("pageSize",2);
List<Book> bookList= iBookService.fuzzyBook(map);
modelAndView.addObject("bookList",bookList);
modelAndView.addObject("pageUtils",pageUtils);
}
modelAndView.setViewName("show");
return modelAndView;
}
//图书批量删除
@RequestMapping(value = "/batchesBook",method = RequestMethod.GET)
@ResponseBody
public Map<String,Object> batchesBook(String ids){
Map<String,Object> map=new HashMap<String, Object>();
System.out.println("ids"+ids);
iBookService.batchesBook(ids);
map.put("ids",ids);
return map;
}
//图书根据id查询
@RequestMapping(value = "/selBookById",method = RequestMethod.GET)
@ResponseBody
public String selBookById(int id){
System.out.println("id"+id);
iBookService.selBookById(id);
return "redirect:/book/fuzzyBook";
}
/* //跳转到用户信息发送
@RequestMapping(value = "/login",method = RequestMethod.GET)
public ModelAndView login(){
ModelAndView modelAndView=new ModelAndView();
modelAndView.setViewName("show");
return modelAndView;
}*/
}
sql book
<?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.bw.dao.IBookDao">
<!--图书模糊查询-->
<select id="fuzzyBook" parameterType="Map" resultType="Book">
select id,name,picture,content,time from book where true
<if test="name!=null"> AND name LIKE concat(concat('%',#{name}),'%')</if>
limit #{startPos},#{pageSize}
</select>
<select id="findCount" parameterType="Map" resultType="int">
select count(*) from book where true
<if test="name!=null"> AND name LIKE concat(concat('%',#{name}),'%')</if>
</select>
<!--图书添加-->
<insert id="addBook" parameterType="Book">
insert into book(name,picture,time,content) values(#{name},#{picture},NOW(),#{content})
</insert>
<!--图书批量删除-->
<delete id="batchesBook" parameterType="String">
delete from book where id in(${value })
</delete>
<!--图书修改-->
<select id="selBookById" parameterType="int" resultType="Book">
select id,name,picture,content,time from book where id=#{id}
</select>
<update id="updateBook" parameterType="Book" >
</update>
</mapper>
show.jsp
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"/> <title>show</title> <script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"/> <script> //反选 function checkAll() { $("#t1 :checkbox").each(function () { $(this).prop("checked", !$(this).prop("checked")); }); $("#t1:checkbox").prop("checked",true); } $(function () { $("button").click(function(){ var checkedNum = $("input[name='features']:checked").length; if(checkedNum == 0) { alert("请选择至少一项!"); return; } if(confirm("确定要删除所选项目?")) { var checkedList=[]; $("input[name='features']:checked").each(function () { checkedList.push($(this).val()); }); ids=checkedList.toString(); $.ajax({ type:"get", url:"/book/batchesBook", data:{ ids:ids }, dataType:"json", success:function (data) { if(data.ids.length>0){ alert("恭喜你批量删除成功!"); location.href="/book/fuzzyBook"; }else{ alert("批量删除失败!"); location.reload(); } }, error:function () { alert("出现错误,撒后再试!"); } }); } }); }); </script> </head> <body> <center><h2>展示页面</h2></center> <!--<input type="button" value="批量删除" id="hide"/>--> <input type="button" value="全选/全不选" id="hide" th:onclick="'javascript:checkAll()'"/> <button >批量删除</button> <table border="1" width="100%" cellspacing="1" cellpadding="0" id="t1"> <tr> <td>复选框</td> <td>书的书号</td> <td>书的封面</td> <td>书名</td> <td>书的评价</td> <td>书的上架时间</td> <td>操作</td> </tr> <tr th:each="book:${bookList}"> <td> <input id="features2" name="features" type="checkbox" th:value="${book.id}"/></td> <td th:text="${book.id}">1</td> <td><img th:src="${book.picture}"/></td> <td th:text="${book.name}">1</td> <td th:text="${book.content}">Red Chair</td> <!--<td th:text="${'$' + #numbers.formatDecimal(book.price, 1, 2)}">$123</td>--> <td th:text="${#dates.format(book.time, 'yyyy-MM-dd')}">2014-12-01</td> <td><a th:href="@{/book/batchesBook/{ids}(ids=${id})}">删除</a> <a th:href="@{/book/batchesBook/{id}(id=${id})}">修改</a> </td> </tr> </table> <a th:if="${pageUtils.hasFirst}" th:href="@{/book/fuzzyBook(pageNow=1)}">首页</a> <a th:unless="${pageUtils.hasFirst}">首页</a> <a th:if="${pageUtils.hasFirst}" th:href="@{/book/fuzzyBook(pageNow=${pageUtils.pageNow-1})}">上一页</a> <a th:unless="${pageUtils.hasFirst}">上一页</a> <span th:text="${pageUtils.pageNow}"></span>/<span th:text="${pageUtils.totalPageCount}"></span> <a th:if="${pageUtils.hasNext}" th:href="@{/book/fuzzyBook(pageNow=${pageUtils.pageNow+1})}">下一页</a> <a th:unless="${pageUtils.hasNext}">下一页</a> <a th:if="${pageUtils.hasNext}" th:href="@{/book/fuzzyBook(pageNow=${pageUtils.totalPageCount})}">末页</a> <a th:unless="${pageUtils.hasNext}">末页</a> </body> </html>
addbook jsp
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>Title</title>
</head>
<body>
<h2>图书添加</h2>
<form action="/book/addBook" enctype="multipart/form-data" method="post">
图书封面:<input type="file" name="file" /><br/>
图书名:<input type="text" name="name" /><br/>
图书评价:<input type="text" name="content" /><br/>
<input type="submit" value="添加"/>
</form>
<a th:href="@{http://www.thymeleaf.org}">Thymeleaf</a>
</body>
</html>
login.jsp
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>Title</title>
</head>
<body>
<h2>login</h2>
<ul>
<li>
<input id="features1" name="features" type="checkbox" value="SEEDSTARTER_SPECIFIC_SUBSTRATE" />
<input name="_features" type="hidden" value="on" />
<label for="features1">Seed starter-specific substrate</label>
</li>
<li>
<input id="features2" name="features" type="checkbox" value="FERTILIZER" />
<input name="_features" type="hidden" value="on" />
<label for="features2">Fertilizer used</label>
</li>
<li>
<input id="features3" name="features" type="checkbox" value="PH_CORRECTOR" />
<input name="_features" type="hidden" value="on" />
<label for="features3">PH Corrector used</label>
</li>
</ul>
</body>
</html>