位置: IT常识 - 正文

Educoder/头歌JAVA——Java Web:基于JSP的网上商城(头歌java第一课)

编辑:rootadmin
Educoder/头歌JAVA——Java Web:基于JSP的网上商城

目录

一、商品列表

本关任务

具体要求

结果输出

实现代码

二、商品详情

本关任务

JDBC查询方法封装

商品相关信息介绍

具体要求

结果输出

实现代码

三、商品搜索

编程要求

测试说明

实现代码

四、购物车列表

本关任务

JDBC查询方法封装

购物车相关信息介绍

编程要求

测试说明

实现代码

五、购物车操作

本关任务

JDBC查询方法封装

购物车相关信息介绍

编程要求

实现代码

六、下单

本关任务

编程要求

实现代码

七、订单查询

本关任务

编程要求

测试说明

实现代码


一、商品列表本关任务

推荐整理分享Educoder/头歌JAVA——Java Web:基于JSP的网上商城(头歌java第一课),希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:头歌educoder答案java第三章,头歌java中的异常答案,头歌educoder答案java第三章,头歌educoder实训怎么用,头歌educoder实训怎么用,头歌java第一课,头歌educoder网址,头歌educoder答案java,内容如对您有帮助,希望把文章链接给更多的朋友!

一个商场不能缺少商品,本关需要借助JDBC,从 t_goods表中获取销量前四的商品信息,并展示到页面。

JDBC信息;

MYSQL用户名MYSQL密码驱动URLroot123123com.mysql.jdbc.Driverjdbc:mysql://127.0.0.1:3306/online_shop?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true

com.educoder.entity.Goods字段及方法;

字段描述类型get方法set方法goodsId商品IdStringgetGoodsId()setGoodsId(String goodsId)goodsName商品名StringgetGoodsName()setGoodsName(String goodsName)goodsImg展示于主页的图StringgetGoodsImg() setGoodsImg(String goodsImg)goodsPrice价格BigDecimalgetGoodsPrice()setGoodsPrice(BigDecimal goodsPrice)goodsNum库存数量IntegergetGoodsNum()setGoodsNum(Integer goodsNum)salesNum销售数量IntegergetSalesNum()setSalesNum(Integer salesNum)goodsSize商品规格StringgetGoodsSize()setGoodsSize(String goodsSize)goodsFrom商品产地StringgetGoodsFrom()setGoodsFrom(String goodsFrom)goodsTime保质期StringgetGoodsTime()setGoodsTime(String goodsTime)goodsSaveCondition存储条件StringgetGoodsSaveCondition()setGoodsSaveCondition(String goodsSaveCondition)goodsDescribe商品描述介绍StringgetGoodsDescribe()setGoodsDescribe(String goodsDescribe)goodsExplain对商品简短说明StringgetGoodsExplain()setGoodsExplain(String goodsExplain)goodsClass所属类别StringgetGoodsClass()setGoodsClass(String goodsClass)goodsDiscount折扣BigDecimalgetGoodsDiscount()setGoodsDiscount(BigDecimal goodsDiscount)discountStartTime优惠起始时间DategetDiscountStartTime()setDiscountStartTime(Date discountStartTime)discountEndTime优惠截止时间DategetDiscountEndTime()setDiscountEndTime(Date discountEndTime)

表和类对应表;

库名表名类名online_shopt_goodsGoods注意:类字段和对应的表字段名称一致,这里不再重复列出了。

页面初始效果图:

最终页面效果图:

