首先这个项目没有什么创新的精神啊,就是想把一些技术栈和框架整合起来都实践以下,我们希望在线商城和后台管理分开来,有两个登录的入口进到不同的主页下。在线商城主要有用户来将商品加入购物车然后进入到购物车进行结算,最后能再订单界面看到自己的订单情况。而后台管理可以调控商品的库存和订单管理。因为我们后期这个项目是要不停的调整架构,继承前沿的框架,所以我们业务就不要做的太复杂。
一、登录界面我们再请求为/的页面下设置了两个登录的入口用来选择系统入口,一个是通往在线商城的登录界面,一个是通往后台管理的登录界面。本项目是介于上篇搭建出的框架上进行的。所以只要写两个控制类来拦截/再增加两个按钮点击就发送请求转到另外的登录界面去。效果图如下,代码太简单就不贴了:
前往到商城的登录界面
这边主要是用了BootStrap经典的表单框架,我们在登录的时候通过Mybatis框架进行对User的查询,如果查到的密码和账号相匹配就登入成功。进入到我们的主页面:
这个导航栏也是BootStrap的,非常好用。因为我们后期好几个页面都是要通过登录验证的,所以我们需要在登录的是后设置Session用于拦截器的校验。
我构思了一下这个商品的展示和购物车的使用,我决定了用Redis暂存添加到购物车的数据所以SSM框架就到此为止了,直接给爷整SpringBoot以下是各层的代码:
**POJO:**
略......
**Mapper:**
@Component
public interface UserMapper {
@Select("select * from user")
public List listUser();
@Select("select * from user where email = #{email}")
public User getUserByEmail(String email);
}
**Controller**
@Controller
@RequestMapping("/managerment")
public class ManagermentIndexController {
@Autowired
UserService userService;
@GetMapping("/login")
public ModelAndView login_() {
ModelAndView mv = new ModelAndView("login");
return mv;
}
@PostMapping("/login")
public ModelAndView login(@RequestParam("email") String email, @RequestParam("password") String password, HttpSession httpSession) {
ModelAndView mv = new ModelAndView();
User user = userService.getUserByEmail(email);
if(user != null && user.password.equals(password)) {
mv.setViewName("redirect:/managerment/index");
mv.addObject("user", user);
httpSession.setAttribute("USER_KEY", user);
return mv;
}
mv.setViewName("redirect:/managerment/login");
return mv;
}
@GetMapping("/index")
public ModelAndView index_(HttpSession httpSession) {
User user = (User)httpSession.getAttribute("USER_KEY");
ModelAndView modAndView = new ModelAndView();
if(user == null) {
modAndView.setViewName("redirect:/managerment/login");
return modAndView;
}
modAndView.addObject("user", user);
modAndView.setViewName("managerment_index");
return modAndView;
}
@GetMapping("/logout")
public ModelAndView logout(HttpSession httpSession) {
ModelAndView mv = new ModelAndView();
httpSession.removeAttribute("USER_KEY");
mv.setViewName("redirect:/managerment/login");
return mv;
}
}
二、框架更换说明
我们需要整合各种第三方框架所以直接换成SpringBoot,导入各种Starter非常的哇塞,简单到变态,我们后期在购物车继承Redis时需要设置一个过期时间,例如30分钟没结算那就清空,这是一个典型的Redis应用场景。还有一些点赞排名也是Redis的应用场景在此就不做过多的叙述。



