基于javaweb+mysql的ssm电商平台网上超市在线购物商城系统(java+ssm+javascript+jsp+mysql+tomcat)
私信源码获取及调试交流
运行环境
Java≥8、MySQL≥5.7、Tomcat≥8
开发工具
eclipse/idea/myeclipse/sts等均可配置运行
适用
课程设计,大作业,毕业设计,项目练习,学习演示等
功能说明
基于javaweb的SSM电商平台网上超市在线购物商城系统(java+ssm+javascript+jsp+mysql+tomcat)
前台用户:
user1@qq.com 123456
user2@qq.com 123456
后台管理员:
admin 123456
@Service("indexService")
@Transactional
public class IndexServiceImpl implements IndexService{
@Autowired
private IndexDao indexDao;
@Autowired
private AdminTypeDao adminTypeDao;
@Autowired
private AdminNoticeDao adminNoticeDao;
@Override
public String before(Model model, HttpSession session, Goods goods) {
session.setAttribute("goodsType", adminTypeDao.selectGoodsType());
model.addAttribute("salelist", indexDao.getSaleOrder());
model.addAttribute("focuslist", indexDao.getFocusOrder());
model.addAttribute("noticelist", indexDao.selectNotice());
if(goods.getId() == null)
goods.setId(0);
model.addAttribute("lastedlist", indexDao.getLastedGoods(goods));
return "before/index";
}
@Override
public String toRegister(Model model) {
model.addAttribute("rbuser", new Buser());
return "before/register";
}
@Override
public String toLogin(Model model) {
model.addAttribute("lbuser", new Buser());
return "before/login";
}
@Override
public String goodsDetail(Model model, Integer id) {
Goods goods = indexDao.selectGoodsById(id);
model.addAttribute("goods", goods);
Map<String, Object> model = new HashMap<String, Object>();
model.put("ex", arg3);
// 根据不同错误转向不同页面
if(arg3 instanceof AdminLoginNoException){
//登录页面需要auser对象
arg0.setAttribute("auser", new Auser());
arg0.setAttribute("msg", "没有登录,请登录!");
return new ModelAndView("/admin/login", model);
} else if(arg3 instanceof UserLoginNoException){
arg0.setAttribute("buser", new Buser());
arg0.setAttribute("msg", "没有登录,请登录!");
return new ModelAndView("/before/login", model);
}else{
return new ModelAndView("/error/error", model);
}
}
}
@Controller
@RequestMapping("/adminNotice")
public class AdminNoticeController extends BaseController{
@Autowired
private AdminNoticeService adminNoticeService;
@RequestMapping("/toAddNotice")
public String toAddNotice(Model model) {
model.addAttribute("notice", new Notice());
return "admin/addNotice";
}
@RequestMapping("/addNotice")
public String addNotice(@ModelAttribute Notice notice) {
return adminNoticeService.addNotice(notice);
}
@RequestMapping("/deleteNoticeSelect")
public String deleteNoticeSelect(Model model) {
return adminNoticeService.deleteNoticeSelect(model);
}
@RequestMapping("/selectANotice")
public String selectANotice(Model model, Integer id) {
return adminNoticeService.selectANotice(model, id);
}
}
/**
* 查询一个商品
*/
@Override
public String selectAGoods(Model model, Integer id, String act) {
Goods agoods = adminGoodsDao.selectGoodsById(id);
model.addAttribute("goods", agoods);
//修改页面
if("updateAgoods".equals(act)){
return "admin/updateAgoods";
}
//详情页面
return "admin/goodsDetail";
}
/**
* 删除多个商品
*/
@Override
public String deleteGoods(Integer[] ids, Model model) {
List<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < ids.length; i++) {
//商品有关联
if(adminGoodsDao.selectCartGoods(ids[i]).size() > 0 ||
adminGoodsDao.selectFocusGoods(ids[i]).size() > 0 ||
adminGoodsDao.selectOrderdetailGoods(ids[i]).size() > 0) {
model.addAttribute("msg", "商品有关联,不允许删除!");
return "forward:/adminGoods/selectGoods?act=deleteSelect";
}
list.add(ids[i]);
}
adminGoodsDao.deleteGoods(list);
model.addAttribute("msg", "成功删除商品!");
return "forward:/adminGoods/selectGoods?act=deleteSelect";
}
/**
* 删除一个商品
*/
@Override
public String deleteAGoods(Integer id, Model model) {
@RequestMapping("/toDeleteType")
public String toDeleteType(Model model) {
return adminTypeService.toDeleteType(model);
}
/**
* 删除类型
*/
@RequestMapping("/deleteType")
public String deleteType(Integer id,Model model) {
return adminTypeService.deleteType(id, model);
}
}
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/register")
public String register(@ModelAttribute Buser buser,Model model, HttpSession session, String code) {
return userService.register(buser, model, session, code);
}
@RequestMapping("/login")
public String login(@ModelAttribute Buser buser,Model model, HttpSession session, String code) {
return userService.login(buser, model, session, code);
}
@RequestMapping("/exit")
public String exit(HttpSession session) {
session.invalidate();
return "forward:/before";
}
}
for (Map<String, Object> map2 : list) {
orderDao.updateStore(map2);
}
//清空购物车
orderDao.clear(MyUtil.getUserId(session));
model.addAttribute("ordersn", order.getId());
return "before/orderdone";
}
@Override
public String pay(Integer ordersn) {
orderDao.pay(ordersn);
return "before/paydone";
}
}
@Controller
@RequestMapping("/adminGoods")
public class AdminGoodsController extends BaseController{
@Autowired
private AdminGoodsService adminGoodsService;
@RequestMapping("/selectGoods")
public String selectGoods(Model model, Integer pageCur, String act) {
return adminGoodsService.selectGoods(model, pageCur, act);
}
/**
* add页面初始化
*/
@RequestMapping("/toAddGoods")
public String toAddGoods(Model model){
model.addAttribute("goods", new Goods());
return "admin/addGoods";
}
/**
* 添加与修改
*/
@RequestMapping("/addGoods")
public String addGoods(@ModelAttribute Goods goods, HttpServletRequest request, String updateAct){
return adminGoodsService.addOrUpdateGoods(goods, request, updateAct);
}
/**
* 查询一个名片
@Service("indexService")
@Transactional
public class IndexServiceImpl implements IndexService{
@Autowired
private IndexDao indexDao;
@Autowired
private AdminTypeDao adminTypeDao;
@Autowired
private AdminNoticeDao adminNoticeDao;
@Override
public String before(Model model, HttpSession session, Goods goods) {
session.setAttribute("goodsType", adminTypeDao.selectGoodsType());
model.addAttribute("salelist", indexDao.getSaleOrder());
model.addAttribute("focuslist", indexDao.getFocusOrder());
model.addAttribute("noticelist", indexDao.selectNotice());
if(goods.getId() == null)
goods.setId(0);
model.addAttribute("lastedlist", indexDao.getLastedGoods(goods));
return "before/index";
}
@Override
public String toRegister(Model model) {
model.addAttribute("rbuser", new Buser());
return "before/register";
}
@Override
public String toLogin(Model model) {
model.addAttribute("lbuser", new Buser());
return "before/login";
}
@Override
public String goodsDetail(Model model, Integer id) {
Goods goods = indexDao.selectGoodsById(id);
model.addAttribute("goods", goods);
return "before/goodsdetail";
}
@Override
public String selectANotice(Model model, Integer id) {
Notice notice = adminNoticeDao.selectANotice(id);
model.addAttribute("notice", notice);
@Override
public String pay(Integer ordersn) {
orderDao.pay(ordersn);
return "before/paydone";
}
}
@Controller
@RequestMapping("/adminGoods")
public class AdminGoodsController extends BaseController{
@Autowired
private AdminGoodsService adminGoodsService;
@RequestMapping("/selectGoods")
public String selectGoods(Model model, Integer pageCur, String act) {
return adminGoodsService.selectGoods(model, pageCur, act);
}
/**
* add页面初始化
*/
@RequestMapping("/toAddGoods")
public String toAddGoods(Model model){
model.addAttribute("goods", new Goods());
return "admin/addGoods";
}
/**
* 添加与修改
*/
@RequestMapping("/addGoods")
public String addGoods(@ModelAttribute Goods goods, HttpServletRequest request, String updateAct){
return adminGoodsService.addOrUpdateGoods(goods, request, updateAct);
}
/**
* 查询一个名片
*/
@RequestMapping("/selectAGoods")
public String selectAGoods(Model model, Integer id, String act){
}
}
//修改
if("update".equals(updateAct)){//updateAct不能与act重名,因为使用了转发
//修改到数据库
if(adminGoodsDao.updateGoodsById(goods) > 0){
return "forward:/adminGoods/selectGoods?act=updateSelect";
}else{
return "/adminGoods/updateAgoods";
}
}else{//添
//保存到数据库
if(adminGoodsDao.addGoods(goods) > 0){
//转发到查询的controller
return "forward:/adminGoods/selectGoods";
}else{
return "card/addCard";
}
}
}
/**
* 查询商品
*/
@Override
public String selectGoods(Model model, Integer pageCur, String act) {
List<Goods> allGoods = adminGoodsDao.selectGoods();
int temp = allGoods.size();
model.addAttribute("totalCount", temp);
int totalPage = 0;
if (temp == 0) {
totalPage = 0;//总页数
} else {
//返回大于或者等于指定表达式的最小整数
totalPage = (int) Math.ceil((double) temp / 10);
}
if (pageCur == null) {
pageCur = 1;
}
if ((pageCur - 1) * 10 > temp) {
pageCur = pageCur - 1;
}
//分页查询
Map<String, Object> map = new HashMap<String, Object>();
map.put("startIndex", (pageCur - 1) * 10);//起始位置
map.put("perPageSize", 10);//每页10个
allGoods = adminGoodsDao.selectAllGoodsByPage(map);
model.addAttribute("allGoods", allGoods);
model.addAttribute("totalPage", totalPage);
model.addAttribute("pageCur", pageCur);
//删除查询
if("deleteSelect".equals(act)){
@Service("adminService")
@Transactional
public class AdminServiceImpl implements AdminService{
@Autowired
private AdminDao adminDao;
@Autowired
private AdminTypeDao adminTypeDao;
@Override
public String login(Auser auser, Model model, HttpSession session) {
if(adminDao.login(auser) != null && adminDao.login(auser).size() > 0) {
session.setAttribute("auser", auser);
//添加商品与修改商品页面使用
session.setAttribute("goodsType", adminTypeDao.selectGoodsType());
return "admin/main";
}
model.addAttribute("msg", "用户名或密码错误!");
return "admin/login";
}
}
@Service("userCenterService")
@Transactional
public class UserCenterServiceImpl implements UserCenterService{
@Autowired
private UserCenterDao userCenterDao;
@Override
public String userCenter(HttpSession session, Model model) {
model.addAttribute("myOrder", userCenterDao.myOrder(MyUtil.getUserId(session)));
public String selectGoods(Model model, Integer pageCur, String act) {
List<Goods> allGoods = adminGoodsDao.selectGoods();
int temp = allGoods.size();
model.addAttribute("totalCount", temp);
int totalPage = 0;
if (temp == 0) {
totalPage = 0;//总页数
} else {
//返回大于或者等于指定表达式的最小整数
totalPage = (int) Math.ceil((double) temp / 10);
}
if (pageCur == null) {
pageCur = 1;
}
if ((pageCur - 1) * 10 > temp) {
pageCur = pageCur - 1;
}
//分页查询
Map<String, Object> map = new HashMap<String, Object>();
map.put("startIndex", (pageCur - 1) * 10);//起始位置
map.put("perPageSize", 10);//每页10个
allGoods = adminGoodsDao.selectAllGoodsByPage(map);
model.addAttribute("allGoods", allGoods);
model.addAttribute("totalPage", totalPage);
model.addAttribute("pageCur", pageCur);
//删除查询
if("deleteSelect".equals(act)){
return "admin/deleteSelectGoods";
}
//修改查询
else if("updateSelect".equals(act)){
return "admin/updateSelectGoods";
}else{
return "admin/selectGoods";
}
}
/**
* 查询一个商品
*/
@Override
public String selectAGoods(Model model, Integer id, String act) {
String realpath = request.getServletContext().getRealPath("logos");
//实现文件上传
String fileType = fileName.substring(fileName.lastIndexOf('.'));
//防止文件名重名
newFileName = MyUtil.getStringID() + fileType;
goods.setGpicture(newFileName);
File targetFile = new File(realpath, newFileName);
if(!targetFile.exists()){
targetFile.mkdirs();
}
//上传
try {
goods.getLogoImage().transferTo(targetFile);
} catch (Exception e) {
e.printStackTrace();
}
}
//修改
if("update".equals(updateAct)){//updateAct不能与act重名,因为使用了转发
//修改到数据库
if(adminGoodsDao.updateGoodsById(goods) > 0){
return "forward:/adminGoods/selectGoods?act=updateSelect";
}else{
return "/adminGoods/updateAgoods";
}
}else{//添
//保存到数据库
if(adminGoodsDao.addGoods(goods) > 0){
//转发到查询的controller
return "forward:/adminGoods/selectGoods";
}else{
return "card/addCard";
}
}
}
/**
* 查询商品
*/
@Override
public String selectGoods(Model model, Integer pageCur, String act) {
List<Goods> allGoods = adminGoodsDao.selectGoods();
@RequestMapping("/toDeleteType")
public String toDeleteType(Model model) {
return adminTypeService.toDeleteType(model);
}
/**
* 删除类型
*/
@RequestMapping("/deleteType")
public String deleteType(Integer id,Model model) {
return adminTypeService.deleteType(id, model);
}
}
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/register")
public String register(@ModelAttribute Buser buser,Model model, HttpSession session, String code) {
return userService.register(buser, model, session, code);
}
@RequestMapping("/login")
public String login(@ModelAttribute Buser buser,Model model, HttpSession session, String code) {
return userService.login(buser, model, session, code);
}
@RequestMapping("/exit")
public String exit(HttpSession session) {
session.invalidate();
return "forward:/before";
}
}
/**
* 验证码
*/
@Controller
public class ValidateCodeController {
private char code[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
'k', 'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y',
'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M',
'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '2',
'3', '4', '5', '6', '7', '8', '9' };
private static final int WIDTH = 50;
private static final int HEIGHT = 20;
private static final int LENGTH = 4;
@RequestMapping("/validateCode")
public void validateCode(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
// 设置响应报头信息
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
// 设置响应的MIME类型
response.setContentType("image/jpeg");
BufferedImage image = new BufferedImage(WIDTH, HEIGHT,
BufferedImage.TYPE_INT_RGB);
Font mFont = new Font("Arial", Font.TRUETYPE_FONT, 18);
Graphics g = image.getGraphics();
Random rd = new Random();
// 设置背景颜色
g.setColor(new Color(rd.nextInt(55) + 200, rd.nextInt(55) + 200, rd
.nextInt(55) + 200));
g.fillRect(0, 0, WIDTH, HEIGHT);
// 设置字体
g.setFont(mFont);
// 画边框
// 随机产生2个干扰线
for (int i = 0; i < 2; i++) {
g.setColor(new Color(rd.nextInt(200), rd.nextInt(200), rd
.nextInt(200)));
int x1 = rd.nextInt(WIDTH);
int x2 = rd.nextInt(WIDTH);
int y1 = rd.nextInt(HEIGHT);
int y2 = rd.nextInt(HEIGHT);
g.drawLine(x1, y1, x2, y2);
}
// 释放图形资源
g.dispose();
try {
OutputStream os = response.getOutputStream();
// 输出图像到页面
ImageIO.write(image, "JPEG", os);
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Service("cartService")
@Transactional
public class CartServiceImpl implements CartService{
@Autowired
private CartDao cartDao;
@Override
public String focus(Model model, Integer id, HttpSession session) {
allGoods = adminGoodsDao.selectAllGoodsByPage(map);
model.addAttribute("allGoods", allGoods);
model.addAttribute("totalPage", totalPage);
model.addAttribute("pageCur", pageCur);
//删除查询
if("deleteSelect".equals(act)){
return "admin/deleteSelectGoods";
}
//修改查询
else if("updateSelect".equals(act)){
return "admin/updateSelectGoods";
}else{
return "admin/selectGoods";
}
}
/**
* 查询一个商品
*/
@Override
public String selectAGoods(Model model, Integer id, String act) {
Goods agoods = adminGoodsDao.selectGoodsById(id);
model.addAttribute("goods", agoods);
//修改页面
if("updateAgoods".equals(act)){
return "admin/updateAgoods";
}
//详情页面
return "admin/goodsDetail";
}
/**
* 删除多个商品
*/
@Override
public String deleteGoods(Integer[] ids, Model model) {
List<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < ids.length; i++) {
//商品有关联
if(adminGoodsDao.selectCartGoods(ids[i]).size() > 0 ||
adminGoodsDao.selectFocusGoods(ids[i]).size() > 0 ||
adminGoodsDao.selectOrderdetailGoods(ids[i]).size() > 0) {
model.addAttribute("msg", "商品有关联,不允许删除!");
return "forward:/adminGoods/selectGoods?act=deleteSelect";
order.setBusertable_id(MyUtil.getUserId(session));
//生成订单,并将主键返回order
orderDao.addOrder(order);
//生成订单详情
Map<String, Object> map = new HashMap<String, Object>();
map.put("ordersn", order.getId());
map.put("uid", MyUtil.getUserId(session));
orderDao.addOrderDetail(map);
//更新商品库存
//更新商品库存1.查询商品购买量,以便更新库存使用
List<Map<String, Object>> list = orderDao.selectGoodsShop(MyUtil.getUserId(session));
//更新商品库存2.根据商品购买量更新库存
for (Map<String, Object> map2 : list) {
orderDao.updateStore(map2);
}
//清空购物车
orderDao.clear(MyUtil.getUserId(session));
model.addAttribute("ordersn", order.getId());
return "before/orderdone";
}
@Override
public String pay(Integer ordersn) {
orderDao.pay(ordersn);
return "before/paydone";
}
}
@Controller
@RequestMapping("/adminGoods")
public class AdminGoodsController extends BaseController{
@Autowired
private AdminGoodsService adminGoodsService;
@RequestMapping("/selectGoods")
*/
@Override
public String addOrUpdateGoods(Goods goods, HttpServletRequest request, String updateAct) {
/*上传文件的保存位置"/logos",该位置是指
workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps,
发布后使用*/
//防止文件名重名
String newFileName = "";
String fileName = goods.getLogoImage().getOriginalFilename();
//选择了文件
if(fileName.length() > 0){
String realpath = request.getServletContext().getRealPath("logos");
//实现文件上传
String fileType = fileName.substring(fileName.lastIndexOf('.'));
//防止文件名重名
newFileName = MyUtil.getStringID() + fileType;
goods.setGpicture(newFileName);
File targetFile = new File(realpath, newFileName);
if(!targetFile.exists()){
targetFile.mkdirs();
}
//上传
try {
goods.getLogoImage().transferTo(targetFile);
} catch (Exception e) {
e.printStackTrace();
}
}
//修改
if("update".equals(updateAct)){//updateAct不能与act重名,因为使用了转发
//修改到数据库
if(adminGoodsDao.updateGoodsById(goods) > 0){
return "forward:/adminGoods/selectGoods?act=updateSelect";
}else{
return "/adminGoods/updateAgoods";
}
}else{//添
//保存到数据库
if(adminGoodsDao.addGoods(goods) > 0){
//转发到查询的controller
return "forward:/adminGoods/selectGoods";
public String selectANotice(Model model, Integer id) {
Notice notice = adminNoticeDao.selectANotice(id);
model.addAttribute("notice", notice);
return "admin/noticeDetail";
}
@Override
public String search(Model model, String mykey) {
List<Goods> list = indexDao.search(mykey);
model.addAttribute("searchlist", list);
return "before/searchResult";
}
}
@Service("orderService")
@Transactional
/**
* 订单生成有连串的更新操作,此处必须使用事务管理
*/
public class OrderServiceImpl implements OrderService{
@Autowired
private OrderDao orderDao;
/**
* 订单提交,连续的事务处理
*/
@Override
public String orderSubmit(Model model, HttpSession session, Double amount) {
Order order = new Order();
order.setAmount(amount);
order.setBusertable_id(MyUtil.getUserId(session));
//生成订单,并将主键返回order
orderDao.addOrder(order);
//生成订单详情
Map<String, Object> map = new HashMap<String, Object>();