赞
踩
系统技术栈
服务端:Spring + SpringMVC + Mybatis + JSP
数据库:Mysql
前台技术:js + jQuery + bootstrap
系统模块介绍
前台:
后台:
前台截图效果
首页
购物车
我的订单
商品详细页(含评论信息)
后台效果截图
登录页
首页
类目管理
商品管理
订单管理
评价管理
数据库表
论文截图
核心代码
UserController类
-
-
- import java.io.UnsupportedEncodingException;
- import java.math.BigDecimal;
- import java.util.Objects;
-
- import javax.annotation.Resource;
- import javax.servlet.ServletRequest;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpSession;
-
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.PostMapping;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestParam;
- import org.springframework.web.bind.annotation.ResponseBody;
-
- import com.config.ExceptionConfig.MyException;
- import com.entity.Orders;
- import com.entity.Users;
- import com.service.CartService;
- import com.service.GoodService;
- import com.service.OrderService;
- import com.service.UserService;
- import com.util.PageUtil;
- import com.util.SafeUtil;
-
- /**
- * 用户相关接口
- */
- @Controller
- @RequestMapping("/index")
- public class UserController {
-
- @Resource
- private UserService userService;
- @Resource
- private GoodService goodService;
- @Resource
- private OrderService orderService;
- @Resource
- private CartService cartService;
-
-
- /**
- * 用户注册
- *
- * @return
- */
- @GetMapping("/register")
- public String reg() {
- return "/index/register.jsp";
- }
-
- /**
- * 用户注册
- *
- * @return
- */
- @PostMapping("/register")
- public String register(Users user, HttpServletRequest request) {
- if (user.getUsername().isEmpty()) {
- request.setAttribute("msg", "用户名不能为空!");
- } else if (Objects.nonNull(userService.getByUsername(user.getUsername()))) {
- request.setAttribute("msg", "用户名已存在!");
- } else {
- userService.add(user);
- request.setAttribute("msg", "注册成功 可以去登录了!");
- return "/index/login.jsp";
- }
- return "/index/register.jsp";
- }
-
- /**
- * 用户登录
- *
- * @return
- */
- @GetMapping("/login")
- public String log() {
- return "/index/login.jsp";
- }
-
- /**
- * 用户登录
- *
- * @return
- */
- @PostMapping("/login")
- public String login(Users user, HttpServletRequest request, HttpSession session) {
- Users loginUser = userService.getByUsernameAndPassword(user.getUsername(), user.getPassword());
- if (Objects.isNull(loginUser)) {
- request.setAttribute("msg", "用户名或密码错误");
- return "/index/login.jsp";
- }
- session.setAttribute("user", loginUser);
- // 还原购物车
- session.setAttribute("cartCount", cartService.getCount(loginUser.getId()));
- String referer = request.getHeader("referer"); // 来源页面
- System.out.println(referer); //TODO
- return "redirect:index";
- }
-
- /**
- * 注销登录
- *
- * @return
- */
- @GetMapping("/logout")
- public String logout(HttpSession session) {
- session.removeAttribute("user");
- session.removeAttribute("cartCount");
- return "/index/login.jsp";
- }
-
- /**
- * 查看积分
- */
- @GetMapping("/mypoint")
- public String mypoint() {
- return "/index/mypoint.jsp";
- }
-
- //积分充值
- @RequestMapping("/addPoint")
- public String addPoint(double point, HttpSession session, HttpServletRequest request) {
- Users users = (Users) session.getAttribute("user");
- BigDecimal bigDecimal = new BigDecimal(Double.toString(point)).add(new BigDecimal(Double.toString(users.getPoint())));
- users.setPoint(bigDecimal.doubleValue());
- int count = userService.updatePoint(users);
- if (count > 0) {
- session.setAttribute("user", users);
- request.setAttribute("msg", "充值成功!");
- } else {
- request.setAttribute("msg", "充值失败!");
- }
- return "/index/mypoint.jsp";
- }
-
- /**
- * 查看购物车
- *
- * @return
- */
- @GetMapping("/cart")
- public String cart(HttpServletRequest request, HttpSession session) {
- Users user = (Users) session.getAttribute("user");
- request.setAttribute("cartList", cartService.getList(user.getId()));
- request.setAttribute("cartCount", cartService.getCount(user.getId()));
- request.setAttribute("cartTotal", cartService.getTotal(user.getId()));
- return "/index/cart.jsp";
- }
-
- /**
- * 购物车总金额
- *
- * @return
- */
- @GetMapping("/cartTotal")
- public @ResponseBody
- int cartTotal(HttpSession session) {
- Users user = (Users) session.getAttribute("user");
- return cartService.getTotal(user.getId());
- }
-
- /**
- * 加入购物车
- *
- * @return
- */
- @PostMapping("/cartBuy")
- public @ResponseBody
- boolean cartBuy(int goodId, HttpSession session) {
- Users user = (Users) session.getAttribute("user");
- return cartService.save(goodId, user.getId());
- }
-
- /**
- * 添加数量
- */
- @PostMapping("/cartAdd")
- public @ResponseBody
- boolean cartAdd(int id) {
- return cartService.add(id);
- }
-
- /**
- * 减少数量
- */
- @PostMapping("/cartLess")
- public @ResponseBody
- boolean cartLess(int id) {
- return cartService.less(id);
- }
-
- /**
- * 删除
- */
- @PostMapping("/cartDelete")
- public @ResponseBody
- boolean cartDelete(int id) {
- return cartService.delete(id);
- }
-
-
- /**
- * 查看订单
- *
- * @return
- * @throws UnsupportedEncodingException
- */
- @GetMapping("/order")
- public String order(HttpServletRequest request, HttpSession session,
- @RequestParam(required = false, defaultValue = "1") int page,
- @RequestParam(required = false, defaultValue = "6") int size) throws UnsupportedEncodingException {
- Users user = (Users) session.getAttribute("user");
- request.setAttribute("orderList", orderService.getListByUserid(user.getId(), page, size));
- request.setAttribute("pageHtml", PageUtil.getPageHtml(request, orderService.getCountByUserid(user.getId()), page, size));
- return "/index/order.jsp";
- }
-
- /**
- * 直接购买
- *
- * @return
- * @throws MyException
- */
- @PostMapping("/orderAdd")
- public String orderAdd(int goodId, HttpSession session) throws MyException {
- Users user = (Users) session.getAttribute("user");
- int orderId = orderService.add(goodId, user.getId());
- return "redirect:orderPay?id=" + orderId; // 跳转支付
- }
-
- /**
- * 购物车结算
- *
- * @return
- * @throws MyException
- */
- @GetMapping("/orderSave")
- public String orderSave(ServletRequest request, HttpSession session) throws MyException {
- Users user = (Users) session.getAttribute("user");
- int orderId = orderService.save(user.getId());
- session.removeAttribute("cartCount"); // 清理购物车session
- return "redirect:orderPay?id=" + orderId; // 跳转支付
- }
-
- /**
- * 支付页面
- *
- * @return
- */
- @GetMapping("/orderPay")
- public String orderPay(int id, ServletRequest request) {
- request.setAttribute("order", orderService.get(id));
- return "/index/pay.jsp";
- }
-
- /**
- * 支付(模拟)
- *
- * @return
- */
- @PostMapping("/orderPay")
- @ResponseBody
- public int orderPay(Orders order, HttpSession session) {
- Users users = (Users) session.getAttribute("user");
- BigDecimal bigDecimal = new BigDecimal(0);
- if (order.getPaytype() == Orders.PAYTYPE_OFFLINE) {//为积分支付时
- double d1 = order.getTotal();//商品总价
- if (users.getPoint().compareTo(d1) < 0) {
- return -1;
- } else {
- //总积分 = 用户积分 - 抵扣积分
- bigDecimal = new BigDecimal(Double.toString(users.getPoint())).subtract(new BigDecimal(Double.toString(d1)));
- }
- } else {
- double d2 = order.getTotal() / 100;//购买商品获取的积分
- //总积分 = 用户积分 + 获取的
- bigDecimal = new BigDecimal(Double.toString(users.getPoint())).add(new BigDecimal(Double.toString(d2)));
- }
- users.setPoint(bigDecimal.doubleValue());//保存积分
- int count = userService.updatePoint(users);
- if (count > 0) {
- session.setAttribute("user", users);//更新用户积分
- }
- orderService.pay(order);
- return 1;
- }
-
-
- /**
- * 收货地址
- *
- * @return
- */
- @GetMapping("/address")
- public String address() { // 使用session中的数据
- return "/index/address.jsp";
- }
-
- /**
- * 修改信息
- *
- * @return
- */
- @PostMapping("/addressUpdate")
- public String addressUpdate(String name, String phone, String address, HttpServletRequest request, HttpSession session) {
- Users user = (Users) session.getAttribute("user");
- userService.update(user.getId(), name, phone, address); // 更新数据库
- session.setAttribute("user", userService.get(user.getId())); // 更新session
- request.setAttribute("msg", "信息修改成功!");
- return "/index/address.jsp";
- }
-
- /**
- * 收货地址
- *
- * @return
- */
- @GetMapping("/password")
- public String password() { // 使用session中的数据
- return "/index/password.jsp";
- }
-
- /**
- * 修改密码
- *
- * @return
- */
- @PostMapping("/passwordUpdate")
- public String passwordUpdate(String password, String passwordNew, HttpServletRequest request, HttpSession session) {
- Users user = (Users) session.getAttribute("user");
- user = userService.get(user.getId());
- if (!user.getPassword().equals(SafeUtil.encode(password))) {
- request.setAttribute("msg", "原密码错误!");
- } else {
- userService.updatePassword(user.getId(), passwordNew);
- request.setAttribute("msg", "密码修改成功!");
- }
- return "/index/password.jsp";
- }
-
- @RequestMapping("/userfinish")
- public String userFinish(int id, byte status,
- @RequestParam(required = false, defaultValue = "1") int page) {
- orderService.finish(id);
- return "redirect:order?page=" + page;
- }
- }
UserService实现类(前台用户登录、注册、退出等功能)
- package com.service;
-
- import java.util.List;
-
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
-
- import com.dao.UsersDao;
- import com.entity.Users;
- import com.util.SafeUtil;
-
- /**
- * 用户服务
- */
- @Service
- public class UserService {
-
- @Autowired
- private UsersDao userDao;
-
-
- /**
- * 总数
- *
- * @return
- */
- public long getCount() {
- return userDao.selectCount();
- }
-
- public long queryCount(String username) {
- return userDao.queryCount(username);
- }
-
-
-
- /**
- * 列表
- *
- * @param page
- * @param rows
- * @return
- */
- public List<Users> getList(int page, int rows) {
- return userDao.selectList(rows * (page - 1), rows);
- }
-
- public List<Users> queryList(int page, int rows, String username) {
- return userDao.queryList(rows * (page - 1), rows,username);
- }
-
- /**
- * 通过id获取
- *
- * @param id
- * @return
- */
- public Users get(int id) {
- return userDao.select(id);
- }
-
- /**
- * 通过用户名获取
- *
- * @param username
- * @return
- */
- public Users getByUsername(String username) {
- return userDao.selectByUsername(username);
- }
-
- /**
- * 通过用户名和密码获取
- *
- * @param username
- * @param password
- * @return
- */
- public Users getByUsernameAndPassword(String username, String password) {
- return userDao.selectByUsernameAndPassword(username, SafeUtil.encode(password));
- }
-
- /**
- * 添加
- *
- * @param user
- * @return
- */
- public boolean add(Users user) {
- user.setPassword(SafeUtil.encode(user.getPassword()));
- return userDao.insert(user);
- }
-
- /**
- * 更新
- *
- * @param user
- */
- public boolean update(int id, String name, String phone, String address) {
- Users user = new Users();
- user.setId(id);
- user.setName(name);
- user.setPhone(phone);
- user.setAddress(address);
- return userDao.update(user);
- }
-
- /**
- * 更新
- *
- * @param user
- */
- public boolean updatePassword(int id, String password) {
- return userDao.updatePassword(id, SafeUtil.encode(password));
- }
-
- /**
- * 删除
- *
- * @param id
- */
- public boolean delete(int id) {
- return userDao.delete(id);
- }
-
- public int updatePoint(Users users) {
- return userDao.updatePoint(users);
- }
- }
本系统在处理注册请求时,为了保证数据的安全性,采用Md5加密技术对密码进行加密,工具类代码如下:
- package com.util;
- import java.security.MessageDigest;
- import java.security.NoSuchAlgorithmException;
- import org.springframework.util.Base64Utils;
-
- /**
- * 安全工具类
- */
- public class SafeUtil {
-
- /**
- * md5加密字符串
- */
- public final static String md5(String str) {
- MessageDigest messageDigest = null;
- try {
- messageDigest = MessageDigest.getInstance("MD5");
- } catch (NoSuchAlgorithmException e) {
- e.printStackTrace();
- }
- messageDigest.update(str.getBytes());
- return Base64Utils.encodeToString(messageDigest.digest());
- }
-
- /**
- * sha1加密字符串
- */
- public final static String sha1(String str) {
- MessageDigest messageDigest = null;
- try {
- messageDigest = MessageDigest.getInstance("SHA-1");
- } catch (NoSuchAlgorithmException e) {
- e.printStackTrace();
- }
- messageDigest.update(str.getBytes());
- return Base64Utils.encodeToString(messageDigest.digest());
- }
-
- /**
- * 使用特定加密范式加密
- */
- public final static String encode(String str) {
- return md5(sha1(md5(str)));
- }
-
- }
UserDao接口(执行sql语句来落实业务)
- public interface UsersDao {
-
- @Select("select count(*) from users")
- public long selectCount();
-
- @Select("select * from users order by id desc limit #{begin}, #{size}")
- public List<Users> selectList(@Param("begin") int begin, @Param("size") int size);
-
- @Select("select * from users where id=#{id}")
- public Users select(int id);
-
- @Select("select * from users where username=#{username}")
- public Users selectByUsername(String username);
-
- @Select("select * from users where username=#{username} and password=#{password}")
- public Users selectByUsernameAndPassword(@Param("username") String username, @Param("password") String password);
-
- @Insert("insert into users (username,password,name,phone,address) "
- + "values (#{username},#{password},#{name},#{phone},#{address})")
- @SelectKey(keyProperty = "id", statement = "SELECT LAST_INSERT_ID()", before = false, resultType = Integer.class)
- public boolean insert(Users user);
-
- @Update("update users set name=#{name},phone=#{phone},address=#{address} where id=#{id}")
- public boolean update(Users user);
-
- @Update("update users set password=#{password} where id=#{id}")
- public boolean updatePassword(@Param("id") int id, @Param("password") String password);
-
- @Update("delete from users where id=#{id}")
- public boolean delete(int id);
-
- @Update("update users set point=#{point} where id=#{id}")
- int updatePoint(Users users);
- }
写在最后
该系统功能完整,代码简洁易懂,值得我们去学习
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。