作者主页:源码空间站2022
简介:Java领域优质创作者、Java项目、学习资料、技术互助
文末获取源码
项目介绍
用户角色包含以下功能:
用户登录,查看商品详情,按分类查看,查看我的书架,上传二手书等功能。
由于本程序规模不大,可供课程设计,毕业设计学习演示之用
PS:这个没有管理员角色。
环境需要
1.运行环境:最好是java jdk 1.8,我们在这个平台上运行的。其他版本理论上也可以。
2.IDE环境:IDEA,Eclipse,Myeclipse都可以。推荐IDEA;
3.tomcat环境:Tomcat 7.x,8.x,9.x版本均可
4.硬件环境:windows 7/8/10 1G内存以上;或者 Mac OS;
5.数据库:MySql 5.7版本;
6.是否Maven项目:是;
技术栈
1. 后端:Spring+SpringMVC+Mybatis
2. 前端:JSP+CSS+JavaScript+jquery
使用说明
1. 使用Navicat或者其它工具,在mysql中创建对应名称的数据库,并导入项目的sql文件;
2. 使用IDEA/Eclipse/MyEclipse导入项目,Eclipse/MyEclipse导入时,若为maven项目请选择maven;
若为maven项目,导入成功后请执行maven clean;maven install命令,然后运行;
3. 将项目中jdbc.properties配置文件中的数据库配置改为自己的配置;
4. 运行项目,输入localhost:8080/ssm_sebook_market 登录
运行截图
相关代码
图书控制器
package com.daniel.controller;
import com.alibaba.fastjson.JSONObject;
import com.daniel.common.Result;
import com.daniel.common.ResultGenerator;
import com.daniel.pojo.Book;
import com.daniel.pojo.BookImage;
import com.daniel.pojo.Category;
import com.daniel.pojo.User;
import com.daniel.service.BookImageService;
import com.daniel.service.BookService;
import com.daniel.service.CategoryService;
import com.daniel.service.UserService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Map;
@RestController
@RequestMapping("/books")
public class BookController {
@Autowired
private UserService userService;
@Autowired
private BookService bookService;
@Autowired
private BookImageService bookImageService;
@Autowired
private CategoryService categoryService;
// 日志文件
private static final Logger log = Logger.getLogger(BookController.class);
/**
* 书本详情页
* @param id 图书ID
* @return 该ID图书的详情页
*/
@RequestMapping(value = "/{id}",method = RequestMethod.GET)
public ModelAndView getBookDetail(@PathVariable("id") String id) {
ModelAndView mav = new ModelAndView("bookDetail");
int intId = Integer.parseInt(id);
Book curBook = bookService.get(intId);
curBook.setBookImage(bookImageService.getByBookId(intId));
curBook.setUser(userService.get(bookService.getUserId(intId)));
mav.addObject("book",curBook);
return mav;
}
/**
* 上传图书
* @param request 用于获取当前用户信息
* @param book 图书实体类
* @param file 图片文件
*/
@RequestMapping(value = "",method = RequestMethod.POST)
public Result uploadSell(HttpServletRequest request,Book book,
@RequestParam(value = "image" , required = false) MultipartFile file){
// 获取当前用户的信息
User user = (User) request.getSession().getAttribute("user");
try {
if(file != null && book != null){
// 写入图书信息
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
long time = System.currentTimeMillis();
String timeStr= sdf.format(time);
book.setDate(timeStr);
book.setUser(user);
bookService.add(book);
BookImage bookImage = new BookImage();
bookImage.setBook(book);
bookImageService.add(bookImage);
String imageName = bookImage.getId()+".jpg";
String imagePath = request.getServletContext().getRealPath("/img/book-list/article/");
File filePath = new File(imagePath,imageName);
if (!filePath.getParentFile().exists()){
filePath.getParentFile().mkdir();
}
file.transferTo(new File(imagePath + File.separator + imageName));
log.info("request: book/upload , book: " + book.toString());
return ResultGenerator.genSuccessResult();
}else {
return ResultGenerator.genFailResult("信息填写不完整或未上传图片!");
}
} catch (IOException e) {
e.printStackTrace();
return ResultGenerator.genFailResult("上传失败");
}
}
/**
* 更新图书的视图
* @param id 图书的ID
* @return 该ID的图书的更新界面
*/
@RequestMapping(value = "/renewal/{id}",method = RequestMethod.GET)
public ModelAndView goEditBook(@PathVariable String id){
ModelAndView mav = new ModelAndView("editBook");
int bookId = Integer.parseInt(id);
Book curBook = bookService.get(bookId);
log.info("request: book/update , book: " + curBook.toString());
if (curBook != null){
curBook.setBookImage(bookImageService.getByBookId(bookId));
}
mav.addObject("book",curBook);
Map<Integer,String> categories = categoryService.listByMap();
mav.addObject("categories",categories);
return mav;
}
/**
* 根据ID获取书的Category
* @param book
* 应该使用GET的,但是会产生不合法URI异常,待解决
*/
@RequestMapping(value = "/categories")
public Result getCategory(@RequestBody Book book){
JSONObject data = new JSONObject();
Category category = bookService.get(book.getId()).getCategory();
log.info("request: book/category/get , bookId: " + book.getId()+" , category:"+category.toString());
if (category.getId() != 0){
data.put("categoryId",category.getId());
return ResultGenerator.genSuccessResult(data);
}else {
return ResultGenerator.genFailResult("无效的Category!");
}
}
/**
* 更新图书内容
* @param request 用于获取路径
* @param book 除图片外其他的图书信息
* @param file 图片
* @return
* 应该使用PUT,可是需要上传图片,表单提交无法用PUT,待解决
*/
@RequestMapping(value = "/renewal",method = RequestMethod.POST)
public Result editBook(HttpServletRequest request, Book book,
@RequestParam(value = "image" , required = false) MultipartFile file){
try {
bookService.update(book);
if (file != null) {
BookImage bookImage = bookImageService.getByBookId(book.getId());
bookImage.setBook(book);
bookImageService.update(bookImage);
String imageName = bookImage.getId() + ".jpg";
String imagePath = request.getServletContext().getRealPath("/img/book-list/article/");
File filePath = new File(imagePath, imageName);
if (!filePath.getParentFile().exists()) {
filePath.getParentFile().mkdir();
}else if (filePath.exists()){
filePath.delete();
}
file.transferTo(new File(imagePath + File.separator + imageName));
}
log.info("request: book/update , book: " + book.toString());
return ResultGenerator.genSuccessResult();
} catch (IOException e) {
e.printStackTrace();
return ResultGenerator.genFailResult("修改失败!");
}
}
/**
* 删除一本或多本图书
* @param request 用于获取路径,删除图片
* @param bookIds 要删除的图书ID数组
*/
@RequestMapping(value = "",method = RequestMethod.DELETE)
public Result deleteBook(HttpServletRequest request, @RequestParam(value = "bookIds", required = false) String[] bookIds){
if (bookIds != null) {
// 遍历每个ID
for (String bookId : bookIds) {
int id = Integer.parseInt(bookId);
// 获取当前图书的图片名称与存放路径
String imageName = bookImageService.getByBookId(id).getId() + ".jpg";
String imagePath = request.getServletContext().getRealPath("/img/book-list/article/");
File filePath = new File(imagePath, imageName);
// 删除图片
if (filePath.exists()){
filePath.delete();
}
// 删除数据库中的图书
bookImageService.deleteByBookId(id);
bookService.delete(id);
}
log.info("request: book/delete , bookIds: " + Arrays.toString(bookIds));
return ResultGenerator.genSuccessResult();
}else {
return ResultGenerator.genFailResult("删除失败!未选中图书");
}
}
}
如果也想学习本系统,下面领取。回复:187ssm