Java 实战项目锤炼之网上商城系统的实现流程
作者:qq_1334611189 时间:2022-06-26 17:21:47
一、项目简述(+需求文档+PPT)
功能: 主页显示热销商品;所有商品展示,可进行商品搜索;点 击商品进入商品详情页,显示库存,具有立即购买和加入 购物车功能,可增减购买商品数量亦可手动输入(同时验证 库存),热销商品展示。立即购买进入确认订单页面,可选 择已经添加的地址,亦可新增地址。(同时验证库存),可 选择购买哪些商品,可删除不需要的商品。点击结算进入 确认订单页面,确认后提交订单,订单重复提交给予响 应,库存不足或商品下架给予响应。后台管理:(修改密码 等),商品管理(商品批量添加、上下架、库存维护等), 订单管理。
二、项目运行
环境配置: Jdk1.8 + Tomcat8.5 + mysql + Eclispe ( IntelliJ IDEA,Eclispe,MyEclispe,Sts 都支持)
项目技术: JSP + C3P0+ Servlert + html+ css + JavaScript + JQuery + Ajax + Fileupload等等。
后台管理-主页操作代码:
/**
* 后台管理-主页
*/
@Controller
public class AdminHomeController extends BaseController {
@Resource(name = "adminService")
private AdminService adminService;
@Resource(name = "productOrderService")
private ProductOrderService productOrderService;
@Resource(name = "productService")
private ProductService productService;
@Resource(name = "userService")
private UserService userService;
/**
* 转到后台管理-主页
* @param session session对象
* @param map 前台传入的Map
* @return 响应数据
* @throws ParseException 转换异常
*/
@RequestMapping(value = "admin", method = RequestMethod.GET)
public String goToPage(HttpSession session, Map<String, Object> map) throws ParseException {
logger.info("获取管理员信息");
Object adminId = checkAdmin(session);
if (adminId == null) {
return "redirect:/admin/login";
}
Admin admin = adminService.get(null, Integer.parseInt(adminId.toString()));
map.put("admin", admin);
logger.info("获取统计信息");
//产品总数
Integer productTotal = productService.getTotal(null, new Byte[]{0, 2});
//用户总数
Integer userTotal = userService.getTotal(null);
//订单总数
Integer orderTotal = productOrderService.getTotal(null, new Byte[]{3});
logger.info("获取图表信息");
map.put("jsonObject", getChartData(null,null,7));
map.put("productTotal", productTotal);
map.put("userTotal", userTotal);
map.put("orderTotal", orderTotal);
logger.info("转到后台管理-主页");
return "admin/homePage";
}
/**
* 转到后台管理-主页(ajax方式)
* @param session session对象
* @param map 前台传入的Map
* @return 响应数据
* @throws ParseException 转换异常
*/
@RequestMapping(value = "admin/home", method = RequestMethod.GET)
public String goToPageByAjax(HttpSession session, Map<String, Object> map) throws ParseException {
logger.info("获取管理员信息");
Object adminId = checkAdmin(session);
if (adminId == null) {
return "admin/include/loginMessage";
}
Admin admin = adminService.get(null, Integer.parseInt(adminId.toString()));
map.put("admin", admin);
logger.info("获取统计信息");
Integer productTotal = productService.getTotal(null, new Byte[]{0, 2});
Integer userTotal = userService.getTotal(null);
Integer orderTotal = productOrderService.getTotal(null, new Byte[]{3});
logger.info("获取图表信息");
map.put("jsonObject", getChartData(null, null,7));
logger.info("获取图表信息");
map.put("jsonObject", getChartData(null,null,7));
map.put("productTotal", productTotal);
map.put("userTotal", userTotal);
map.put("orderTotal", orderTotal);
logger.info("转到后台管理-主页-ajax方式");
return "admin/homeManagePage";
}
/**
* 按日期查询图表数据(ajax方式)
* @param beginDate 开始日期
* @param endDate 结束日期
* @return 响应数据
* @throws ParseException 转换异常
*/
@ResponseBody
@RequestMapping(value = "admin/home/charts", method = RequestMethod.GET, produces = "application/json;charset=utf-8")
public String getChartDataByDate(@RequestParam(required = false) String beginDate, @RequestParam(required = false) String endDate) throws ParseException {
if (beginDate != null && endDate != null) {
//转换日期格式
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
return getChartData(simpleDateFormat.parse(beginDate), simpleDateFormat.parse(endDate),7).toJSONString();
} else {
return getChartData(null, null,7).toJSONString();
}
}
/**
* 按日期获取图表数据
* @param beginDate 开始日期
* @param endDate 结束日期
* @param days 天数
* @return 图表数据的JSON对象
* @throws ParseException 转换异常
*/
private JSONObject getChartData(Date beginDate,Date endDate,int days) throws ParseException {
JSONObject jsonObject = new JSONObject();
SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd", Locale.UK);
SimpleDateFormat time2 = new SimpleDateFormat("MM/dd", Locale.UK);
SimpleDateFormat timeSpecial = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.UK);
//如果没有指定开始和结束日期
if (beginDate == null || endDate == null) {
//指定一周前的日期为开始日期
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, 1-days);
beginDate = time.parse(time.format(cal.getTime()));
//指定当前日期为结束日期
cal = Calendar.getInstance();
endDate = cal.getTime();
} else {
beginDate = time.parse(time.format(beginDate));
endDate = timeSpecial.parse(time.format(endDate) + " 23:59:59");
}
logger.info("根据订单状态分类");
//未付款订单数统计数组
int[] orderUnpaidArray = new int[7];
//未发货订单数统计叔祖
int[] orderNotShippedArray = new int[7];
//未确认订单数统计数组
int[] orderUnconfirmedArray = new int[7];
//交易成功订单数统计数组
int[] orderSuccessArray = new int[7];
//总交易订单数统计数组
int[] orderTotalArray = new int[7];
logger.info("从数据库中获取统计的订单集合数据");
List<OrderGroup> orderGroupList = productOrderService.getTotalByDate(beginDate, endDate);
//初始化日期数组
JSONArray dateStr = new JSONArray(days);
//按指定的天数进行循环
for (int i = 0; i < days; i++) {
//格式化日期串(MM/dd)并放入日期数组中
Calendar cal = Calendar.getInstance();
cal.setTime(beginDate);
cal.add(Calendar.DATE, i);
String formatDate = time2.format(cal.getTime());
dateStr.add(formatDate);
//该天的订单总数
int orderCount = 0;
//循环订单集合数据的结果集
for(int j = 0; j < orderGroupList.size(); j++){
OrderGroup orderGroup = orderGroupList.get(j);
//如果该订单日期与当前日期一致
if(orderGroup.getProductOrder_pay_date().equals(formatDate)){
//从结果集中移除数据
orderGroupList.remove(j);
//根据订单状态将统计结果存入对应的订单状态数组中
switch (orderGroup.getProductOrder_status()) {
case 0:
//未付款订单
orderUnpaidArray[i] = orderGroup.getProductOrder_count();
break;
case 1:
//未发货订单
orderNotShippedArray[i] = orderGroup.getProductOrder_count();
break;
case 2:
//未确认订单
orderUnconfirmedArray[i] = orderGroup.getProductOrder_count();
break;
case 3:
//交易成功订单
orderSuccessArray[i] = orderGroup.getProductOrder_count();
break;
}
//累加当前日期的订单总数
orderCount += orderGroup.getProductOrder_count();
}
}
//将统计的订单总数存入总交易订单数统计数组
orderTotalArray[i] = orderCount;
}
logger.info("返回结果集map");
jsonObject.put("orderTotalArray", orderTotalArray);
jsonObject.put("orderUnpaidArray", orderUnpaidArray);
jsonObject.put("orderNotShippedArray", orderNotShippedArray);
jsonObject.put("orderUnconfirmedArray", orderUnconfirmedArray);
jsonObject.put("orderSuccessArray", orderSuccessArray);
jsonObject.put("dateStr",dateStr);
return jsonObject;
}
}
前台主页代码:
/**
* 主页
*/
@Controller
public class ForeHomeController extends BaseController {
@Resource(name = "userService")
private UserService userService;
@Resource(name="categoryService")
private CategoryService categoryService;
@Resource(name="productService")
private ProductService productService;
@Resource(name="productImageService")
private ProductImageService productImageService;
//转到前台天猫-主页
@RequestMapping(value = "/", method = RequestMethod.GET)
public String goToPage(HttpSession session, Map<String, Object> map) {
logger.info("检查用户是否登录");
Object userId = checkUser(session);
if (userId != null) {
logger.info("获取用户信息");
User user = userService.get(Integer.parseInt(userId.toString()));
map.put("user", user);
}
logger.info("获取产品分类列表");
List<Category> categoryList = categoryService.getList(null,null);
logger.info("获取每个分类下的产品列表");
for(Category category : categoryList){
List<Product> productList = productService.getList(
new Product().setProduct_category(category),
new Byte[]{0, 2},
new OrderUtil("product_sale_count", true), new PageUtil(0, 8)
);
if (productList != null) {
for (Product product : productList) {
Integer product_id = product.getProduct_id();
product.setSingleProductImageList(
productImageService.getList(
product_id, (byte) 0, new PageUtil(0, 1)
)
);
}
}
category.setProductList(productList);
}
map.put("categoryList",categoryList);
logger.info("获取促销产品列表");
List<Product> specialProductList = productService.getList(
null, new Byte[]{2}, null, new PageUtil(0, 6)
);
map.put("specialProductList", specialProductList);
logger.info("转到前台主页");
return "fore/homePage";
}
//转到前台天猫-错误页
@RequestMapping(value = "error", method = RequestMethod.GET)
public String goToErrorPage() {
return "fore/errorPage";
}
//获取主页分类下产品信息-ajax
@ResponseBody
@RequestMapping(value = "product/nav/{category_id}", method = RequestMethod.GET, produces = "application/json;charset=utf-8")
public String getProductByNav(@PathVariable("category_id") Integer category_id) {
JSONObject object = new JSONObject();
if (category_id == null) {
object.put("success", false);
return object.toJSONString();
}
logger.info("获取分类ID为{}的产品标题数据", category_id);
List<Product> productList = productService.getTitle(
new Product().setProduct_category(new Category().setCategory_id(category_id)),
new PageUtil(0, 40)
);
List<List<Product>> complexProductList = new ArrayList<>(8);
List<Product> products = new ArrayList<>(5);
for (int i = 0; i < productList.size(); i++) {
//如果临时集合中产品数达到5个,加入到产品二维集合中,并重新实例化临时集合
if (i % 5 == 0) {
complexProductList.add(products);
products = new ArrayList<>(5);
}
products.add(productList.get(i));
}
complexProductList.add(products);
Category category = new Category().setCategory_id(category_id).setComplexProductList(complexProductList);
object.put("success", true);
object.put("category", category);
return object.toJSONString();
}
}
来源:https://blog.csdn.net/m0_59687645/article/details/121224704
标签:Java,网上商城系统,实战项目
0
投稿
猜你喜欢
Java源码解析之object类
2023-11-05 00:46:28
java高并发锁的3种实现示例代码
2022-05-23 16:45:47
Java类和成员变量声明类详解
2023-02-13 18:54:46
Android 字符串中某个字段可点击和设置颜色的方法
2023-07-24 00:16:53
java实现简单石头剪刀布游戏
2023-07-20 05:31:37
C#中Lambda表达式的用法
2022-07-29 17:29:56
springboot整合mybatis plus与druid详情
2022-07-31 14:22:14
c# 获取字符串的字节数的方法
2022-01-17 13:52:50
java9开始——接口中可以定义private私有方法
2023-03-27 06:53:15
SpringBoot项目jar发布后如何获取jar包所在目录路径
2023-02-23 08:36:04
WebSocket实现Web聊天室功能
2023-11-27 06:10:52
Spring Boot利用JSR303实现参数验证的方法实例
2022-07-28 20:36:26
Hashmap非线程安全关于hash值冲突处理
2023-11-11 09:22:10
Java 判断实体对象及所有属性是否为空的操作
2022-12-06 14:32:07
C#委托delegate实例解析
2021-07-28 08:58:26
Java使用BigDecimal精确运算浮点数
2023-08-11 09:47:11
解决Spring Batch框架job任务只跑一次的问题
2023-01-07 00:13:53
Java日常练习题,每天进步一点点(55)
2022-10-13 15:51:03
解决genymotion模拟器无法联网的正确方法100%成功
2023-09-04 23:20:54
Java带返回值的方法的定义和调用详解
2022-10-05 21:01:09