欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

用eclipse实现电商平台系统(SpringBoot+Thymeleaf+MyBatis)

时间:2023-08-15

1.开发工具:eclipse、Spring Boot+Thymeleaf + MyBatis、MySQL8.0

2.项目功能:

游客浏览商品,注册用户浏览商品,管理员后台增删改查商品
商品类型(goodstype)的增删查
商品(goodstable)的增删改查
后台顾客信息的删除,注册界面顾客的添加
查看或加入购物车,查看或加入收藏夹,查看或加入用户个人订单,查看用户个人信息。

3.其他功能
后台上传前台商品照片到mysql,前台验证码登录,异常界面处理,商品关键字搜索,前台轮播图展示,后台设置前台的推荐商品栏,后台设置前台的新添加商品栏,后台使用分页查询,前台模拟订单支付。

4.数据库表

在MySQL8中创建数据库shop1
创建8张与系统相关的数据表: ausertable 、busertable、carttable、focustable、goodstable、goodstype、orderdetail和orderbasetable.
 

一、eclipse中新建spring boot项目。  二、建立如下项目目录的文件。

1.1.1系统设计

电面务平台分为两个子系统,一是后台管理子系统, 二是前台电子商务子系统,下面分别说明这两个子系统的功能需求与模块划分。

1.1.1系统功能需求

1、后台管理子系统

后台管理子系统要求管理员登录成功后,才能对商品进行管理,包括添加商品、查询商品、修改商品以及删除商品。除商品管理外,管理员还需要对商品类型、注册用户以及用户的订单等进行管理。

2、前台电子商务子系统

1)非注册用户

非注册用户或未登录用户具有的功能如下:浏览首页、查看商品详情以及搜索商品。

2)用户

成功登录的用户除具有未登录用户具有的功能外,还具有购买商品、查看购物车、收藏商品、查看订单、查看收藏以及查看用户个人信息的功能。

三、编写HTML代码

addGood.html 

商品类型添加页面


添加商品

addType.html 

商品类型添加页面


添加类型

allOrder.html

主页订单列表订单ID用户邮箱订单金额订单状态下单日期

第页共页上一页下一页

allUser.html

主页


商品列表用户ID用户邮箱删除删除

第页共页上一页下一页

四、spring boot层级
SpringBoot分为四层: controller层、 service层 、dao层、entity层 
1.entity层:和model层- -样, 存放的是实体类,属性
值与数据库值保持一致, 实现setter和getter方法

AUser.java

package com.ch.ebusiness.entity;public class AUser {private String aname;private String apwd;public String getAname() {return aname;}public void setAname(String aname) {this.aname = aname;}public String getApwd() {return apwd;}public void setApwd(String apwd) {this.apwd = apwd;}}

 Buser.java

package com.ch.ebusiness.entity;import javax.validation.constraints.Email;import javax.validation.constraints.NotBlank;import org.hibernate.validator.constraints.Length;public class BUser {private Integer id;@NotBlank(message="邮箱必须输入!")@Email(message="邮箱格式不正确!")private String bemail;@NotBlank(message="密码必须输入!")@Length(min=6, max=20, message="密码长度在6到20之间!")private String bpwd;private String rebpwd;private String code;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getBemail() {return bemail;}public void setBemail(String bemail) {this.bemail = bemail;}public String getBpwd() {return bpwd;}public void setBpwd(String bpwd) {this.bpwd = bpwd;}public String getCode() {return code;}public void setCode(String code) {this.code = code;}public String getRebpwd() {return rebpwd;}public void setRebpwd(String rebpwd) {this.rebpwd = rebpwd;}}

 Goods.java

