当前位置:   article > 正文

SSM网上商城购物系统(前台+后台)_基于ssm的线上商城系统(前台+后台)

基于ssm的线上商城系统(前台+后台)

系统技术栈

服务端:Spring + SpringMVC + Mybatis + JSP

数据库:Mysql

前台技术:js + jQuery + bootstrap

 

系统模块介绍

前台:

  • 用户的登录和注册,注册包括用户名、密码、姓名、手机号、收货地址
  • 个人中心(我的订单、积分查看、收货地址、修改密码等)
  • 购物车列表(查看当前登录用户的购物车)
  • 商品评论
  • 商品信息列表(今日推荐、热销排行、新品上市)
  • 支付功能

后台:

  • 类目管理
  • 商品管理
  • 订单管理
  • 用户管理
  • 评论管理
  • 管理员操作
  • 退出

 

前台截图效果

首页

ad287e1dac5e4783a408bf1fbd8477ab.png


购物车

2b951c6743724c0391a306f6fd97b5b5.png

 


我的订单

8069c105241d46cca8391dfa2fe8a96d.png


商品详细页(含评论信息)

 

8ffba03e39df4febadd8e6e24b60a579.png

后台效果截图

登录页

a9b406eaa54c4ebcbbe5c6c550f98552.png


首页

49b09562340c417f949f925e88579ed6.png

 


 类目管理

0226f88841ef4db3bc09eaa01d607cfa.png


商品管理

ce4e5732c1284be88d2da0b040ece679.png


订单管理

c737799eb03e4a82a1de86b79c4995aa.png


评价管理

 

e5244e9687f94c1990231085d69b72c7.png 

数据库表

00def942ba8d46268bb2df50844e55b8.png

 

 

论文截图

189eda52b08c46639df39cf5e1e3af82.png

 b7e8c18531254647b57555b6137e6183.png

 核心代码