具体要求补全 getGoodsList()方法,返回List<Goods>(商品列表)。结果输出[{"goodsClass":"甜品","goodsDescribe":"口感绵密,精致细腻\r\n这份雪白让你有如获至宝的幸福感","goodsExplain":"手工打发而成的进口鲜奶油","goodsFrom":"上海","goodsId":"list1","goodsImg":"list_1-1.jpg","goodsName":"雪域牛乳芝士","goodsNum":996,"goodsPrice":98.00,"goodsSaveCondition":"冷藏0~4摄氏度","goodsSize":"约450g","goodsTime":"3天","salesNum":44},{"goodsClass":"甜品","goodsDescribe":"酸甜清爽\r\n回味无穷","goodsExplain":"卡福洛芒果泥","goodsFrom":"上海","goodsId":"list3","goodsImg":"list_3-1.jpg","goodsName":"芒果熔岩星球蛋糕 Mango Planet","goodsNum":1994,"goodsPrice":118.00,"goodsSaveCondition":"冷藏0~4摄氏度","goodsSize":"约680g","goodsTime":"2天","salesNum":42},{"goodsClass":"甜品","goodsDescribe":"通过精致的工艺\r\n将巧克力的香醇甜蜜发挥到极致\r\n每一口都倍感甜蜜","goodsExplain":"进口的黑巧克力","goodsFrom":"上海","goodsId":"list2","goodsImg":"list_2-1.jpg","goodsName":"哈!蜜瓜蛋糕 Hey Melon","goodsNum":1496,"goodsPrice":99.00,"goodsSaveCondition":"冷藏0~4摄氏度","goodsSize":"约500g","goodsTime":"3天","salesNum":39},{"goodsClass":"甜品","goodsDescribe":"在金色芒果淋面的外衣下\r\n是芒果慕斯的柔情\r\n是芒果啫喱的牵挂\r\n还有丝丝香草戚风蛋糕的香气","goodsExplain":"手工打发而成的进口鲜奶油","goodsFrom":"上海","goodsId":"list5","goodsImg":"list_5-1.jpg","goodsName":"蟹蟹你偷吃 Yum Yum Cake","goodsNum":6999,"goodsPrice":88.00,"goodsSaveCondition":"冷藏0~4摄氏度","goodsSize":"约480g","goodsTime":"3天","salesNum":5}]实现代码package com.educoder.service.impl; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import com.educoder.entity.Goods; import com.educoder.service.GoodsService; import com.educoder.dao.impl.BaseDao; public class GoodsServiceImpl implements GoodsService { /** * 商品详情接口 */ public Goods getGoodsByGoodsId(String goodsId) { /********* Begin *********/ String sql = "select * from t_goods where goodsId = ?"; List<Object> parameters = new ArrayList<Object>(); parameters.add(goodsId); List<Goods> goodsList = null; try { goodsList = BaseDao.operQuery(sql, parameters, Goods.class); } catch (Exception e) { e.printStackTrace(); } return goodsList.size() == 0 ? null : goodsList.get(0); /********* End *********/ } /** * 商品搜索接口 */ public List<Goods> searchGoods(String condition) { /********* Begin *********/ String sql = "select * from t_goods where goodsName like ? or goodsClass like ? order by salesNum desc"; List<Goods> goodsList = null; condition = "%" + condition + "%"; List<Object> paramenter = new ArrayList<Object>(); paramenter.add(condition); paramenter.add(condition); try { goodsList = BaseDao.operQuery(sql, paramenter, Goods.class); } catch (Exception e) { e.printStackTrace(); } return goodsList; /********* End *********/ } /** * 商品列表接口 */ public List<Goods> getGoodsList() { /********* Begin *********/ String USERNAME = "root"; String PASSWORD = "123123"; String URL = "jdbc:mysql://127.0.0.1:3306/online_shop?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true"; Connection conn = null; PreparedStatement pste = null; ResultSet executeQuery = null; List<Goods> list = new ArrayList<Goods>(); String sql = "select * from t_goods order by salesNum desc limit 4"; try { conn = DriverManager.getConnection(URL, USERNAME, PASSWORD); pste = conn.prepareStatement(sql); executeQuery = pste.executeQuery(); while (executeQuery.next()) { Goods goods = new Goods(); goods.setGoodsId(executeQuery.getString("goodsId")); goods.setGoodsName(executeQuery.getString("goodsName")); goods.setGoodsImg(executeQuery.getString("goodsImg")); goods.setGoodsPrice(executeQuery.getBigDecimal("goodsPrice")); goods.setGoodsNum(executeQuery.getInt("goodsNum")); goods.setSalesNum(executeQuery.getInt("salesNum")); goods.setGoodsSize(executeQuery.getString("goodsSize")); goods.setGoodsFrom(executeQuery.getString("goodsFrom")); goods.setGoodsTime(executeQuery.getString("goodsTime")); goods.setGoodsSaveCondition(executeQuery.getString("goodsSaveCondition")); goods.setGoodsDescribe(executeQuery.getString("goodsDescribe")); goods.setGoodsExplain(executeQuery.getString("goodsExplain")); goods.setGoodsClass(executeQuery.getString("goodsClass")); goods.setGoodsDiscount(executeQuery.getBigDecimal("goodsDiscount")); goods.setDiscountStartTime(executeQuery.getDate("discountStartTime")); goods.setDiscountEndTime(executeQuery.getDate("discountEndTime")); list.add(goods); } } catch (SQLException e) { e.printStackTrace(); } finally { try { if (executeQuery != null) executeQuery.close(); if (pste != null) pste.close(); if (conn != null) conn.close(); } catch (SQLException e) { e.printStackTrace(); } } return list; /********* End *********/ } }二、商品详情本关任务

有了商品还需要看商品的详细信息,本关需要借助JDBC,通过参数商品Id从数据库中获取商品详情,展示到页面。为了完成本关任务,需要了解:

JDBC 查询方法封装;商品相关信息介绍。JDBC查询方法封装

为了方便JDBC操作,平台对JDBC查询方法进行了封装,封装类com.educoder.dao.impl.BaseDao。

public static <T> List<T> operQuery(String sql, List<Object> p, Class<T> cls) throws Exception { Connection conn = null; PreparedStatement pste = null; ResultSet rs = null; List<T> list = new ArrayList<T>(); conn = getConn(); try { pste = conn.prepareStatement(sql); if (p != null) { for (int i = 0; i < p.size(); i++) { pste.setObject(i + 1, p.get(i)); } } rs = pste.executeQuery(); ResultSetMetaData rsmd = rs.getMetaData(); while (rs.next()) { T entity = cls.newInstance(); for (int j = 0; j < rsmd.getColumnCount(); j++) { String col_name = rsmd.getColumnName(j + 1); Object value = rs.getObject(col_name); Field field = cls.getDeclaredField(col_name); field.setAccessible(true); field.set(entity, value); } list.add(entity); } } catch (SQLException e) { e.printStackTrace(); } finally { releaseAll(rs, pste, conn); } return list; }

例如,第一关可以使用如下方法,获取商品列表:

String sql = "select * from t_goods order by salesNum desc limit 4"; List<Goods> goodsList = null; try { goodsList = BaseDao.operQuery(sql, null, Goods.class); } catch (Exception e) { e.printStackTrace(); }商品相关信息介绍MYSQL用户名MYSQL密码驱动URLroot123123com.mysql.jdbc.Driverjdbc:mysql://127.0.0.1:3306/online_shop?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true

com.educoder.entity.Goods字段及方法;

字段描述类型get方法set方法goodsId商品IdStringgetGoodsId()setGoodsId(String goodsId)goodsName商品名StringgetGoodsName()setGoodsName(String goodsName)goodsImg展示于主页的图StringgetGoodsImg() setGoodsImg(String goodsImg)goodsPrice价格BigDecimalgetGoodsPrice()setGoodsPrice(BigDecimal goodsPrice)goodsNum库存数量IntegergetGoodsNum()setGoodsNum(Integer goodsNum)salesNum销售数量IntegergetSalesNum()setSalesNum(Integer salesNum)goodsSize商品规格StringgetGoodsSize()setGoodsSize(String goodsSize)goodsFrom商品产地StringgetGoodsFrom()setGoodsFrom(String goodsFrom)goodsTime保质期StringgetGoodsTime()setGoodsTime(String goodsTime)goodsSaveCondition存储条件StringgetGoodsSaveCondition()setGoodsSaveCondition(String goodsSaveCondition)goodsDescribe商品描述介绍StringgetGoodsDescribe()setGoodsDescribe(String goodsDescribe)goodsExplain对商品简短说明StringgetGoodsExplain()setGoodsExplain(String goodsExplain)goodsClass所属类别StringgetGoodsClass()setGoodsClass(String goodsClass)goodsDiscount折扣BigDecimalgetGoodsDiscount()setGoodsDiscount(BigDecimal goodsDiscount)discountStartTime优惠起始时间DategetDiscountStartTime()setDiscountStartTime(Date discountStartTime)discountEndTime优惠截止时间DategetDiscountEndTime()setDiscountEndTime(Date discountEndTime)

