public class TypeController implements ServletContextAware {
@Autowired
private BookTypeService bookTypeService;
@Autowired
private BookinfoService bookinfoService;
private ServletContext servletContext;
@Override
public void setServletContext(ServletContext servletContext) {
this.servletContext = servletContext;
}
/**
*
* 图片回显
* @param fileImage
* @return
*/
@RequestMapping("/bbb.do")
@ResponseBody
public String imageUpload(@RequestParam("fileImage") CommonsMultipartFile fileImage) {
// 获取上传图片的位置
System.out.println("========dasdasdsadsadasd"+111);
String path = servletContext.getRealPath("/resource/upload/");
System.out.println("上传的路径为:" + path);
// 获取文件名称
String name = fileImage.getOriginalFilename();
// 创建file对象 写入
File file = new File(path, name);
try {
fileImage.getFileItem().write(file);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 将上传的图片路径以json的方式返回客户端
String imagePath = "/resource/upload/" + name;
JSONObject jsonObject = new JSONObject();
jsonObject.put("imagePath", imagePath);
// 将对象转为json格式
String json = jsonObject.toJSONString();
System.out.println("json:"+json);
return json;
}
/**
* 显示书籍类型
* @param map
* @return
*/
@RequestMapping("/goadd.do")
public String showType(ModelMap map){
List<BookType> bookTypes = bookTypeService.queryAll();
map.put("bookTypes",bookTypes);
return "add";
}
/**
* 显示书籍类型
* @param map
* @return
*/
@RequestMapping("/goinsert.do")
public String showType2( Integer id,ModelMap map,HttpServletResponse response) throws IOException {
List<BookType> bookTypes = bookTypeService.queryAll();
map.put("bookTypes",bookTypes);
BookInfo bookInfo = bookinfoService.selectByPrimaryKey(id);
map.put("bookInfo",bookInfo);
return "update";
}
/**
*
* 添加
* @param bookInfo
* @param response
*/
@RequestMapping("/addtoto.do")
public void toadd(BookInfo bookInfo,HttpServletResponse response) throws IOException {
int rows = bookinfoService.insert(bookInfo);
response.setContentType("text/html; charset=UTF-8");
PrintWriter pWriter = response.getWriter();
if (rows > 0) {
pWriter.write("<script type='text/javascript'>alert('新增成功');location.href='querybook.htm'</script>");
} else {
pWriter.write("<script type='text/javascript'>alert('新增失败');location.href='add.htm'</script>");
}
}
@RequestMapping("/query.do")
public String queryAll(@RequestParam(value = "bookTypeid",required = false,defaultValue = "0")Integer bookTypeid,
String bookName,@RequestParam(value = "borrow",required = false,defaultValue = "0")Integer isBorrow,ModelMap map,
@RequestParam(value = "now", required = false, defaultValue = "1")Integer now
){
//分页工具 设置分页
PageHelper.startPage(now,3);
//查询 书籍 返回 集合
List<BookInfo> bookInfos = bookinfoService.queryAllbook(bookTypeid, bookName, isBorrow);
//将集合与分页绑定 存到map
PageInfo<BookInfo> pageInfo=new PageInfo<BookInfo>(bookInfos);
map.put("pageInfo",pageInfo);
//获取所有图书类型显示到页面
List<BookType> bookTypes = bookTypeService.queryAll();
map.put("bookTypes",bookTypes);
//将3个属性存入map作为回显
map.put("bookTypeid",bookTypeid);
map.put("bookName",bookName);
map.put("isBorrow",isBorrow);
return "index";
}
/**
* 详情
* @param id
* @param map
* @return
*/
@RequestMapping("/select.do")
public String selectBook(Integer id,ModelMap map){
BookInfo book = bookinfoService.selectByPrimaryKey(id);
map.put("book",book);
return "Detail";
}
@RequestMapping("/delete.do")
public void delbyID(Integer id,HttpServletResponse response) throws IOException {
int rows = bookinfoService.deleteByPrimaryKey(id);
response.setContentType("text/html; charset=UTF-8");
PrintWriter pWriter = response.getWriter();
if (rows > 0) {
pWriter.write("<script type='text/javascript'>alert('删除成功');location.href='query.do'</script>");
} else {
pWriter.write("<script type='text/javascript'>alert('删除失败');location.href='query.do'</script>");
}
}
@RequestMapping("updodo.do")
public void update( BookInfo bookInfo,Integer id,HttpServletResponse response,ModelMap map) throws IOException {
int rows = bookinfoService.insert(bookInfo);
response.setContentType("text/html; charset=UTF-8");
PrintWriter pWriter = response.getWriter();
if (rows > 0) {
pWriter.write("<script type='text/javascript'>alert('修改成功');location.href='query.do'</script>");
} else {
pWriter.write("<script type='text/javascript'>alert('修改失败');location.href='query.do'</script>");
}
}
@RequestMapping("delbooks.do")
//获取bookid的集合
public void delallBook(String[] bookid,HttpServletResponse response) throws IOException {
int rows = bookinfoService.deleteAllBooks(bookid);
response.setContentType("text/html; charset=UTF-8");
PrintWriter pWriter = response.getWriter();
if (rows > 0) {
pWriter.write("<script type='text/javascript'>alert('删除成功');location.href='query.do'</script>");
} else {
pWriter.write("<script type='text/javascript'>alert('删除失败');location.href='query.do'</script>");
}
}
}
public interface BookInfoMapper {
int deleteByPrimaryKey(Integer bookId);
int insert(BookInfo record);
int insertSelective(BookInfo record);
BookInfo selectByPrimaryKey(Integer bookId);
int updateByPrimaryKeySelective(BookInfo record);
int updateByPrimaryKey(BookInfo record);
List<BookInfo> queryAllbook(@Param("id") Integer bookTypeid, @Param("bookname") String bookname, @Param("borrow") Integer borrow);
int deleteAllBooks(String[] idss);
}