package com.ch.ebusiness.entity;import org.springframework.web.multipart.MultipartFile;public class Goods {private int id;private String gname;private double goprice;private double grprice;private int gstore;private String gpicture;private MultipartFile fileName;private int goodstype_id;private String typename;private int buyNumber;//加入购物车使用private int isAdvertisement;private int isRecommend;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getGname() {return gname;}public void setGname(String gname) {this.gname = gname;}public double getGoprice() {return goprice;}public void setGoprice(double goprice) {this.goprice = goprice;}public double getGrprice() {return grprice;}public void setGrprice(double grprice) {this.grprice = grprice;}public int getGstore() {return gstore;}public void setGstore(int gstore) {this.gstore = gstore;}public String getGpicture() {return gpicture;}public void setGpicture(String gpicture) {this.gpicture = gpicture;}public int getGoodstype_id() {return goodstype_id;}public void setGoodstype_id(int goodstype_id) {this.goodstype_id = goodstype_id;}public String getTypename() {return typename;}public void setTypename(String typename) {this.typename = typename;}public int getBuyNumber() {return buyNumber;}public void setBuyNumber(int buyNumber) {this.buyNumber = buyNumber;}public int getIsAdvertisement() {return isAdvertisement;}public void setIsAdvertisement(int isAdvertisement) {this.isAdvertisement = isAdvertisement;}public int getIsRecommend() {return isRecommend;}public void setIsRecommend(int isRecommend) {this.isRecommend = isRecommend;}public MultipartFile getFileName() {return fileName;}public void setFileName(MultipartFile fileName) {this.fileName = fileName;}}

 
2.dao层:即mapper层,对数据库进行持久化操作,他的方法是针对数据库操作额,基本、上用的就是增删改查,就是一个接口,只有方法名,具体实现在mapper.xmI中。

GoodsRepository.java

package com.ch.ebusiness.repository.admin;import java.util.List;import java.util.Map;import org.apache.ibatis.annotations.Mapper;import org.apache.ibatis.annotations.Param;import com.ch.ebusiness.entity.Goods;import com.ch.ebusiness.entity.GoodsType;@Mapperpublic interface GoodsRepository {int addGoods(Goods goods);int updateGoods(Goods goods);Goods selectAGoods(Integer id);int selectAllGoods();List selectAllGoodsType();List selectAllGoodsByPage(@Param("startIndex") int startIndex, @Param("perPageSize") int perPageSize);int deleteAGoods(Integer id);List> selectFocusGoods(Integer id);List> selectCartGoods(Integer id);List> selectOrderGoods(Integer id);}

TypeRepository.java

package com.ch.ebusiness.repository.admin;import java.util.List;import org.apache.ibatis.annotations.Mapper;import org.apache.ibatis.annotations.Param;import com.ch.ebusiness.entity.Goods;import com.ch.ebusiness.entity.GoodsType;@Mapperpublic interface TypeRepository {int selectAll();List selectAllTypeByPage(@Param("startIndex") int startIndex, @Param("perPageSize") int perPageSize);int deleteType(int id);List selectGoods(int goodstype_id);int addType(GoodsType goodsType);}

AdminMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>select gt.*, gy.typename from goodstable gt,goodstype gy where gt.goodstype_id = gy.id and gt.isAdvertisement = 1order by gt.id desc limit 5 select * from goodstypeselect gt.*, gy.typename from goodstable gt,goodstype gy where gt.goodstype_id = gy.id and gy.id = #{tid} order by gt.id desc limit 6 select count(*) from goodstype select * from goodstable where goodstype_id = #{goodstype_id}insert into goodstype (id, typename) values(null, #{typename})

 
3.service层:业务层,存放业务逻辑处理,不直接对数据库进行操作,有接口和接口实现类,提供controller层调用方法。

AdminService.java

package com.ch.ebusiness.service.admin;import javax.servlet.http.HttpSession;import javax.transaction.Transactional;import org.springframework.stereotype.Service;import org.springframework.ui.Model;import com.ch.ebusiness.entity.AUser;@Service//@Transactionalpublic interface AdminService {public String login(AUser aUser, HttpSession session, Model model);}

GoodsService.java

package com.ch.ebusiness.service.admin;import java.io.IOException;import javax.servlet.http.HttpServletRequest;import javax.transaction.Transactional;import org.springframework.stereotype.Service;import org.springframework.ui.Model;import com.ch.ebusiness.entity.Goods;@Service//@Transactionalpublic interface GoodsService {public String selectAllGoodsByPage(Model model, int currentPage, String act);public String addGoods(Goods goods, HttpServletRequest request, String act) throws IllegalStateException, IOException ;public String toAddGoods(Goods goods, Model model);public String detail(Model model, Integer id, String act);public String delete(Integer id);}

TypeService.java

package com.ch.ebusiness.service.admin;import javax.transaction.Transactional;import org.springframework.stereotype.Service;import org.springframework.ui.Model;import com.ch.ebusiness.entity.GoodsType;@Service//@Transactionalpublic interface TypeService {public String selectAllTypeByPage(Model model, int currentPage);public String delete(int id);public String addType(GoodsType goodsType);}

 
4.controller层:控制层,导入service层,调用你service方法,controller通过 接收前端传来的参数进
行业务操作,在返回一个指定的路径或数据表。

AdminbaseController.java

package com.ch.ebusiness.controller.admin;import javax.servlet.http.HttpSession;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.ModelAttribute;import com.ch.ebusiness.NoLoginException;@Controllerpublic class AdminbaseController {@ModelAttribute public void isLogin(HttpSession session) throws NoLoginException { if(session.getAttribute("auser") == null){ throw new NoLoginException("没有登录"); } } }

AdminController.java

package com.ch.ebusiness.controller.admin;import javax.servlet.http.HttpSession;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.ModelAttribute;import org.springframework.web.bind.annotation.RequestMapping;import com.ch.ebusiness.entity.AUser;import com.ch.ebusiness.service.admin.AdminService;@Controller@RequestMapping("/admin")public class AdminController {@Autowiredprivate AdminService adminService;@RequestMapping("/toLogin")public String toLogin(@ModelAttribute("aUser") AUser aUser) {return "admin/login";}@RequestMapping("/login")public String login(@ModelAttribute("aUser") AUser aUser, HttpSession session, Model model) {return adminService.login(aUser, session, model);}}

GoodsController.java

package com.ch.ebusiness.controller.admin;import java.io.IOException;import javax.servlet.http.HttpServletRequest;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.ModelAttribute;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import com.ch.ebusiness.entity.Goods;import com.ch.ebusiness.service.admin.GoodsService;@Controller@RequestMapping("/goods")public class GoodsController extends AdminbaseController{@Autowiredprivate GoodsService goodsService;@RequestMapping("/selectAllGoodsByPage")public String selectAllGoodsByPage(Model model, int currentPage, String act) {return goodsService.selectAllGoodsByPage(model, currentPage, act);}@RequestMapping("/toAddGoods")public String toAddGoods(@ModelAttribute("goods") Goods goods, Model model) {goods.setIsAdvertisement(0);goods.setIsRecommend(1);return goodsService.toAddGoods(goods, model);}@RequestMapping("/addGoods")public String addGoods(@ModelAttribute("goods") Goods goods, HttpServletRequest request, String act) throws IllegalStateException, IOException {return goodsService.addGoods(goods, request, act);}@RequestMapping("/detail")public String detail(Model model, Integer id, String act) {return goodsService.detail(model, id, act);}@RequestMapping("/delete")@ResponseBodypublic String delete(Integer id) {return goodsService.delete(id);}}

 
五、SpringBoot各 层详解
constant:常量包,存放一些常量数据,如定义服务器响应状态码。
controller:控制器,存放各种控制器,来提供数据或
者返回界面
entity:实体类包,存放各种与数据库对应的实体类
mapper:存放返回数据json的格式样式
service:返回数据给控制调用  六、运行截图

 

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。