UserController类

  1. import java.io.UnsupportedEncodingException;
  2. import java.math.BigDecimal;
  3. import java.util.Objects;
  4. import javax.annotation.Resource;
  5. import javax.servlet.ServletRequest;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpSession;
  8. import org.springframework.stereotype.Controller;
  9. import org.springframework.web.bind.annotation.GetMapping;
  10. import org.springframework.web.bind.annotation.PostMapping;
  11. import org.springframework.web.bind.annotation.RequestMapping;
  12. import org.springframework.web.bind.annotation.RequestParam;
  13. import org.springframework.web.bind.annotation.ResponseBody;
  14. import com.config.ExceptionConfig.MyException;
  15. import com.entity.Orders;
  16. import com.entity.Users;
  17. import com.service.CartService;
  18. import com.service.GoodService;
  19. import com.service.OrderService;
  20. import com.service.UserService;
  21. import com.util.PageUtil;
  22. import com.util.SafeUtil;
  23. /**
  24. * 用户相关接口
  25. */
  26. @Controller
  27. @RequestMapping("/index")
  28. public class UserController {
  29. @Resource
  30. private UserService userService;
  31. @Resource
  32. private GoodService goodService;
  33. @Resource
  34. private OrderService orderService;
  35. @Resource
  36. private CartService cartService;
  37. /**
  38. * 用户注册
  39. *
  40. * @return
  41. */
  42. @GetMapping("/register")
  43. public String reg() {
  44. return "/index/register.jsp";
  45. }
  46. /**
  47. * 用户注册
  48. *
  49. * @return
  50. */
  51. @PostMapping("/register")
  52. public String register(Users user, HttpServletRequest request) {
  53. if (user.getUsername().isEmpty()) {
  54. request.setAttribute("msg", "用户名不能为空!");
  55. } else if (Objects.nonNull(userService.getByUsername(user.getUsername()))) {
  56. request.setAttribute("msg", "用户名已存在!");
  57. } else {
  58. userService.add(user);
  59. request.setAttribute("msg", "注册成功 可以去登录了!");
  60. return "/index/login.jsp";
  61. }
  62. return "/index/register.jsp";
  63. }
  64. /**
  65. * 用户登录
  66. *
  67. * @return
  68. */
  69. @GetMapping("/login")
  70. public String log() {
  71. return "/index/login.jsp";
  72. }
  73. /**
  74. * 用户登录
  75. *
  76. * @return
  77. */
  78. @PostMapping("/login")
  79. public String login(Users user, HttpServletRequest request, HttpSession session) {
  80. Users loginUser = userService.getByUsernameAndPassword(user.getUsername(), user.getPassword());
  81. if (Objects.isNull(loginUser)) {
  82. request.setAttribute("msg", "用户名或密码错误");
  83. return "/index/login.jsp";
  84. }
  85. session.setAttribute("user", loginUser);
  86. // 还原购物车
  87. session.setAttribute("cartCount", cartService.getCount(loginUser.getId()));
  88. String referer = request.getHeader("referer"); // 来源页面
  89. System.out.println(referer); //TODO
  90. return "redirect:index";
  91. }
  92. /**
  93. * 注销登录
  94. *
  95. * @return
  96. */
  97. @GetMapping("/logout")
  98. public String logout(HttpSession session) {
  99. session.removeAttribute("user");
  100. session.removeAttribute("cartCount");
  101. return "/index/login.jsp";
  102. }
  103. /**
  104. * 查看积分
  105. */
  106. @GetMapping("/mypoint")
  107. public String mypoint() {
  108. return "/index/mypoint.jsp";
  109. }
  110. //积分充值
  111. @RequestMapping("/addPoint")
  112. public String addPoint(double point, HttpSession session, HttpServletRequest request) {
  113. Users users = (Users) session.getAttribute("user");
  114. BigDecimal bigDecimal = new BigDecimal(Double.toString(point)).add(new BigDecimal(Double.toString(users.getPoint())));
  115. users.setPoint(bigDecimal.doubleValue());
  116. int count = userService.updatePoint(users);
  117. if (count > 0) {
  118. session.setAttribute("user", users);
  119. request.setAttribute("msg", "充值成功!");
  120. } else {
  121. request.setAttribute("msg", "充值失败!");
  122. }
  123. return "/index/mypoint.jsp";
  124. }
  125. /**
  126. * 查看购物车
  127. *
  128. * @return
  129. */
  130. @GetMapping("/cart")
  131. public String cart(HttpServletRequest request, HttpSession session) {
  132. Users user = (Users) session.getAttribute("user");
  133. request.setAttribute("cartList", cartService.getList(user.getId()));
  134. request.setAttribute("cartCount", cartService.getCount(user.getId()));
  135. request.setAttribute("cartTotal", cartService.getTotal(user.getId()));
  136. return "/index/cart.jsp";
  137. }
  138. /**
  139. * 购物车总金额
  140. *
  141. * @return
  142. */
  143. @GetMapping("/cartTotal")
  144. public @ResponseBody
  145. int cartTotal(HttpSession session) {
  146. Users user = (Users) session.getAttribute("user");
  147. return cartService.getTotal(user.getId());
  148. }
  149. /**
  150. * 加入购物车
  151. *
  152. * @return
  153. */
  154. @PostMapping("/cartBuy")
  155. public @ResponseBody
  156. boolean cartBuy(int goodId, HttpSession session) {
  157. Users user = (Users) session.getAttribute("user");
  158. return cartService.save(goodId, user.getId());
  159. }
  160. /**
  161. * 添加数量
  162. */
  163. @PostMapping("/cartAdd")
  164. public @ResponseBody
  165. boolean cartAdd(int id) {
  166. return cartService.add(id);
  167. }
  168. /**
  169. * 减少数量
  170. */
  171. @PostMapping("/cartLess")
  172. public @ResponseBody
  173. boolean cartLess(int id) {
  174. return cartService.less(id);
  175. }
  176. /**
  177. * 删除
  178. */
  179. @PostMapping("/cartDelete")
  180. public @ResponseBody
  181. boolean cartDelete(int id) {
  182. return cartService.delete(id);
  183. }
  184. /**
  185. * 查看订单
  186. *
  187. * @return
  188. * @throws UnsupportedEncodingException
  189. */
  190. @GetMapping("/order")
  191. public String order(HttpServletRequest request, HttpSession session,
  192. @RequestParam(required = false, defaultValue = "1") int page,
  193. @RequestParam(required = false, defaultValue = "6") int size) throws UnsupportedEncodingException {
  194. Users user = (Users) session.getAttribute("user");
  195. request.setAttribute("orderList", orderService.getListByUserid(user.getId(), page, size));
  196. request.setAttribute("pageHtml", PageUtil.getPageHtml(request, orderService.getCountByUserid(user.getId()), page, size));
  197. return "/index/order.jsp";
  198. }
  199. /**
  200. * 直接购买
  201. *
  202. * @return
  203. * @throws MyException
  204. */
  205. @PostMapping("/orderAdd")
  206. public String orderAdd(int goodId, HttpSession session) throws MyException {
  207. Users user = (Users) session.getAttribute("user");
  208. int orderId = orderService.add(goodId, user.getId());
  209. return "redirect:orderPay?id=" + orderId; // 跳转支付
  210. }
  211. /**
  212. * 购物车结算
  213. *
  214. * @return
  215. * @throws MyException
  216. */
  217. @GetMapping("/orderSave")
  218. public String orderSave(ServletRequest request, HttpSession session) throws MyException {
  219. Users user = (Users) session.getAttribute("user");
  220. int orderId = orderService.save(user.getId());
  221. session.removeAttribute("cartCount"); // 清理购物车session
  222. return "redirect:orderPay?id=" + orderId; // 跳转支付
  223. }
  224. /**
  225. * 支付页面
  226. *
  227. * @return
  228. */
  229. @GetMapping("/orderPay")
  230. public String orderPay(int id, ServletRequest request) {
  231. request.setAttribute("order", orderService.get(id));
  232. return "/index/pay.jsp";
  233. }
  234. /**
  235. * 支付(模拟)
  236. *
  237. * @return
  238. */
  239. @PostMapping("/orderPay")
  240. @ResponseBody
  241. public int orderPay(Orders order, HttpSession session) {
  242. Users users = (Users) session.getAttribute("user");
  243. BigDecimal bigDecimal = new BigDecimal(0);
  244. if (order.getPaytype() == Orders.PAYTYPE_OFFLINE) {//为积分支付时
  245. double d1 = order.getTotal();//商品总价
  246. if (users.getPoint().compareTo(d1) < 0) {
  247. return -1;
  248. } else {
  249. //总积分 = 用户积分 - 抵扣积分
  250. bigDecimal = new BigDecimal(Double.toString(users.getPoint())).subtract(new BigDecimal(Double.toString(d1)));
  251. }
  252. } else {
  253. double d2 = order.getTotal() / 100;//购买商品获取的积分
  254. //总积分 = 用户积分 + 获取的
  255. bigDecimal = new BigDecimal(Double.toString(users.getPoint())).add(new BigDecimal(Double.toString(d2)));
  256. }
  257. users.setPoint(bigDecimal.doubleValue());//保存积分
  258. int count = userService.updatePoint(users);
  259. if (count > 0) {
  260. session.setAttribute("user", users);//更新用户积分
  261. }
  262. orderService.pay(order);
  263. return 1;
  264. }
  265. /**
  266. * 收货地址
  267. *
  268. * @return
  269. */
  270. @GetMapping("/address")
  271. public String address() { // 使用session中的数据
  272. return "/index/address.jsp";
  273. }
  274. /**
  275. * 修改信息
  276. *
  277. * @return
  278. */
  279. @PostMapping("/addressUpdate")
  280. public String addressUpdate(String name, String phone, String address, HttpServletRequest request, HttpSession session) {
  281. Users user = (Users) session.getAttribute("user");
  282. userService.update(user.getId(), name, phone, address); // 更新数据库
  283. session.setAttribute("user", userService.get(user.getId())); // 更新session
  284. request.setAttribute("msg", "信息修改成功!");
  285. return "/index/address.jsp";
  286. }
  287. /**
  288. * 收货地址
  289. *
  290. * @return
  291. */
  292. @GetMapping("/password")
  293. public String password() { // 使用session中的数据
  294. return "/index/password.jsp";
  295. }
  296. /**
  297. * 修改密码
  298. *
  299. * @return
  300. */
  301. @PostMapping("/passwordUpdate")
  302. public String passwordUpdate(String password, String passwordNew, HttpServletRequest request, HttpSession session) {
  303. Users user = (Users) session.getAttribute("user");
  304. user = userService.get(user.getId());
  305. if (!user.getPassword().equals(SafeUtil.encode(password))) {
  306. request.setAttribute("msg", "原密码错误!");
  307. } else {
  308. userService.updatePassword(user.getId(), passwordNew);
  309. request.setAttribute("msg", "密码修改成功!");
  310. }
  311. return "/index/password.jsp";
  312. }
  313. @RequestMapping("/userfinish")
  314. public String userFinish(int id, byte status,
  315. @RequestParam(required = false, defaultValue = "1") int page) {
  316. orderService.finish(id);
  317. return "redirect:order?page=" + page;
  318. }
  319. }