表和类对应表;

库名表名类名online_shopt_goodsGoods注意:类字段和对应的表字段名称一致,这里不再重复列出了。

页面初始效果图:

最终页面效果图:

具体要求补全getGoodsByGoodsId()方法,完成查询商品详情的任务,最后返回Goods(商品)。结果输出{"goodsClass":"甜品","goodsDescribe":"口感绵密,精致细腻\r\n这份雪白让你有如获至宝的幸福感","goodsExplain":"手工打发而成的进口鲜奶油","goodsFrom":"上海","goodsId":"list1","goodsImg":"list_1-1.jpg","goodsName":"雪域牛乳芝士","goodsNum":996,"goodsPrice":98.00,"goodsSaveCondition":"冷藏0~4摄氏度","goodsSize":"约450g","goodsTime":"3天","salesNum":44}实现代码package com.educoder.service.impl; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import com.educoder.entity.Goods; import com.educoder.service.GoodsService; import com.educoder.dao.impl.BaseDao; public class GoodsServiceImpl implements GoodsService { /** * 商品详情接口 */ public Goods getGoodsByGoodsId(String goodsId) { /********* Begin *********/ String sql = "select * from t_goods where goodsId = ?"; List<Object> parameters = new ArrayList<Object>(); parameters.add(goodsId); List<Goods> goodsList = null; try { goodsList = BaseDao.operQuery(sql, parameters, Goods.class); } catch (Exception e) { e.printStackTrace(); } return goodsList.size() == 0 ? null : goodsList.get(0); /********* End *********/ } /** * 商品搜索接口 */ public List<Goods> searchGoods(String condition) { /********* Begin *********/ String sql = "select * from t_goods where goodsName like ? or goodsClass like ? order by salesNum desc"; List<Goods> goodsList = null; condition = "%" + condition + "%"; List<Object> paramenter = new ArrayList<Object>(); paramenter.add(condition); paramenter.add(condition); try { goodsList = BaseDao.operQuery(sql, paramenter, Goods.class); } catch (Exception e) { e.printStackTrace(); } return goodsList; /********* End *********/ } /** * 商品列表接口 */ public List<Goods> getGoodsList() { /********* Begin *********/ String USERNAME = "root"; String PASSWORD = "123123"; String URL = "jdbc:mysql://127.0.0.1:3306/online_shop?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true"; Connection conn = null; PreparedStatement pste = null; ResultSet executeQuery = null; List<Goods> list = new ArrayList<Goods>(); String sql = "select * from t_goods order by salesNum desc limit 4"; try { conn = DriverManager.getConnection(URL, USERNAME, PASSWORD); pste = conn.prepareStatement(sql); executeQuery = pste.executeQuery(); while (executeQuery.next()) { Goods goods = new Goods(); goods.setGoodsId(executeQuery.getString("goodsId")); goods.setGoodsName(executeQuery.getString("goodsName")); goods.setGoodsImg(executeQuery.getString("goodsImg")); goods.setGoodsPrice(executeQuery.getBigDecimal("goodsPrice")); goods.setGoodsNum(executeQuery.getInt("goodsNum")); goods.setSalesNum(executeQuery.getInt("salesNum")); goods.setGoodsSize(executeQuery.getString("goodsSize")); goods.setGoodsFrom(executeQuery.getString("goodsFrom")); goods.setGoodsTime(executeQuery.getString("goodsTime")); goods.setGoodsSaveCondition(executeQuery.getString("goodsSaveCondition")); goods.setGoodsDescribe(executeQuery.getString("goodsDescribe")); goods.setGoodsExplain(executeQuery.getString("goodsExplain")); goods.setGoodsClass(executeQuery.getString("goodsClass")); goods.setGoodsDiscount(executeQuery.getBigDecimal("goodsDiscount")); goods.setDiscountStartTime(executeQuery.getDate("discountStartTime")); goods.setDiscountEndTime(executeQuery.getDate("discountEndTime")); list.add(goods); } } catch (SQLException e) { e.printStackTrace(); } finally { try { if (executeQuery != null) executeQuery.close(); if (pste != null) pste.close(); if (conn != null) conn.close(); } catch (SQLException e) { e.printStackTrace(); } } return list; /********* End *********/ } }三、商品搜索编程要求

请仔细阅读右侧代码,在Begin-End区域内进行代码补充,实现商品搜索的功能,具体需求如下:

补全searchGoods()方法,完成商品搜索的功能,最后返回List<Goods>(商品列表)。测试说明

平台会对你编写的代码进行测试:

测试输入:雪域牛

预期输出:

[{"goodsClass":"甜品","goodsDescribe":"口感绵密,精致细腻\r\n这份雪白让你有如获至宝的幸福感","goodsExplain":"手工打发而成的进口鲜奶油","goodsFrom":"上海","goodsId":"list1","goodsImg":"list_1-1.jpg","goodsName":"雪域牛乳芝士","goodsNum":996,"goodsPrice":98.00,"goodsSaveCondition":"冷藏0~4摄氏度","goodsSize":"约450g","goodsTime":"3天","salesNum":44}]实现代码package com.educoder.service.impl; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import com.educoder.entity.Goods; import com.educoder.service.GoodsService; import com.educoder.dao.impl.BaseDao; public class GoodsServiceImpl implements GoodsService { /** * 商品详情接口 */ public Goods getGoodsByGoodsId(String goodsId) { /********* Begin *********/ String sql = "select * from t_goods where goodsId = ?"; List<Object> parameters = new ArrayList<Object>(); parameters.add(goodsId); List<Goods> goodsList = null; try { goodsList = BaseDao.operQuery(sql, parameters, Goods.class); } catch (Exception e) { e.printStackTrace(); } return goodsList.size() == 0 ? null : goodsList.get(0); /********* End *********/ } /** * 商品搜索接口 */ public List<Goods> searchGoods(String condition) { /********* Begin *********/ String sql = "select * from t_goods where goodsName like ? or goodsClass like ? order by salesNum desc"; List<Goods> goodsList = null; condition = "%" + condition + "%"; List<Object> paramenter = new ArrayList<Object>(); paramenter.add(condition); paramenter.add(condition); try { goodsList = BaseDao.operQuery(sql, paramenter, Goods.class); } catch (Exception e) { e.printStackTrace(); } return goodsList; /********* End *********/ } /** * 商品列表接口 */ public List<Goods> getGoodsList() { /********* Begin *********/ String USERNAME = "root"; String PASSWORD = "123123"; String URL = "jdbc:mysql://127.0.0.1:3306/online_shop?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true"; Connection conn = null; PreparedStatement pste = null; ResultSet executeQuery = null; List<Goods> list = new ArrayList<Goods>(); String sql = "select * from t_goods order by salesNum desc limit 4"; try { conn = DriverManager.getConnection(URL, USERNAME, PASSWORD); pste = conn.prepareStatement(sql); executeQuery = pste.executeQuery(); while (executeQuery.next()) { Goods goods = new Goods(); goods.setGoodsId(executeQuery.getString("goodsId")); goods.setGoodsName(executeQuery.getString("goodsName")); goods.setGoodsImg(executeQuery.getString("goodsImg")); goods.setGoodsPrice(executeQuery.getBigDecimal("goodsPrice")); goods.setGoodsNum(executeQuery.getInt("goodsNum")); goods.setSalesNum(executeQuery.getInt("salesNum")); goods.setGoodsSize(executeQuery.getString("goodsSize")); goods.setGoodsFrom(executeQuery.getString("goodsFrom")); goods.setGoodsTime(executeQuery.getString("goodsTime")); goods.setGoodsSaveCondition(executeQuery.getString("goodsSaveCondition")); goods.setGoodsDescribe(executeQuery.getString("goodsDescribe")); goods.setGoodsExplain(executeQuery.getString("goodsExplain")); goods.setGoodsClass(executeQuery.getString("goodsClass")); goods.setGoodsDiscount(executeQuery.getBigDecimal("goodsDiscount")); goods.setDiscountStartTime(executeQuery.getDate("discountStartTime")); goods.setDiscountEndTime(executeQuery.getDate("discountEndTime")); list.add(goods); } } catch (SQLException e) { e.printStackTrace(); } finally { try { if (executeQuery != null) executeQuery.close(); if (pste != null) pste.close(); if (conn != null) conn.close(); } catch (SQLException e) { e.printStackTrace(); } } return list; /********* End *********/ } }四、购物车列表本关任务

网上看中的商品,暂时不买,还需要保存起来,这个需要通过购物车来实现。本关需要从session里获取属性名为user的User对象,并借助JDBC,通过表t_cart、t_goods获取用户购物车的商品内容,并展示到页面。

为了完成本关任务,需要了解:

JDBC查询方法封装;

购物车相关信息介绍。

JDBC查询方法封装

为了方便JDBC操作,平台对JDBC查询方法进行了封装,封装类com.educoder.dao.impl.BaseDao。

public static <T> List<T> operQuery(String sql, List<Object> p, Class<T> cls) throws Exception { Connection conn = null; PreparedStatement pste = null; ResultSet rs = null; List<T> list = new ArrayList<T>(); conn = getConn(); try { pste = conn.prepareStatement(sql); if (p != null) { for (int i = 0; i < p.size(); i++) { pste.setObject(i + 1, p.get(i)); } } rs = pste.executeQuery(); ResultSetMetaData rsmd = rs.getMetaData(); while (rs.next()) { T entity = cls.newInstance(); for (int j = 0; j < rsmd.getColumnCount(); j++) { String col_name = rsmd.getColumnName(j + 1); Object value = rs.getObject(col_name); Field field = cls.getDeclaredField(col_name); field.setAccessible(true); field.set(entity, value); } list.add(entity); } } catch (SQLException e) { e.printStackTrace(); } finally { releaseAll(rs, pste, conn); } return list; }

例如,第一关可以使用如下方法,获取商品列表:

String sql = "select * from t_goods order by salesNum desc limit 4";List<Goods> goodsList = null;try {goodsList = BaseDao.operQuery(sql, null, Goods.class);} catch (Exception e) {e.printStackTrace();}购物车相关信息介绍MYSQL用户名MYSQL密码驱动URLroot123123com.mysql.jdbc.Driverjdbc:mysql://127.0.0.1:3306/online_shop?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true

com.educoder.entity.Goods字段及方法;

字段描述类型get方法set方法goodsId商品IdStringgetGoodsId()setGoodsId(String goodsId)goodsName商品名StringgetGoodsName()setGoodsName(String goodsName)goodsImg展示于主页的图StringgetGoodsImg() setGoodsImg(String goodsImg)goodsPrice价格BigDecimalgetGoodsPrice()setGoodsPrice(BigDecimal goodsPrice)goodsNum库存数量IntegergetGoodsNum()setGoodsNum(Integer goodsNum)salesNum销售数量IntegergetSalesNum()setSalesNum(Integer salesNum)goodsSize商品规格StringgetGoodsSize()setGoodsSize(String goodsSize)goodsFrom商品产地StringgetGoodsFrom()setGoodsFrom(String goodsFrom)goodsTime保质期StringgetGoodsTime()setGoodsTime(String goodsTime)goodsSaveCondition存储条件StringgetGoodsSaveCondition()setGoodsSaveCondition(String goodsSaveCondition)goodsDescribe商品描述介绍StringgetGoodsDescribe()setGoodsDescribe(String goodsDescribe)goodsExplain对商品简短说明StringgetGoodsExplain()setGoodsExplain(String goodsExplain)goodsClass所属类别StringgetGoodsClass()setGoodsClass(String goodsClass)goodsDiscount折扣BigDecimalgetGoodsDiscount()setGoodsDiscount(BigDecimal goodsDiscount)discountStartTime优惠起始时间DategetDiscountStartTime()setDiscountStartTime(Date discountStartTime)discountEndTime优惠截止时间DategetDiscountEndTime()setDiscountEndTime(Date discountEndTime)

