基于javaweb+mysql的springboot+mybatis网上书店管理系统在线购书系统(前台、后台)(javaweb+springboot+thymeleaf+html+mysql)
运行环境
Java≥8、MySQL≥5.7
开发工具
eclipse/idea/myeclipse/sts等均可配置运行
适用
课程设计,大作业,毕业设计,项目练习,学习演示等
功能说明
前台:购书
后台:管理书、用户、订单发货等
前台
后台
技术框架
JavaWeb SpringBoot MyBatis Thymeleaf html layui JavaScript SpringMVC MySQL
基于javaweb+mysql的SpringBoot+MyBatis网上书店管理系统在线购书系统(前台、后台)(javaweb+springboot+thymeleaf+html+mysql)
order.setUserId(user.getUserId());
orderHandleService.createOrder(order);
return new ResultVO(ResultCode.SUCCESS,"/"+user.getUserName()+"/orders");
}
/**
* 更新订单信息,比如审核通过
* @param order
* @return
*/
@PutMapping("/list/{orderId}")
public ResultVO updateOrder(@PathVariable("orderId") Integer orderId,@RequestBody Order order) {
Order orders1 = orderService.selectByOrderId(orderId);
if(orders1!=null)
{
order.setOrderId(orderId);
int orders = orderService.updateByOrderId(order);
return new ResultVO(ResultCode.SUCCESS,null);
}
else
return new ResultVO(ResultCode.RECORD_NOT_FOUND,null);
}
/**
* 删除指定orderId的订单
* @param orderId
* @return
*/
@DeleteMapping("/list/{orderId}")
public ResultVO deleteOrder(@PathVariable("orderId") Integer orderId) {
int orders = orderService.deleteByOrderId(orderId);
return new ResultVO(ResultCode.SUCCESS,null);
}
/* @GetMapping("/list/user_order/{userId}")
public ResultVO getOrderByUserId(@PathVariable("userId") Integer userId) {
List<Order> orders = orderService.selectByUserId(userId);
if(orders.size()!=0)
{
return new ResultVO(ResultCode.SUCCESS,orders);
}
else
return new ResultVO(ResultCode.RECORD_NOT_FOUND,null);
}*/
@GetMapping({"/","/book_manage"})
public String toBookManage(){
return "admin/books";
}
//跳转分类管理页面
@GetMapping("/category_manage")
public String toCategoryManage(){
return "admin/category";
}
//跳转订单管理页面
@GetMapping("/order_manage")
public String toOrderManage(){
return "admin/order";
}
//跳转添加书籍页面
@GetMapping("/add_book")
public String AddBook(){
return "admin/add_book";
}
//跳转后台登录页面
@GetMapping("/login")
public String toAdminLogin(){
return "admin/login";
}
}
/**
* @Description: 登录验证拦截器
*/
public class ClientLoginInterceptor implements HandlerInterceptor {
/**
* 拦截请求判断是否已经登录
* @param request
session.removeAttribute("user");
return "redirect:/login";
}
/**
* 后台管理退出
* @param session
* @return
*/
@GetMapping("/admin/logout")
public String adminLogout(HttpSession session){
session.removeAttribute("admin");
return "redirect:/";
}
@GetMapping("/checkLoggedIn")
@ResponseBody
public ResultVO checkUserIsLoggedIn(HttpSession session) {
if (session.getAttribute("user") == null) {
throw new CustomizeException(ResultCode.USER_NOT_LOGGED_IN);
}
return new ResultVO(ResultCode.SUCCESS);
}
}
/**
* @Description: 图片上传控制器
*/
@RestController
@RequestMapping("/upload")
public class ImageUploadController {
@PostMapping("/book_image")
public ResultVO uploadBookImage(MultipartFile bookImage, HttpServletRequest request) {
if(quantity<=0){
throw new CustomizeException(ResultCode.FAILED,"购物数量必须大于0");
}
if(quantity>10){
throw new CustomizeException(ResultCode.FAILED,"每件商品限购10件");
}
ShoppingCart cart = new ShoppingCart();
cart.setCartId(cartId);
cart.setQuantity(quantity);
shoppingCartService.updateShoppingCart(cart);
return new ResultVO(ResultCode.SUCCESS);
}
/**
* 添加到购物车请求处理
*
* @param cart 接收前台传来的参数
* @return
*/
@PostMapping("/list")
public ResultVO addToShoppingCart(@Valid ShoppingCart cart, HttpSession session) {
User user = (User) session.getAttribute("user");
cart.setUserId(user.getUserId());
shoppingCartService.addToShoppingCart(cart);
return new ResultVO(ResultCode.SUCCESS);
}
/**
* 删除前台传来的cartId的购物车项
* @param cartId
* @return
*/
@DeleteMapping("/list/{cartId}")
public ResultVO deleteCartItem(@PathVariable("cartId") Integer cartId) {
shoppingCartService.deleteShoppingCartByCartId(cartId);
return new ResultVO(ResultCode.SUCCESS);
}
/**
* 批量删除购物车
* @param cartIds
* @return
*/
@DeleteMapping("/list")
public ResultVO deleteCartItem(@RequestBody int[] cartIds) {
shoppingCartService.deleteShoppingCarts(cartIds);
/**
* @Description: 用户管理模块控制器
*/
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
/**
* 分页获取用户列表
* @param page
* @param limit
* @return
*/
@GetMapping("/list")
public ResultVO getUserList(@RequestParam(required = false) Integer page, @RequestParam(required = false) Integer limit) {
List<User> users= userService.selectAll(page,limit);
PageInfo pageInfo = new PageInfo(users);
return new ResultVO(ResultCode.SUCCESS,(int)pageInfo.getTotal(),users);
}
/**
* 删除指定用户ID
* @param userId
* @return
*/
@DeleteMapping("/list/{userId}")
public ResultVO deleteUser(@PathVariable("userId") Integer userId) {
int users =userService.deleteByUserId(userId);
return new ResultVO(ResultCode.SUCCESS,null);
/**
* 根据用户ID,获取用户订单
* @return
*/
@GetMapping("/orders")
@ResponseBody
public ResultVO getUserOrders(Integer page, Integer limit, HttpSession session) {
User user = (User) session.getAttribute("user");
List<Order> orders = orderHandleService.getOrdersByUserId(user.getUserId(), page, limit);
PageInfo pageInfo = new PageInfo(orders);
return new ResultVO(ResultCode.SUCCESS, (int)pageInfo.getTotal(),orders);
}
/**
* 删除指定orderId的订单
* @param orderId
* @return
*/
@DeleteMapping("/orders/{orderId}")
@ResponseBody
public ResultVO deleteOrder(@PathVariable("orderId") Integer orderId) {
int orders = orderHandleService.deleteOrderById(orderId);
return new ResultVO(ResultCode.SUCCESS,null);
}
}
/**
* @Description: 后台页面的路由跳转
*/
@Controller
@RequestMapping("/admin")
public class AdminRouterController {
//跳转用户管理页面
@GetMapping("/user_manage")
public String toUserManage(){
return "admin/user";
}
PageInfo pageInfo = new PageInfo(orders);
return new ResultVO(ResultCode.SUCCESS,(int)pageInfo.getTotal(),orders);
}
/**
* 用户提交订单处理
* @param order
* @param session
* @return
*/
@PostMapping("/submit")
public ResultVO orderSubmit(@RequestBody @Valid Order order, HttpSession session) {
User user = (User) session.getAttribute("user");
order.setUserId(user.getUserId());
orderHandleService.createOrder(order);
return new ResultVO(ResultCode.SUCCESS,"/"+user.getUserName()+"/orders");
}
/**
* 更新订单信息,比如审核通过
* @param order
* @return
*/
@PutMapping("/list/{orderId}")
public ResultVO updateOrder(@PathVariable("orderId") Integer orderId,@RequestBody Order order) {
Order orders1 = orderService.selectByOrderId(orderId);
if(orders1!=null)
{
order.setOrderId(orderId);
int orders = orderService.updateByOrderId(order);
return new ResultVO(ResultCode.SUCCESS,null);
}
else
return new ResultVO(ResultCode.RECORD_NOT_FOUND,null);
}
/**
* 删除指定orderId的订单
* @param orderId
* @return
*/
@DeleteMapping("/list/{orderId}")
/**
* @Description: 登录注册模块实现类
*/
@Service
public class LoginRegisterServiceImpl implements LoginRegisterService {
@Resource
private UserMapper userMapper;
/**
* 用户登录处理
* @param record
* @param session
*/
@Override
public void userLogin(User record, HttpSession session) {
User user=userMapper.selectByUserName(record.getUserName());
if(user==null){
//用户不存在
throw new CustomizeException(ResultCode.USER_NOT_FOUND);
}
if(!user.getPassword().equals(record.getPassword())){
//密码错误
throw new CustomizeException(ResultCode.PASSWORD_ERROR);
}
session.setAttribute("user",user);
}
/**
* 用户注册处理
* @param record
*/
@Override
public void userRegister(User record) {
User user=userMapper.selectByUserName(record.getUserName());
if(user!=null){
throw new CustomizeException(ResultCode.FAILED,"用户名已存在");
}
userMapper.insert(record);
}
@Override
public void adminLogin(Admin admin, HttpSession session) {
* 用户注册处理
* @param record
*/
@Override
public void userRegister(User record) {
User user=userMapper.selectByUserName(record.getUserName());
if(user!=null){
throw new CustomizeException(ResultCode.FAILED,"用户名已存在");
}
userMapper.insert(record);
}
@Override
public void adminLogin(Admin admin, HttpSession session) {
}
}
/**
* @Description: 用户中心模块
*/
@Controller
@RequestMapping("/user_center")
public class UserCenterController {
@GetMapping("/searchid")
public ResultVO bookSearchByBookId(Integer bookId) {
// Book books=bookMapper.selectByBookId(bookIde);
Book books = bookService.bookSearchById(bookId);
if (books != null)
return new ResultVO(ResultCode.SUCCESS,books);
else
return new ResultVO(ResultCode.RECORD_NOT_FOUND, null);
}
@PostMapping("/delete")
public ResultVO bookDelete(Integer bookId) {
int books = bookService.bookDeleteSearchById(bookId);
return new ResultVO(ResultCode.SUCCESS, null);
}
//添加验证注解
@PostMapping("/insert")
public ResultVO bookInsert(@Valid Book record) {
int books = bookService.bookInsert(record);
return new ResultVO(ResultCode.SUCCESS, null);
}
@GetMapping("/searchcode")
public ResultVO bookSerchByCategoryCode(String catrgoryCode,Integer page,Integer limit) {
List<Book> books = bookService.bookSearchByCode(catrgoryCode,page,limit);
PageInfo pageInfo = new PageInfo(books);
if(books.size()!=0)
{
return new ResultVO(ResultCode.SUCCESS,(int)pageInfo.getTotal(), books);
}
else
return new ResultVO(ResultCode.RECORD_NOT_FOUND, null);
}
//添加验证注解
@PostMapping("/update")
*/
@GetMapping("/orders")
@ResponseBody
public ResultVO getUserOrders(Integer page, Integer limit, HttpSession session) {
User user = (User) session.getAttribute("user");
List<Order> orders = orderHandleService.getOrdersByUserId(user.getUserId(), page, limit);
PageInfo pageInfo = new PageInfo(orders);
return new ResultVO(ResultCode.SUCCESS, (int)pageInfo.getTotal(),orders);
}
/**
* 删除指定orderId的订单
* @param orderId
* @return
*/
@DeleteMapping("/orders/{orderId}")
@ResponseBody
public ResultVO deleteOrder(@PathVariable("orderId") Integer orderId) {
int orders = orderHandleService.deleteOrderById(orderId);
return new ResultVO(ResultCode.SUCCESS,null);
}
}
/**
* @Description: 后台页面的路由跳转
*/
@Controller
@RequestMapping("/admin")
public class AdminRouterController {
//跳转用户管理页面
@GetMapping("/user_manage")
public String toUserManage(){
return "admin/user";
}
//跳转书籍管理页面
@GetMapping({"/","/book_manage"})
public String toBookManage(){
return "admin/books";
/**
* @Description: 管理员访问拦截器
*/
public class AdminInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
HttpSession session = request.getSession();
if (session.getAttribute("admin")==null) {
//用户未登录,抛出异常
throw new CustomizeException(ResultCode.USER_NOT_LOGGED_IN);
}
Admin admin = (Admin) session.getAttribute("admin");
//已登录,放行请求
return true;
}
}
/**
* @Description: 登录注册模块业务接口
*/
public interface LoginRegisterService {
/**
* 用户登录
* @param record
* @param session
*/
void userLogin(User record, HttpSession session);
/**
* 用户注册
@Autowired
private BookService bookService;
@GetMapping("/searchid")
public ResultVO bookSearchByBookId(Integer bookId) {
// Book books=bookMapper.selectByBookId(bookIde);
Book books = bookService.bookSearchById(bookId);
if (books != null)
return new ResultVO(ResultCode.SUCCESS,books);
else
return new ResultVO(ResultCode.RECORD_NOT_FOUND, null);
}
@PostMapping("/delete")
public ResultVO bookDelete(Integer bookId) {
int books = bookService.bookDeleteSearchById(bookId);
return new ResultVO(ResultCode.SUCCESS, null);
}
//添加验证注解
@PostMapping("/insert")
public ResultVO bookInsert(@Valid Book record) {
int books = bookService.bookInsert(record);
return new ResultVO(ResultCode.SUCCESS, null);
}
@GetMapping("/searchcode")
public ResultVO bookSerchByCategoryCode(String catrgoryCode,Integer page,Integer limit) {
List<Book> books = bookService.bookSearchByCode(catrgoryCode,page,limit);
PageInfo pageInfo = new PageInfo(books);
if(books.size()!=0)
{
return new ResultVO(ResultCode.SUCCESS,(int)pageInfo.getTotal(), books);
}
else
return new ResultVO(ResultCode.RECORD_NOT_FOUND, null);
}
//添加验证注解
@PostMapping("/update")
public ResultVO bookUpdate(@Valid Book record) {
int books = bookService.bookUpdate(record);
*/
@Override
public void userRegister(User record) {
User user=userMapper.selectByUserName(record.getUserName());
if(user!=null){
throw new CustomizeException(ResultCode.FAILED,"用户名已存在");
}
userMapper.insert(record);
}
@Override
public void adminLogin(Admin admin, HttpSession session) {
}
}
/**
* @Description: 用户中心模块
*/
@Controller
@RequestMapping("/user_center")
public class UserCenterController {
@Autowired
@GetMapping("/list")
public ResultVO getOrderList(@RequestParam(required = false) Integer page, @RequestParam(required = false) Integer limit) {
List<Order> orders = orderHandleService.getAllOrdersByPage(page, limit);
PageInfo pageInfo = new PageInfo(orders);
return new ResultVO(ResultCode.SUCCESS,(int)pageInfo.getTotal(),orders);
}
/**
* 用户提交订单处理
* @param order
* @param session
* @return
*/
@PostMapping("/submit")
public ResultVO orderSubmit(@RequestBody @Valid Order order, HttpSession session) {
User user = (User) session.getAttribute("user");
order.setUserId(user.getUserId());
orderHandleService.createOrder(order);
return new ResultVO(ResultCode.SUCCESS,"/"+user.getUserName()+"/orders");
}
/**
* 更新订单信息,比如审核通过
* @param order
* @return
*/
@PutMapping("/list/{orderId}")
public ResultVO updateOrder(@PathVariable("orderId") Integer orderId,@RequestBody Order order) {
Order orders1 = orderService.selectByOrderId(orderId);
if(orders1!=null)
{
order.setOrderId(orderId);
int orders = orderService.updateByOrderId(order);
return new ResultVO(ResultCode.SUCCESS,null);
}
else
return new ResultVO(ResultCode.RECORD_NOT_FOUND,null);
}
/**
* 删除指定orderId的订单
* @param orderId
* @return
*/
@DeleteMapping("/list/{orderId}")
public ResultVO deleteOrder(@PathVariable("orderId") Integer orderId) {
int orders = orderService.deleteByOrderId(orderId);
return new ResultVO(ResultCode.SUCCESS,null);
}
/* @GetMapping("/list/user_order/{userId}")
public ResultVO getOrderByUserId(@PathVariable("userId") Integer userId) {
@ExceptionHandler(CustomizeException.class)
public ResultVO customizeExceptionHandler(CustomizeException e){
log.error(e.getMessage());
return new ResultVO(e.getCode(),e.getMsg(),null);
}
/**
* 参数校验异常处理和数据绑定异常处理
* @param e
* @return
*/
@ExceptionHandler(value = {MethodArgumentNotValidException.class, BindException.class})
public ResultVO methodArgumentNotValidExceptionHandler(Exception e) {
BindingResult bindingResult;
if(e instanceof MethodArgumentNotValidException){
MethodArgumentNotValidException ex= (MethodArgumentNotValidException)e;
bindingResult = ex.getBindingResult();
}else {
BindException ex=(BindException)e;
bindingResult = ex.getBindingResult();
}
ObjectError error = bindingResult.getAllErrors().get(0);
return new ResultVO(ResultCode.ARGUMENT_NOT_VALID,error.getDefaultMessage(),null);
}
/**
* 其他异常处理
* @param e
* @return
*/
@ExceptionHandler(Exception.class)
public ResultVO ExceptionHandler(Exception e){
log.error(e.getMessage());
return new ResultVO(ResultCode.UNKNOWN_ERROR,e.getMessage());
}
}
/**
* @Description: 客户端路由控制器
*/
@Controller
public class ClientRouterController {
* @param page 页码
* @param limit 每页的条数
* @return
*/
@GetMapping("/list")
public ResultVO getOrderList(@RequestParam(required = false) Integer page, @RequestParam(required = false) Integer limit) {
List<Order> orders = orderHandleService.getAllOrdersByPage(page, limit);
PageInfo pageInfo = new PageInfo(orders);
return new ResultVO(ResultCode.SUCCESS,(int)pageInfo.getTotal(),orders);
}
/**
* 用户提交订单处理
* @param order
* @param session
* @return
*/
@PostMapping("/submit")
public ResultVO orderSubmit(@RequestBody @Valid Order order, HttpSession session) {
User user = (User) session.getAttribute("user");
order.setUserId(user.getUserId());
orderHandleService.createOrder(order);
return new ResultVO(ResultCode.SUCCESS,"/"+user.getUserName()+"/orders");
}
/**
* 更新订单信息,比如审核通过
* @param order
* @return
*/
@PutMapping("/list/{orderId}")
public ResultVO updateOrder(@PathVariable("orderId") Integer orderId,@RequestBody Order order) {
Order orders1 = orderService.selectByOrderId(orderId);
if(orders1!=null)
{
order.setOrderId(orderId);
int orders = orderService.updateByOrderId(order);
return new ResultVO(ResultCode.SUCCESS,null);
}
else
return new ResultVO(ResultCode.RECORD_NOT_FOUND,null);
}
/**
* 删除指定orderId的订单
* @param orderId
* @return
int categories = categoryService.updateByCategoryCode(record);
return new ResultVO(ResultCode.SUCCESS,null);
}
//添加分页
@GetMapping("/searchall")
public ResultVO categorySearchAll(Integer page,Integer limit)
{
List<Category> categories = categoryService.selectAll(page==null?0:page,limit==null?10:limit);
if(categories.size()!=0)
return new ResultVO(ResultCode.SUCCESS,categories);
else
return new ResultVO(ResultCode.RECORD_NOT_FOUND,null);
}
}
/**
* @Description: 全局异常处理
*/
@RestControllerAdvice
public class GlobalExceptionHandler {
private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);
/**
* 自定义异常处理
* @param e
* @return
*/