一个基于ssm框架的书城项目

一个基于ssm框架的书城项目 适合ssm的整合练习,前端页面基本使用的尚硅谷web教学里的页面,将sql脚本运行后,可以使用账号{用户名:测试用户,密码:123456}进行登录来体验 同样可以通过注册功能注册一个账号进行访问 可能做的不够完善,有什么需要完善的希望大佬们能够指出,谢谢!!!

刚刚学完ssm框架,非常试试用这个项目练练手的!!!

完整项目可通过GitHub或者Gitee进行拉取,顺手点点星(●'◡'●)

GitHub:sxliuhai/bookstore: 一个基于ssm框架的书城项目 (github.com)

Gitee:bookstore: 一个基于ssm的简单书城项目 (gitee.com)

 主页部分

订单页面

登陆页面购物车页面

 部分代码如下

图书模块controller
package lh.com.controller;

import lh.com.pojo.Book;
import lh.com.pojo.Page;
import lh.com.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping("/book")
public class BookController {
    @Autowired
    BookService bookService;
    @RequestMapping("/booklist/{pageNum}")
    public ModelAndView booklist(@PathVariable("pageNum")int pageNum){
        ModelAndView modelAndView = new ModelAndView();
        Page page= bookService.booklist(pageNum);
        modelAndView.setViewName("manager/book_manager");
        modelAndView.addObject("page",page);
        return modelAndView;
    }
    @RequestMapping("/bookadd")
    public String bookadd(Book book){
        bookService.bookadd(book);
        return "redirect:/book/booklist/1";
    }
    @RequestMapping("/bookget/{id}")
    public ModelAndView bookget(@PathVariable("id")int id){
        ModelAndView modelAndView = new ModelAndView();
        Book book = bookService.bookget(id);
        modelAndView.addObject("book",book);
        modelAndView.setViewName("manager/book_update");
        return modelAndView;
    }
    @RequestMapping("/bookupdate/{id}")
    public String bookupdate(Book book,@PathVariable("id")int id){
        book.setId(id);
        bookService.bookupdate(book);
        return "redirect:/book/booklist/1";
    }
    @RequestMapping("/bookdelete/{id}")
    public String bookdelete(@PathVariable("id")int id){
        bookService.bookdelete(id);
        return "redirect:/book/booklist/1";
    }

}

 购物车模块controller 

package lh.com.controller;

import lh.com.pojo.CartItem;
import lh.com.pojo.User;
import lh.com.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/cart")
public class CartController {
    @Autowired
    CartService cartService;
    @RequestMapping("/add/{id}")
    public String add(@PathVariable("id")int id,HttpSession httpSession){
        String order_id =(String)httpSession.getAttribute("order_id");
        User user=(User)httpSession.getAttribute("user");
        int user_id=user.getId();
        if(order_id==null){
            order_id=Long.toString(System.currentTimeMillis());
            httpSession.setAttribute("order_id",order_id);
            cartService.addOrder(user_id,order_id);}
        cartService.addItem(id,order_id);
        return "redirect:/";
    }
    @RequestMapping("/list")
    public ModelAndView itemList(HttpSession httpSession){
        ModelAndView modelAndView = new ModelAndView();
        String order_id = (String) httpSession.getAttribute("order_id");
        List<CartItem> items = cartService.itemList(order_id);
        modelAndView.setViewName("cart/cart");
        modelAndView.addObject("items",items);
        return modelAndView;
    }
    @RequestMapping("/itemDelete/{id}")
    public String itemDelete(@PathVariable("id")int id){
        cartService.itemDelete(id);
        return "redirect:/cart/list";
    }
    @RequestMapping("allDelete")
    public String allDelete(HttpSession httpSession){
        String order_id = (String) httpSession.getAttribute("order_id");
        cartService.allDelete(order_id);
        return "redirect:/cart/list";
    }
    @RequestMapping("/pay")
    public ModelAndView pay(HttpSession httpSession){
        ModelAndView modelAndView = new ModelAndView();
        String order_id = (String) httpSession.getAttribute("order_id");
        cartService.updateOrder(order_id);
        modelAndView.setViewName("cart/checkout");
        modelAndView.addObject("order_id",order_id);
        httpSession.removeAttribute("order_id");
        return modelAndView;
    }

}

 订单模块controller

package lh.com.controller;

import lh.com.pojo.CartItem;
import lh.com.pojo.Order;
import lh.com.pojo.User;
import lh.com.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @RequestMapping("/listOrder")
    public ModelAndView listOrder(HttpSession httpSession){
        User user = (User) httpSession.getAttribute("user");
        int id = user.getId();
        ModelAndView modelAndView = new ModelAndView();
        List<Order> orders=orderService.listOrder(id);
        modelAndView.setViewName("/order/order");
        modelAndView.addObject("orders",orders);
        return modelAndView;
    }
    @RequestMapping("/listCart/{order_id}")
    public ModelAndView listCart(@PathVariable("order_id")String order_id ){
        ModelAndView modelAndView=new ModelAndView();
        List<CartItem>items=orderService.listCart(order_id);
        modelAndView.setViewName("/cart/cart");
        modelAndView.addObject("items",items);
        return modelAndView;
    }
}

 用户模块controller

package lh.com.controller;

import lh.com.pojo.User;
import lh.com.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;
    @RequestMapping("/login")
    public String login(User user, HttpSession httpSession,HttpServletRequest httpServletRequest){
        ModelAndView modelAndView = new ModelAndView();
        User check = userService.check(user);
        if(check!=null)
       {   httpSession.setAttribute("user",check);
           return"redirect:/";
       }
       else{
           httpServletRequest.setAttribute("msg","您的用户名或密码错误");
           return "user/login";
        }
    }
    @RequestMapping("/logout")
    public String logout(HttpSession httpSession){
       httpSession.removeAttribute("user");
            return "user/login";
    }
    @RequestMapping("/regist")
    public String regist(User user){
        userService.saveUser(user);
        return"user/login";
    }
}

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值