UserService实现类(前台用户登录、注册、退出等功能)

  1. package com.service;
  2. import java.util.List;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.stereotype.Service;
  5. import com.dao.UsersDao;
  6. import com.entity.Users;
  7. import com.util.SafeUtil;
  8. /**
  9. * 用户服务
  10. */
  11. @Service
  12. public class UserService {
  13. @Autowired
  14. private UsersDao userDao;
  15. /**
  16. * 总数
  17. *
  18. * @return
  19. */
  20. public long getCount() {
  21. return userDao.selectCount();
  22. }
  23. public long queryCount(String username) {
  24. return userDao.queryCount(username);
  25. }
  26. /**
  27. * 列表
  28. *
  29. * @param page
  30. * @param rows
  31. * @return
  32. */
  33. public List<Users> getList(int page, int rows) {
  34. return userDao.selectList(rows * (page - 1), rows);
  35. }
  36. public List<Users> queryList(int page, int rows, String username) {
  37. return userDao.queryList(rows * (page - 1), rows,username);
  38. }
  39. /**
  40. * 通过id获取
  41. *
  42. * @param id
  43. * @return
  44. */
  45. public Users get(int id) {
  46. return userDao.select(id);
  47. }
  48. /**
  49. * 通过用户名获取
  50. *
  51. * @param username
  52. * @return
  53. */
  54. public Users getByUsername(String username) {
  55. return userDao.selectByUsername(username);
  56. }
  57. /**
  58. * 通过用户名和密码获取
  59. *
  60. * @param username
  61. * @param password
  62. * @return
  63. */
  64. public Users getByUsernameAndPassword(String username, String password) {
  65. return userDao.selectByUsernameAndPassword(username, SafeUtil.encode(password));
  66. }
  67. /**
  68. * 添加
  69. *
  70. * @param user
  71. * @return
  72. */
  73. public boolean add(Users user) {
  74. user.setPassword(SafeUtil.encode(user.getPassword()));
  75. return userDao.insert(user);
  76. }
  77. /**
  78. * 更新
  79. *
  80. * @param user
  81. */
  82. public boolean update(int id, String name, String phone, String address) {
  83. Users user = new Users();
  84. user.setId(id);
  85. user.setName(name);
  86. user.setPhone(phone);
  87. user.setAddress(address);
  88. return userDao.update(user);
  89. }
  90. /**
  91. * 更新
  92. *
  93. * @param user
  94. */
  95. public boolean updatePassword(int id, String password) {
  96. return userDao.updatePassword(id, SafeUtil.encode(password));
  97. }
  98. /**
  99. * 删除
  100. *
  101. * @param id
  102. */
  103. public boolean delete(int id) {
  104. return userDao.delete(id);
  105. }
  106. public int updatePoint(Users users) {
  107. return userDao.updatePoint(users);
  108. }
  109. }