注意:Cart类冗余信息是通过和t_goods表关联查询出来的,不是存在下面的t_cart表里。

表和类对应表;

库名表名类名online_shopt_cartCart

注意:类字段和对应的表字段名称一致,这里不再重复列出了。

页面初始效果图:

最终页面效果图:

编程要求

根据提示,在右侧编辑器补充getGoodsByUserId方法代码,并返回Cart列表。

注意:后续关卡都是需要从request获取用户信息,从而获得userId。在本关卡获取成功后,后续关卡默认已经获取到userId,并当成接口参数传入,不再需要重复获取。

测试说明

平台会对你编写的代码进行测试:

预期输出:

[{"addTime":1543505629000,"buyNum":1,"cartId":101,"goodsClass":"甜品","goodsDescribe":"酸甜清爽\r\n回味无穷","goodsExplain":"卡福洛芒果泥","goodsFrom":"上海","goodsId":"list3","goodsImg":"list_3-1.jpg","goodsName":"芒果熔岩星球蛋糕 Mango Planet","goodsNum":1994,"goodsPrice":118.00,"goodsSaveCondition":"冷藏0~4摄氏度","goodsSize":"约680g","goodsTime":"2天","salesNum":42,"userId":"123456"},{"addTime":1543505631000,"buyNum":1,"cartId":102,"discountEndTime":1499184679000,"discountStartTime":1498923465000,"goodsClass":"甜品","goodsDescribe":"芝士与香草戚风被雪域奶油的浪漫笼罩\r\n每一口都充满快乐的滋味","goodsDiscount":0.75,"goodsExplain":"选用进口白巧克力","goodsFrom":"上海","goodsId":"list6","goodsImg":"list_6-1.jpg","goodsName":"美刀刀蛋糕 Ms. Golden","goodsNum":3000,"goodsPrice":99.00,"goodsSaveCondition":"冷藏0~4摄氏度","goodsSize":"约700g","goodsTime":"3天","salesNum":5,"userId":"123456"}]实现代码package com.educoder.service.impl; import java.util.ArrayList; import java.util.Date; import java.util.List; import javax.servlet.http.HttpServletRequest; import com.educoder.dao.impl.BaseDao; import com.educoder.entity.Cart; import com.educoder.entity.User; import com.educoder.service.CartService; public class CartServiceImpl implements CartService { // 查询购物车列表 public List<Cart> getGoodsByUserId(HttpServletRequest request) { /********* Begin *********/ User user = (User)request.getSession().getAttribute("user"); String userId = user.getUserId(); String sql = "select * from t_cart as a, t_goods as b where a.goodsId = b.goodsId and a.userId = ?"; List<Cart> cartList = null; List<Object> parameters = new ArrayList<Object>(); parameters.add(userId); try { cartList = BaseDao.operQuery(sql, parameters, Cart.class); } catch (Exception e) { e.printStackTrace(); } return cartList; /********* End *********/ } // 购物车操作接口 @Override public void doCartHandle(String userId, String goodsId, String buyNum, String oper) { /********* Begin *********/ String deletesql = "delete from t_cart where userId=? and goodsId=?"; String querysql = "select * from t_cart where userId = ? and goodsId = ?"; String updatesql = "update t_cart set buyNum=? where userId=? and goodsId=?"; String addsql = "insert into t_cart(userId, goodsId, buyNum, addTime) values(?, ?, ?, ?)"; List<Object> parameters = new ArrayList<Object>(); //删除 if(buyNum == null) { parameters.add(userId); parameters.add(goodsId); BaseDao.operUpdate(deletesql, parameters); } else{ //进行修改和添加 List<Cart> goodsList = null; parameters.add(userId); parameters.add(goodsId); try { goodsList = BaseDao.operQuery(querysql, parameters, Cart.class); parameters.clear(); } catch (Exception e) { e.printStackTrace(); } //进行添加 if(goodsList.isEmpty()) { parameters.add(userId); parameters.add(goodsId); parameters.add(buyNum); parameters.add(new Date()); BaseDao.operUpdate(addsql, parameters); } else { //进行修改 if(oper != null) { buyNum += goodsList.get(0).getBuyNum(); parameters.add(buyNum); parameters.add(userId); parameters.add(goodsId); BaseDao.operUpdate(updatesql, parameters); } } } /********* End *********/ } }五、购物车操作本关任务

为了能进行购物车的修改、添加、删除操作,本关需要借助JDBC,对数据库表t_cart、t_goods表进行操作。

为了完成本关任务,需要了解:

JDBC更新方法封装;

购物车相关信息介绍。

JDBC查询方法封装

为了方便JDBC操作,平台进行的JDBC更新方法进行了封装,封装类com.educoder.dao.impl.BaseDao。

public static boolean operUpdate(String sql, List<Object> p) { Connection conn = null; PreparedStatement pste = null; int res = 0; conn = getConn(); try { pste = conn.prepareStatement(sql); if (p != null) { for (int i = 0; i < p.size(); i++) { pste.setObject(i + 1, p.get(i)); } } res = pste.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } finally { releaseAll(null, pste, conn); } return res > 0;// }购物车相关信息介绍MYSQL用户名MYSQL密码驱动URLroot123123com.mysql.jdbc.Driverjdbc:mysql://127.0.0.1:3306/online_shop?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true

com.educoder.entity.Goods字段及方法;

字段描述类型get方法set方法goodsId商品IdStringgetGoodsId()setGoodsId(String goodsId)goodsName商品名StringgetGoodsName()setGoodsName(String goodsName)goodsImg展示于主页的图StringgetGoodsImg() setGoodsImg(String goodsImg)goodsPrice价格BigDecimalgetGoodsPrice()setGoodsPrice(BigDecimal goodsPrice)goodsNum库存数量IntegergetGoodsNum()setGoodsNum(Integer goodsNum)salesNum销售数量IntegergetSalesNum()setSalesNum(Integer salesNum)goodsSize商品规格StringgetGoodsSize()setGoodsSize(String goodsSize)goodsFrom商品产地StringgetGoodsFrom()setGoodsFrom(String goodsFrom)goodsTime保质期StringgetGoodsTime()setGoodsTime(String goodsTime)goodsSaveCondition存储条件StringgetGoodsSaveCondition()setGoodsSaveCondition(String goodsSaveCondition)goodsDescribe商品描述介绍StringgetGoodsDescribe()setGoodsDescribe(String goodsDescribe)goodsExplain对商品简短说明StringgetGoodsExplain()setGoodsExplain(String goodsExplain)goodsClass所属类别StringgetGoodsClass()setGoodsClass(String goodsClass)goodsDiscount折扣BigDecimalgetGoodsDiscount()setGoodsDiscount(BigDecimal goodsDiscount)discountStartTime优惠起始时间DategetDiscountStartTime()setDiscountStartTime(Date discountStartTime)discountEndTime优惠截止时间DategetDiscountEndTime()setDiscountEndTime(Date discountEndTime)

注意:Cart类冗余信息是需要通过和t_goods表关联查询出来的,不会存在下面的t_cart表里。

Educoder/头歌JAVA——Java Web:基于JSP的网上商城(头歌java第一课)

表和类对应表;

库名表名类名online_shopt_cartCart

注意:类字段和对应的表字段名称一致,这里不再重复列出了。

编程要求

根据提示,在右侧编辑器补充doCartHandle方法代码。

接口请求参数说明:

userId 用户Id;

goodsId 商品Id;

buyNum 添加或修改数量,当为null,表示删除;

oper 如果存在,添加buyNum;如果不存在,修改为buyNum。

测试输入:123456 list2 1 2

预期输出:插入成功 userId:123456 goodsId:list2 buyNum:1 oper:2

实现代码package com.educoder.service.impl; import java.util.ArrayList; import java.util.Date; import java.util.List; import javax.servlet.http.HttpServletRequest; import com.educoder.dao.impl.BaseDao; import com.educoder.entity.Cart; import com.educoder.entity.User; import com.educoder.service.CartService; public class CartServiceImpl implements CartService { // 查询购物车列表 public List<Cart> getGoodsByUserId(HttpServletRequest request) { /********* Begin *********/ User user = (User)request.getSession().getAttribute("user"); String userId = user.getUserId(); String sql = "select * from t_cart as a, t_goods as b where a.goodsId = b.goodsId and a.userId = ?"; List<Cart> cartList = null; List<Object> parameters = new ArrayList<Object>(); parameters.add(userId); try { cartList = BaseDao.operQuery(sql, parameters, Cart.class); } catch (Exception e) { e.printStackTrace(); } return cartList; /********* End *********/ } // 购物车操作接口 @Override public void doCartHandle(String userId, String goodsId, String buyNum, String oper) { /********* Begin *********/ String deletesql = "delete from t_cart where userId=? and goodsId=?"; String querysql = "select * from t_cart where userId = ? and goodsId = ?"; String updatesql = "update t_cart set buyNum=? where userId=? and goodsId=?"; String addsql = "insert into t_cart(userId, goodsId, buyNum, addTime) values(?, ?, ?, ?)"; List<Object> parameters = new ArrayList<Object>(); //删除 if(buyNum == null) { parameters.add(userId); parameters.add(goodsId); BaseDao.operUpdate(deletesql, parameters); } else{ //进行修改和添加 List<Cart> goodsList = null; parameters.add(userId); parameters.add(goodsId); try { goodsList = BaseDao.operQuery(querysql, parameters, Cart.class); parameters.clear(); } catch (Exception e) { e.printStackTrace(); } //进行添加 if(goodsList.isEmpty()) { parameters.add(userId); parameters.add(goodsId); parameters.add(buyNum); parameters.add(new Date()); BaseDao.operUpdate(addsql, parameters); } else { //进行修改 if(oper != null) { buyNum += goodsList.get(0).getBuyNum(); parameters.add(buyNum); parameters.add(userId); parameters.add(goodsId); BaseDao.operUpdate(updatesql, parameters); } } } /********* End *********/ } }六、下单本关任务

上一关已经完成了购物车操作功能开发,现在我们就可以编写下单接口进行购买商品了,本关需要借助JDBC,在t_order、t_order_child表中存储用户订单信息。

下单相关信息介绍;

MYSQL用户名MYSQL密码驱动URLroot123123com.mysql.jdbc.Driverjdbc:mysql://127.0.0.1:3306/online_shop?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true

com.educoder.entity.Order字段及方法;

字段描述类型get方法set方法orderId订单编号StringgetOrderId()setOrderId(String orderId)userId用户IdStringgetUserId()setUserId(String userId)orderTime订单时间DategetOrderTime()setOrderTime(Date orderTime)addressId订单地址StringgetAddressId()setAddressId()(String addressId)

com.educoder.entity.Order冗余信息字段及方法;

字段描述类型get方法set方法goodsId商品IdStringgetGoodsId()setGoodsId(String goodsId)goodsName商品名称StringgetGoodsName()setGoodsName(String goodsName)goodsImg商品图片StringgetGoodsImg()setGoodsImg(String goodsImg)goodsPrice下单价格BigDecimalgetGoodsPrice()setGoodsPrice(BigDecimal goodsPrice)

注意:Order类冗余信息是需要通过关联查询出来的,不会存在下面的t_order表里。

com.educoder.entity.OrderChildTable字段及方法;

字段描述类型get方法set方法id订单商品主键IntegergetId()setId(Integer id)orderId订单IdStringgetOrderId()setOrderId(String orderId)goodsId商品IdStringgetGoodsId()setGoodsId(String goodsId)buyNum购买数量IntegergetBuyNum()setBuyNum()(Integer buyNum)

表和类对应表;

库名表名类名online_shopt_orderOrderonline_shopt_order_childOrderChildTable

注意:类字段和对应的表字段名称一致,这里不再重复列出了。

编程要求

在右侧编辑器补充submitOrder方法代码,并返回Order类。

接口请求参数说明:

userId 用户Id;

addressId 地址Id;

goodsBuyNum 数组,购物车选中的商品数量;

chooseGoodId 数组,购物车选中的商品Id。

逻辑说明:

下单时orderId需要唯一,可以使用UUID;

表分为t_order订单表和t_order_child订单子表,因为下单商品有可能多个;

在下单时,需要调用上一关的购物车操作接口,进行购物车数据清空。

测试输入:123456 d01e153c-fc1f-4326-9956-cbf9582a9df7 1,1 list3,list6

预期输出:

清空购物车成功订单存储信息: userId:123456 addressId:d01e153c-fc1f-4326-9956-cbf9582a9df7订单商品存储信息:[{"buyNum":1,"goodsId":"list3","id":53},{"buyNum":1,"goodsId":"list6","id":54}]实现代码package com.educoder.service.impl;import java.util.ArrayList;import java.util.Date;import java.util.List;import java.util.UUID;import com.educoder.dao.impl.BaseDao;import com.educoder.entity.Order;import com.educoder.service.OrderService;public class OrderServiceImpl implements OrderService { private CartServiceImpl catService = new CartServiceImpl(); // 下单 public Order submitOrder(String userId, String addressId, String[] goodsBuyNum, String[] chooseGoodId) { /********* Begin *********/ Order order = new Order(); String ordersql = "insert into t_order(orderId, userId, orderTime, addressId) values(?, ?, ?, ?)"; String child_order_sql = "insert into t_order_child (orderId, goodsId, buyNum) values( ?, ?, ?) "; String orderId = UUID.randomUUID().toString(); List<Object> parameters = new ArrayList<Object>(); parameters.add(orderId); parameters.add(userId); parameters.add(new Date()); parameters.add(addressId); BaseDao.operUpdate(ordersql, parameters); for(int n = 0 ; n < chooseGoodId.length; n++) { parameters.clear(); parameters.add(orderId); parameters.add(chooseGoodId[n]); parameters.add(goodsBuyNum[n]); BaseDao.operUpdate(child_order_sql, parameters); //下单后,需要清空购物车 catService.doCartHandle(userId, chooseGoodId[n], null, null); } //返回带有订单id的订单 order.setOrderId(orderId); return order; /********* End *********/ } // 订单查询 public List<Order> getOrderByUserId(String userId) { /********* Begin *********/ String sql = "select A.orderId, A.userId, B.userName, A.orderTime, C.address, E.goodsId, E.goodsName, D.buyNum, E.goodsPrice from " + "t_order as A, t_user as B, t_address as C, t_order_child as D, t_goods as E " + "where A.userId=? and A.orderId=D.orderId and A.userId=B.userId and A.addressId=C.addressId and D.goodsId=E.goodsId " + "order by orderTime desc"; List<Object> parameters = new ArrayList<Object>(); parameters.add(userId); List<Order> orderList = null; try { orderList = BaseDao.operQuery(sql, parameters, Order.class); } catch (Exception e) { e.printStackTrace(); } return orderList; /********* End *********/ }}七、订单查询本关任务

上一关已经完成下单接口,本关需要完成查询订单列表,本关需要借助JDBC从t_order、t_user、t_address、t_order_child表中查询订单信息。

订单查询相关信息介绍;

MYSQL用户名MYSQL密码驱动URLroot123123com.mysql.jdbc.Driverjdbc:mysql://127.0.0.1:3306/online_shop?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true

com.educoder.entity.Order字段及方法;

字段描述类型get方法set方法orderId订单编号StringgetOrderId()setOrderId(String orderId)userId用户IdStringgetUserId()setUserId(String userId)orderTime订单时间DategetOrderTime()setOrderTime(Date orderTime)addressId订单地址StringgetAddressId()setAddressId()(String addressId)

com.educoder.entity.Order冗余信息字段及方法;

字段描述类型get方法set方法goodsId商品IdStringgetGoodsId()setGoodsId(String goodsId)goodsName商品名称StringgetGoodsName()setGoodsName(String goodsName)goodsImg商品图片StringgetGoodsImg()setGoodsImg(String goodsImg)goodsPrice下单价格BigDecimalgetGoodsPricesetGoodsPrice(BigDecimal goodsPrice)

注意:Order类冗余信息是需要通过关联查询出来的,不会存在下面的t_order表里。

com.educoder.entity.OrderChildTable字段及方法;

字段描述类型get方法set方法id订单商品主键IntegergetId()setId(Integer id)orderId订单IdStringgetOrderId()setOrderId(String orderId)goodsId商品IdStringgetGoodsId()setGoodsId(String goodsId)buyNum购买数量IntegergetBuyNum()setBuyNum()(Integer buyNum)

com.educoder.entity.Address字段及方法;

字段描述类型get方法set方法addressId地址IdStringgetAddressId()setAddressId(String addressId)userId用户IdStringgetUserId()setUserId(String userId)userName收货姓名StringgetUserName()setUserName(String userName)userTel收货电话StringgetUserTel()setUserTel()(String addressId)address收货地址StringgetAddress()setAddress()(String addressId)

表和类对应表;

库名表名类名online_shopt_orderOrderonline_shopt_order_childOrderChildTableonline_shopt_addressAddress

注意:类字段和对应的表字段名称一致,这里不再重复列出了。

页面初始效果图:

最终页面效果图:

编程要求

在右侧编辑器补充getOrderByUserId方法代码,并返回Goods列表。

接口请求参数说明:

userId 用户Id。测试说明

测试输入:123456

预期输出:

[{"address":"测试地址","buyNum":1,"goodsId":"list1","goodsName":"雪域牛乳芝士","goodsPrice":98.00,"orderId":"ab25ca41-2230-4af1-81f7-6cfe8872d6b0","orderTime":1543220250000,"userId":"123456","userName":"test"},{“address":"测试地址","buyNum":1,"goodsId":"list2","goodsName":"哈!蜜瓜蛋糕 Hey Melon","goodsPrice":99.00,"orderId":"ab25ca41-2230-4af1-81f7-6cfe8872d6b0","orderTime":1543220250000,"userId":"123456","userName":"test"}]实现代码package com.educoder.service.impl;import java.util.ArrayList;import java.util.Date;import java.util.List;import java.util.UUID;import com.educoder.dao.impl.BaseDao;import com.educoder.entity.Order;import com.educoder.service.OrderService;public class OrderServiceImpl implements OrderService { private CartServiceImpl catService = new CartServiceImpl(); // 下单 public Order submitOrder(String userId, String addressId, String[] goodsBuyNum, String[] chooseGoodId) { /********* Begin *********/ Order order = new Order(); String ordersql = "insert into t_order(orderId, userId, orderTime, addressId) values(?, ?, ?, ?)"; String child_order_sql = "insert into t_order_child (orderId, goodsId, buyNum) values( ?, ?, ?) "; String orderId = UUID.randomUUID().toString(); List<Object> parameters = new ArrayList<Object>(); parameters.add(orderId); parameters.add(userId); parameters.add(new Date()); parameters.add(addressId); BaseDao.operUpdate(ordersql, parameters); for(int n = 0 ; n < chooseGoodId.length; n++) { parameters.clear(); parameters.add(orderId); parameters.add(chooseGoodId[n]); parameters.add(goodsBuyNum[n]); BaseDao.operUpdate(child_order_sql, parameters); //下单后,需要清空购物车 catService.doCartHandle(userId, chooseGoodId[n], null, null); } //返回带有订单id的订单 order.setOrderId(orderId); return order; /********* End *********/ } // 订单查询 public List<Order> getOrderByUserId(String userId) { /********* Begin *********/ String sql = "select A.orderId, A.userId, B.userName, A.orderTime, C.address, E.goodsId, E.goodsName, D.buyNum, E.goodsPrice from " + "t_order as A, t_user as B, t_address as C, t_order_child as D, t_goods as E " + "where A.userId=? and A.orderId=D.orderId and A.userId=B.userId and A.addressId=C.addressId and D.goodsId=E.goodsId " + "order by orderTime desc"; List<Object> parameters = new ArrayList<Object>(); parameters.add(userId); List<Order> orderList = null; try { orderList = BaseDao.operQuery(sql, parameters, Order.class); } catch (Exception e) { e.printStackTrace(); } return orderList; /********* End *********/ }}
本文链接地址:https://www.jiuchutong.com/zhishi/289656.html 转载请保留说明!

上一篇:属于大堡礁的一部分的心形礁,澳大利亚昆士兰 (© Peter Adams/Offset by Shutterstock)(大堡礁的现状)

下一篇:莱达尔湖, 英格兰坎布里亚郡湖区 (© Tranquillian1/iStock/Getty Images Plus)

  • 个人所得税个人信息怎么填写
  • 新车车船税怎么交
  • 车位销售技巧
  • 餐费可以抵扣吗
  • 纳税申报 财务报表利润表
  • 实收资本实缴需要什么手续
  • 红字发票信息表需要盖章吗
  • 坏账准备期末应有余额怎么算
  • 购买完货物又退回怎么办
  • 企业所得税跨期费用的相关规定
  • 航天金税交499
  • 外币报表折算差额在会计报表中应作为
  • 应收账款贷方余额表示什么
  • 小规模纳税人减免税收政策
  • 低价股权转让是利空还是利好
  • 企业购买黄金如何处理
  • 转增股本是股票股利吗
  • 新成立的公司季报
  • 怎么计提所得税费用
  • 债务免除的税务处理
  • 转籍车辆是什么意思
  • 无形资产怎么衡量
  • 房租费进项税能不能抵扣
  • 出口货物退免税凭证资料应当保存几年
  • 从租计征的房产税需要缴纳土地使用税吗
  • 出纳人员赔偿
  • 当月费用下月付款
  • 如何设置电源键关闭屏幕
  • 清除cmos数据按钮一直亮
  • 附有退货权商品是什么
  • 资产和负债的计税基础金额的确认思维导图
  • 分配结转制造费用时借方科目
  • 股权投资的收益回收方式
  • 云杉树下面长什么蘑菇
  • laravel视频教程
  • php数组按照某个字段排序
  • 跨年调整收入增值税怎么办
  • 教育行业财务处理流程
  • 非独立核算的分公司怎么做账
  • 二维码的实现原理和实现过程
  • js匿名函数和命名函数
  • 机械租赁税率是多少?
  • 房租发票需要写备注吗
  • 增值税专用发票几个点
  • 固定资产有内容限制吗
  • 出口免税不退税会计分录
  • sql数据库聚集索引和非聚集索引的区别是什么?
  • 员工的提成
  • 出口退免税的条件
  • 增值税的视同销售行为有哪些?
  • 充值优惠怎么写
  • 固定资产资本化后续支出
  • 期间费用明细表必须填吗
  • 企业收到海河工厂发运的乙材料,并验收入库
  • 交去年的企业所得税吗
  • 支付给外包公司的工资和保险费
  • 销售的差额如何分录
  • 其他业务支出包括哪些内容科目
  • SQL Server Alwayson创建代理作业的注意事项详解
  • centos7.6无法远程
  • SQL Server2005、2008如何彻底删除卸载并重新安装?
  • xp系统弹出广告怎么处理
  • 安装win7旗舰版用户名和密码是多少
  • zmweb.exe是什么进程
  • 苹果mac系统关机怎么关
  • wlms.exe是什么
  • windows10x预览版
  • Javascript之Number对象介绍
  • js数组菜鸟教程
  • Javascript中的this
  • Node.js微信公众号开发
  • Android include 标签注意点
  • js 数组去重的四种方法
  • android下载图片并保存到本地
  • 电子税务局领取纸质发票的步骤
  • 关于成品油的吨油换算
  • 湖南省国家税务局历任局长
  • 舟山自贸区税收优惠政策五减五免
  • 税控机怎么开发票
  • 山东省地方税务局关于个人所得税
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设