基于javaweb+mysql的ssm在线商城购物商城美妆商城(前台、后台)(java+jsp+ssm+javabean+mysql+tomcat)
运行环境
Java≥8、MySQL≥5.7、Tomcat≥8
开发工具
eclipse/idea/myeclipse/sts等均可配置运行
适用
课程设计,大作业,毕业设计,项目练习,学习演示等
功能说明
前台:
首页展示商品信息,注册,登录,查询商品,看商品详情,添加商品至购物车,更新、删除购物车中商品信息,下订单、订单查看等操作
后台:登录,商品上架下架、商品信息修改、添加,订单查看,类型管理:商品类型新增删除以及修改查询等
前台:
后台:
技术框架
JavaBean MVC JSP SSM(Spring SpringMVC MyBatis) MySQL Bootstrap JavaScript
基于javaweb+mysql的SSM在线商城购物商城美妆商城(前台、后台)(java+jsp+ssm+javabean+mysql+tomcat)
@RequestMapping("/showAdd.action")
public String showAdd() {
return "articleType/view";
}
//通过异步请求的方式获取所有一级商品类型
@ResponseBody
@RequestMapping(value = "/loadFirstArticleType.action", produces = {"application/json;charset=utf-8"})
public String loadFirstArticleType() {
List<ArticleType> types = articleTypeService.findAllFirstArticleType();
System.out.println("types:" + JSONArray.fromObject(types).toString());
return JSONArray.fromObject(types).toString();
}
//商品类型的添加或者修改操作
@RequestMapping("/addOrUpdate.action")
public String addOrUpdate(ArticleType type, String parentCode) {
if (type.getCode() != null && !type.getCode().equals("")) {
//进行更新商品类型操作
articleTypeService.updateArticleType(type);
} else {
//进行添加商品类型操作
articleTypeService.saveArticleType(type, parentCode);
}
return "redirect:/articleType_manager/getAll.action";
}
//删除商品类型
@RequestMapping("/deleteType.action")
public String deleteType(@Param("code") String code) {
String message = articleTypeService.deleteType(code);
return "redirect:/articleType_manager/getAll.action?message=delete success";
}
}
/**
*
*/
}
}
/**
*
*/
@Controller
@RequestMapping("/order")
public class OrderController {
@Autowired
private OrderServiceI orderServiceI;
//跳转至确认订单信息页面
@RequestMapping(value = "/checkOrder.do")
public String checkOrder(String orderInfo, HttpSession session) {
try {
//将订单相关信息保存至session中
session.setAttribute("orderInfo", orderInfo);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
//跳转至确认订单信息页面
return "checkOrderInfo";
}
out.close();
input.close();
article.setImage(newFileName);
}
//指定商品类型
article.setTypeCode(code);
//更新商品信息
articleService.updateArticle(article);
request.setAttribute("tip", "商品更新成功!");
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
request.setAttribute("tip", "商品更新失败:"+e.getMessage());
}
return "redirect:/article_manager/getAll.action";
}
}
/**
*
*/
@Controller
@RequestMapping("/shopCar")
public class ShopCarController {
@Autowired
private ShopCarServiceI shopCarService;
@RequestMapping("/addToCar.do")
public String addShopCar(HttpSession session,@Param("id")int id,@Param("number")int number) {
shopCarService.addArticleToShopCar(session,id,number);
//商品加入购物车成功之后,立马展示购物车中的商品信息
return "redirect:/shopCar/showShopCar.do";
}
//展示购物车中的商品信息
@RequestMapping("/showShopCar.do")
public String showShopCar(HttpSession session,Model model) {
//根据用户的id获取该用户购物详情
List<Shopcar> shopCars = shopCarService.getAllShopCarByUserId(session);
model.addAttribute("shopCars", shopCars);
//定义总金额
double totalPrice = 0.0;
//遍历集合 计算购物车中商品的总金额
for(Shopcar shopcar : shopCars) {
totalPrice += shopcar.getArticle().getDiscountPrice() * shopcar.getBuynum();
}
item.setArticleId(articleId);
item.setOrderNum(buyNum);
//订单详细记录放在集合中
items.add(item);
totalPrice += Double.valueOf(infos[2]);
}
//指定订单的总金额
order.setAmount(totalPrice);
//保存订单信息 ,保存完订单信息之后,需要获取订单的id,因为需要将订单的id存放在订单详情中
orderMapper.saveOrder(order);
//获取订单主键的值
int orderId = order.getId();
for(OrderItem item : items) {
item.setOrderId(orderId);
//保存订单明细
orderItemMapper.saveItem(item);
}
/*****************************生成支付二维码*******************************/
DataJoinUtils dataJoinUtils = new DataJoinUtils();
Map<String, String> result = dataJoinUtils.wxPay("http://localhost:8080/ecShop/article/index", order.getOrderCode(), "1", "127.0.0.1", "化妆品");
//将二维码地址封装至order对象中
order.setImgUrl(result.get("code_url"));
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return order;
}
/* (non-Javadoc)
* @see com.demo.shop.service.OrderServiceI#getOrdersByUserId()
* 根据当前用户的id查询,该用户所有的订单信息
*/
@Override
public List<Order> getOrdersByUserId() {
// TODO Auto-generated method stub
//获取当前用户的信息
@RequestMapping("/getAll.action")
public String getAll(PageModel pageModel,Model model) {
List<Order> orders = orderService.getAll(pageModel);
model.addAttribute("orders", orders);
return "order/list";
}
//确认发送订单
@RequestMapping("/checkOrder.action")
public String checkOrder(Order order) {
orderService.checkOrder(order);
return "redirect:/order_manager/getAll.action";
}
}
/**
*
*/
/**
* 定义拦截器,用于判断用户是否登录,如果用户已经登录则放行,否则用户未登录则跳转至登录页面
*/
public class LoginFilter extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
//从session中获取用户信息
List<ArticleType> articleTypes = articleService.findAllFirstArticleType();
model.addAttribute("articleTypes", articleTypes);
model.addAttribute("typeCode", typeCode);
model.addAttribute("keyword", keyword);
System.out.println("typeCode:"+typeCode);
//如果 typecode不为空,则根据typeCode查询二级物品类型
if(typeCode != null && !typeCode.equals("")) {
// 0001 ==> 0001 00010001 ==> 0001
String code = typeCode.substring(0, 4);
//根据一级物品类型获取对应的二级物品类型信息
List<ArticleType> seArticleTypes = articleService.findAllSecondArticleTypes(code+"%");
//将二级物品类型存放至 model
model.addAttribute("secondArticleTypes", seArticleTypes);
}
//根据 用户指定的商品类型查询数据库,获取相应的商品信息
List<Article> articles = articleService.findAllArticle(typeCode == null ? null : typeCode+"%",keyword == null ? null : "%"+keyword+"%",pageModel);
model.addAttribute("articles", articles);
//查询总记录数
int totalNum = articleService.findTotalNum(typeCode == null ? null : typeCode+"%",keyword == null ? null : "%"+keyword+"%");
pageModel.setTotalNum(totalNum);
// WEB-INF/jsp/articleIndex.jsp
return "articleIndex";
}
//根据商品id获取商品详情信息
@RequestMapping("/detail")
public String articleDetail(Integer id,Model model) {
//根据商品id获取商品信息
Article article = articleService.getArticleById(id);
model.addAttribute("article", article);
// /WEB-INF/jsp/articleDetail.jsp
return "articleDetail";
}
}
/**
*
*/
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserServiceI userService;
//处理登录请求
@RequestMapping("/userLogin")
public String userLogin(User user, Model model, HttpSession session) {
//根据用户输入的账号以及密码获取用户的信息
User u = userService.findUserByNameAndPass(user);
if (u == null) {
model.addAttribute("error_message", "您输入的账号或密码不正确,请核实!");
//跳转至登录页面 /WEB-INF/jsp/login.jsp
return "login";
} else if (u.getDisabled().equals("0")) {
model.addAttribute("error_message", "您尚未激活,请打开您的邮箱进行激活操作!");
//跳转至登录页面 /WEB-INF/jsp/login.jsp
return "login";
} else {
//将用户信息存放在session中,直接跳转至首页
session.setAttribute("session_user", u);
return "redirect:/article/index";
}
}
//用户退出
@RequestMapping("/logout")
public String logout(HttpSession session) {
//将用户信息从session中清除
session.removeAttribute("session_user");
//用户退出之后重定向至 首页
return "redirect:/article/index";
}
//异步校验账号是否存在
@ResponseBody
@RequestMapping(value = "/validLoginName", produces = {"application/text;charset=utf-8"})
public String validLoginName(String loginName) {
//用户退出
@RequestMapping("/logout")
public String logout(HttpSession session) {
//将用户信息从session中清除
session.removeAttribute("session_user");
//用户退出之后重定向至 首页
return "redirect:/article/index";
}
//异步校验账号是否存在
@ResponseBody
@RequestMapping(value = "/validLoginName", produces = {"application/text;charset=utf-8"})
public String validLoginName(String loginName) {
//校验账号是否存在
String result = userService.validLoginName(loginName);
return result;
}
//用户注册
@RequestMapping(value = "/userRegister")
public String userRegister(Model model, HttpServletRequest request, User user) {
try {
userService.saveUser(user, request);
model.addAttribute("message", "注册成功!");
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
model.addAttribute("message", "注册失败!");
}
//返回注册页面
return "register";
}
}
/**
*
*/
InputStream input = file.getInputStream();
//获取项目部署路径
String realPath = request.getServletContext().getRealPath("/image/article");
//如果上传所指定的目录不存在则创建目录
File f = new File(realPath);
if(!f.exists()) {
f.mkdirs();
}
//获取当前上传的文件的名字 a.jpg
String imageName = file.getOriginalFilename();
if(imageName!=null && !imageName.equals("")) {
//重新生成新的文件名
String newFileName = UUID.randomUUID().toString() + imageName.substring(imageName.lastIndexOf("."), imageName.length());
System.out.println("文件路径:"+(realPath+File.separator+newFileName));
//构建输出流
OutputStream out = new FileOutputStream(realPath+File.separator+newFileName);
byte[] data = new byte[1024];
int len = 0;
while((len = input.read(data)) != -1) {
out.write(data, 0, len);
out.flush();
}
out.close();
input.close();
article.setImage(newFileName);
}
//指定商品类型
article.setTypeCode(code);
//更新商品信息
articleService.updateArticle(article);
request.setAttribute("tip", "商品更新成功!");
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
request.setAttribute("tip", "商品更新失败:"+e.getMessage());
}
return "redirect:/article_manager/getAll.action";
@RequestMapping("/article_manager")
public class ManagerArticleController {
@Autowired
private ManagerArticleServiceI articleService;
@Autowired
private ManagerArticleTypeServiceI articleTypeService;
//展示首页商品信息
@RequestMapping("/getAll.action")
public String getAllArticles(Model model,String typeCode,String title,PageModel pageModel) {
//根据商品类型以及商品的标题获取商品信息
List<Article> articles = articleService.getAllArticles(typeCode,title,pageModel);
model.addAttribute("articles", articles);
model.addAttribute("pageModel", pageModel);
model.addAttribute("title", title);
model.addAttribute("typeCode", typeCode);
//获取所有的一级商品类型
List<ArticleType> articleTypes = articleTypeService.findAllFirstArticleType();
model.addAttribute("firstArticleTypes", articleTypes);
return "article/list";
}
//展示商品明细信息
@RequestMapping("/preArticle.action")
public String preArticle(Model model,Integer id) {
//根据商品的id获取商品信息
Article article = articleService.getArticleById(id);
model.addAttribute("article", article);
return "article/preArticle";
}
//商品信息下架
@RequestMapping("/removeArticle.action")
public String removeArticle(Model model,Integer id) {
try {
InputStream input = file.getInputStream();
//获取项目部署路径
String realPath = request.getServletContext().getRealPath("/image/article");
//如果上传所指定的目录不存在则创建目录
File f = new File(realPath);
if(!f.exists()) {
f.mkdirs();
}
//获取当前上传的文件的名字 a.jpg
String imageName = file.getOriginalFilename();
//重新生成新的文件名
String newFileName = UUID.randomUUID().toString() + imageName.substring(imageName.lastIndexOf("."), imageName.length());
System.out.println("文件路径:"+(realPath+File.separator+newFileName));
//构建输出流
OutputStream out = new FileOutputStream(realPath+File.separator+newFileName);
byte[] data = new byte[1024];
int len = 0;
while((len = input.read(data)) != -1) {
out.write(data, 0, len);
out.flush();
}
out.close();
input.close();
//指定商品类型
article.setTypeCode(code);
article.setImage(newFileName);
//保存商品信息
articleService.saveArticle(article);
*/
public class LoginFilter extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
//从session中获取用户信息
User user = (User)request.getSession().getAttribute("session_user");
if(user == null) {
request.setAttribute("error_message", "您尚未登录,请登录后再进行相关操作!");
//跳转至登录页面
request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request, response);
return false;
}else {
return true;
}
}
}
/**
*
*/
public interface ShopCarServiceI {
/**
* @param session
* @param id
* @param number
* 将商品信息加入至购物车
*/
void addArticleToShopCar(HttpSession session, int id, int number);
/**
* @param session
* @return
* 展示购物车中的商品信息
*/
List<Shopcar> getAllShopCarByUserId(HttpSession session);
* @param number
* 更新购物车中商品的购物数量
*/
void updateShopcar(HttpSession session, int id, int number);
/**
* @param session
* @param id
* //删除购物车中商品的购物数量
*/
void deleteShopcar(HttpSession session, int id);
}
/**
*
*/
public interface ManagerUserServiceI {
/**
* @param loginName
* @param password
* @return
* 根据账号以及密码获取用户信息
*/
User getUserByNameAndPass(User user);
/**
* @param user
* //用户信息注册
*/
void saveUser(User user,HttpServletRequest request) throws Exception;
/**
* @param activeCode
* //用户信息激活
*/
void activeUser(String activeCode);
/**
* @param loginName
* @return
@Service(value="muserService")
@Transactional
public class ManagerUserServicempl implements ManagerUserServiceI {
@Autowired
private ManagerUserMapper userMapper;
/* (non-Javadoc)
* 根据账号以及密码获取用户信息
*/
@Override
public User getUserByNameAndPass(User user) {
// TODO Auto-generated method stub
return userMapper.getUserByNameAndPass(user);
}
/* (non-Javadoc)
* //用户信息注册
*/
@Override
public void saveUser(User user,HttpServletRequest request) throws Exception {
// TODO Auto-generated method stub
user.setCreateDate(new Date());
//通过UUID生成激活码
String activeCode = UUID.randomUUID().toString();
user.setActive(activeCode);
userMapper.saveUser(user);
/*******************************邮件发送*********************************/
//1、创建 Properties对象用于封装邮件服务器相关信息
Properties props = new Properties();
// 服务器地址
props.setProperty("mail.smtp.host", "smtp.126.com");
InputStream input = file.getInputStream();
//获取项目部署路径
String realPath = request.getServletContext().getRealPath("/image/article");
//如果上传所指定的目录不存在则创建目录
File f = new File(realPath);
if(!f.exists()) {
f.mkdirs();
}
//获取当前上传的文件的名字 a.jpg
String imageName = file.getOriginalFilename();
//重新生成新的文件名
String newFileName = UUID.randomUUID().toString() + imageName.substring(imageName.lastIndexOf("."), imageName.length());
System.out.println("文件路径:"+(realPath+File.separator+newFileName));
//构建输出流
OutputStream out = new FileOutputStream(realPath+File.separator+newFileName);
byte[] data = new byte[1024];
int len = 0;
while((len = input.read(data)) != -1) {
out.write(data, 0, len);
out.flush();
}
out.close();
input.close();
//指定商品类型
article.setTypeCode(code);
article.setImage(newFileName);
//保存商品信息
articleService.saveArticle(article);
request.setAttribute("tip", "商品添加成功!");
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
request.setAttribute("tip", "商品添加失败:"+e.getMessage());
}
return "redirect:/article_manager/getAll.action";
}
//跳转至修改商品信息页面
@RequestMapping("/showUpdate.action")
public String showUpdate(Model model,Integer id) {
//处理登录请求
@RequestMapping("/userLogin")
public String userLogin(User user,Model model,HttpSession session) {
User u = userService.getUserByNameAndPass(user);
if(u == null) {
//账号或密码不正确,给用户提示信息,并跳转至登录页面
model.addAttribute("tip", "您输入的账号或密码不正确请核实!");
return "user/login";
}else if("0".equals(u.getDisabled())){
model.addAttribute("tip", "您尚未激活,请激活后再进行相关操作!");
return "user/login";
}else {
//跳转至首页,并将用户的信息保存至session中
session.setAttribute(Constant.SESSION_USER, u);
return "redirect:/article_manager/getAll.action";
}
}
//用户退出
@RequestMapping("/logout")
public String logout(HttpSession session) {
//将用户的信息从session中清除
session.removeAttribute(Constant.SESSION_USER);
return "user/login";
}
//展示用户注册页面 showRegisterPage
@RequestMapping("/showRegisterPage")
public String showRegisterPage() {
return "user/register";
}
//用户信息注册
@RequestMapping("/userRegister")
public String userRegister(User user,Model model,HttpServletRequest request) {
try {
userService.saveUser(user,request);
model.addAttribute("tip", "注册成功!");
} catch (Exception e) {