本系统在处理注册请求时,为了保证数据的安全性,采用Md5加密技术对密码进行加密,工具类代码如下:

  1. package com.util;
  2. import java.security.MessageDigest;
  3. import java.security.NoSuchAlgorithmException;
  4. import org.springframework.util.Base64Utils;
  5. /**
  6. * 安全工具类
  7. */
  8. public class SafeUtil {
  9. /**
  10. * md5加密字符串
  11. */
  12. public final static String md5(String str) {
  13. MessageDigest messageDigest = null;
  14. try {
  15. messageDigest = MessageDigest.getInstance("MD5");
  16. } catch (NoSuchAlgorithmException e) {
  17. e.printStackTrace();
  18. }
  19. messageDigest.update(str.getBytes());
  20. return Base64Utils.encodeToString(messageDigest.digest());
  21. }
  22. /**
  23. * sha1加密字符串
  24. */
  25. public final static String sha1(String str) {
  26. MessageDigest messageDigest = null;
  27. try {
  28. messageDigest = MessageDigest.getInstance("SHA-1");
  29. } catch (NoSuchAlgorithmException e) {
  30. e.printStackTrace();
  31. }
  32. messageDigest.update(str.getBytes());
  33. return Base64Utils.encodeToString(messageDigest.digest());
  34. }
  35. /**
  36. * 使用特定加密范式加密
  37. */
  38. public final static String encode(String str) {
  39. return md5(sha1(md5(str)));
  40. }
  41. }

 

UserDao接口(执行sql语句来落实业务)

  1. public interface UsersDao {
  2. @Select("select count(*) from users")
  3. public long selectCount();
  4. @Select("select * from users order by id desc limit #{begin}, #{size}")
  5. public List<Users> selectList(@Param("begin") int begin, @Param("size") int size);
  6. @Select("select * from users where id=#{id}")
  7. public Users select(int id);
  8. @Select("select * from users where username=#{username}")
  9. public Users selectByUsername(String username);
  10. @Select("select * from users where username=#{username} and password=#{password}")
  11. public Users selectByUsernameAndPassword(@Param("username") String username, @Param("password") String password);
  12. @Insert("insert into users (username,password,name,phone,address) "
  13. + "values (#{username},#{password},#{name},#{phone},#{address})")
  14. @SelectKey(keyProperty = "id", statement = "SELECT LAST_INSERT_ID()", before = false, resultType = Integer.class)
  15. public boolean insert(Users user);
  16. @Update("update users set name=#{name},phone=#{phone},address=#{address} where id=#{id}")
  17. public boolean update(Users user);
  18. @Update("update users set password=#{password} where id=#{id}")
  19. public boolean updatePassword(@Param("id") int id, @Param("password") String password);
  20. @Update("delete from users where id=#{id}")
  21. public boolean delete(int id);
  22. @Update("update users set point=#{point} where id=#{id}")
  23. int updatePoint(Users users);
  24. }

写在最后

该系统功能完整,代码简洁易懂,值得我们去学习

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小惠珠哦/article/detail/740426
推荐阅读
相关标签
  

闽ICP备14008679号