Jsp+SSH+mysql實現的web網上商場系統(前臺/後臺 功能:商品管理、購物車管理、訂單管理、使用者管理)

anmu4200發表於2020-10-25

SSH-WEB商場系統

本系統是一套電商管理的系統,方便人們線上購買商品,方便商家後臺管理自己的商品。

實現功能截圖

功能點:
(1) 登入功能:使用者輸入使用者名稱和密碼,進行登入驗證。
(2) 註冊功能:可以查詢使用者名稱是否已經存在,對註冊資訊進行驗證。
(3) 商品瀏覽:可以查詢商品,顯示商品詳情,提供購買連結。
(4) 購物車管理:欲購買商品可以增添到購物車,也可以從購物車刪除商品。
(5) 訂單查詢:使用者登入後可以下訂單,使用者登入後也可以檢視自己的訂單。
(6) 商品種類管理:管理員可以對商品種類進行新增、刪除操作。
(7) 商品管理:管理員可以新增商品、刪除商品,檢視所有商品。
(8) 訂單管理:管理員登入後可以對訂單進行管理。
(9) 使用者管理:管理員可以管理註冊使用者資訊。

前臺:

使用者註冊登入:
在這裡插入圖片描述
在這裡插入圖片描述

首頁:
在這裡插入圖片描述
商品分類瀏覽:
在這裡插入圖片描述
商品詳情:
在這裡插入圖片描述
購物車:
在這裡插入圖片描述
我的訂單:
在這裡插入圖片描述
個人資訊更新:
在這裡插入圖片描述
後臺:

管理員登入:
在這裡插入圖片描述
商品種類列表:
在這裡插入圖片描述
新增種類:
在這裡插入圖片描述
商品列表:
在這裡插入圖片描述
新增商品:
在這裡插入圖片描述
訂單管理:
在這裡插入圖片描述
使用者管理:
在這裡插入圖片描述
管理員密碼修改:
在這裡插入圖片描述

主要流程圖

1、系統功能流程圖:
在這裡插入圖片描述
在這裡插入圖片描述

2、資料流圖:
頂層資料流圖:
在這裡插入圖片描述
使用者管理資料流圖:
在這裡插入圖片描述
商品管理資料流圖:
在這裡插入圖片描述
3、資料字典
關鍵資料項:
在這裡插入圖片描述

技術點總結

java框架:SSH
jdk版本:1.7
tomcat: 7.0
資料庫:mysql
開發工具:myeclipse

程式碼

dao層:
訂單Dao:OrderDaoImpl.java

package cn.itbaizhan.dao.impl;

import java.util.List;

import javax.annotation.Resource;




import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component;

import cn.itbaizhan.dao.OrderFormDao;
import cn.itbaizhan.po.Commodity;
import cn.itbaizhan.po.OrderForm;
import cn.itbaizhan.po.User;
@Component("orderFormDaoImpl")
public class OrderFormDaoImpl implements OrderFormDao {
//使用spring對Hibernate的模版 HibernateTemplate
	@Resource(name="hibernateTemplate")
	private HibernateTemplate hibernateTemplate;
	
	public void delete(OrderForm orderForm) {
	hibernateTemplate.delete(orderForm);
	}
	@SuppressWarnings("unchecked")
	public List<OrderForm> findAllOrderForms() {
		String hql = "from OrderForm";
		return (List<OrderForm>)hibernateTemplate.find(hql);
	}

	public OrderForm findOrderFormById(int id) {
		OrderForm orderForm = (OrderForm)hibernateTemplate.get(OrderForm.class, id);
		return orderForm;
	}

	public void save(OrderForm orderForm) {
		System.out.println(orderForm);
	 hibernateTemplate.save(orderForm);
	}

	public void update(OrderForm orderForm) {
	hibernateTemplate.update(orderForm);

	}
	public HibernateTemplate getHibernateTmeplate() {
		return hibernateTemplate;
	}
	public void setHibernateTmeplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}
	public OrderForm queryOrderForm(int orderFormId) {
		
		return (OrderForm)hibernateTemplate.getSessionFactory().openSession().createQuery(
		"from OrderForm o where o.orderFormId=? ").setParameter(
				0, orderFormId).uniqueResult();

	}
	@SuppressWarnings("unchecked")
	public List<OrderForm> findOrderFormByUserName(String username) {
		System.out.println("使用者名稱:"+username);
		String hql = "from OrderForm o where o.username=?";
		
		return (List<OrderForm>)hibernateTemplate.find(hql,username);
	}
	
}

AdminDaoImpl.java:

package cn.itbaizhan.dao.impl;

import java.util.List;

import javax.annotation.Resource;




import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component;

import cn.itbaizhan.dao.AdminDao;
import cn.itbaizhan.po.Admin;
@Component("adminDaoImpl")
public class AdminDaoImpl implements AdminDao {
//使用spring對Hibernate的模版 HibernateTemplate
	@Resource(name="hibernateTemplate")
	private HibernateTemplate hibernateTemplate;
	
	public void delete(Admin admin) {
	hibernateTemplate.delete(admin);
	}
	@SuppressWarnings("unchecked")
	public List<Admin> findAllAdmins() {
		String hql = "from Admin";
		return (List<Admin>)hibernateTemplate.find(hql);
	}

	public Admin findAdminById(int id) {
		Admin admin = (Admin)hibernateTemplate.get(Admin.class, id);
		return admin;
	}

	public void save(Admin admin) {
		System.out.println(admin);
	 hibernateTemplate.save(admin);
	}

	public void update(Admin admin) {
	hibernateTemplate.update(admin);

	}
	public HibernateTemplate getHibernateTmeplate() {
		return hibernateTemplate;
	}
	public void setHibernateTmeplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}
	
	//獲取使用者
	public Admin getUserByLoginNameAndPassword(String username, String password) {
		//String[] values={username, password}; 
		//String hql = "from User u where u.username=? and u.password=?";
		//(List<User>)hibernateTemplate.find(hql, values);
		System.out.println("使用者名稱:"+username);
		
		return  (Admin) hibernateTemplate.getSessionFactory().openSession().createQuery(
		"from Admin a where a.username=? and a.password=?").setParameter(0, username).setParameter(1, password).uniqueResult();
	}

	

}

業務邏輯層(service)
訂單業務邏輯層:OrderServiceImpl.java

package cn.itbaizhan.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import cn.itbaizhan.dao.OrderFormDao;
import cn.itbaizhan.po.OrderForm;
import cn.itbaizhan.po.User;
import cn.itbaizhan.service.OrderFormService;
//把類交給spring管理(如同配置bean節點)此類受spring事務管理
@Component("orderFormServiceImpl")
public class OrderFormServiceImpl implements OrderFormService {
	//要呼叫dao層程式碼,所以要有OrderFormDao的物件,把其實現類靠spring注入
	@Resource(name="orderFormDaoImpl")
	private OrderFormDao orderFormDao;
	public OrderFormDao getOrderFormDao() {
		return orderFormDao;
	}

	public void setOrderFormDao(OrderFormDao orderFormDao) {
		this.orderFormDao = orderFormDao;
	}

	public void delete(OrderForm orderForm) {
		this.orderFormDao.delete(orderForm);

	}
	public List<OrderForm> findAllOrderForms() {
		return this.orderFormDao.findAllOrderForms();
	}
	public OrderForm findOrderFormById(int id) {
		
		return this.orderFormDao.findOrderFormById(id);
	}

	public void save(OrderForm orderForm) {
		this.orderFormDao.save(orderForm);

	}

	public void update(OrderForm orderForm) {
		this.orderFormDao.update(orderForm);
	}

	public OrderForm queryOrderForm(int orderFormId) {
		
		return this.orderFormDao.queryOrderForm(orderFormId);
	}

	public List<OrderForm> findOrderFormByUserName(String username) {
		
		return this.orderFormDao.findOrderFormByUserName(username);
	}

	


}

管理員業務邏輯層:AdminServiceImpl.java

package cn.itbaizhan.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import cn.itbaizhan.dao.AdminDao;
import cn.itbaizhan.po.Admin;
import cn.itbaizhan.service.AdminService;
//把類交給spring管理(如同配置bean節點)此類受spring事務管理
@Component("adminServiceImpl")
public class AdminServiceImpl implements AdminService {
	//要呼叫dao層程式碼,所以要有UserDao的物件,把其實現類靠spring注入
	@Resource(name="adminDaoImpl")
	private AdminDao adminDao;
	public AdminDao getUserDao() {
		return adminDao;
	}

	public void setUserDao(AdminDao adminDao) {
		this.adminDao = adminDao;
	}

	public void delete(Admin user) {
		this.adminDao.delete(user);

	}
	public List<Admin> findAllAdmins() {
		return this.adminDao.findAllAdmins();
	}
	public Admin findAdminById(int id) {
		// TODO Auto-generated method stub
		return this.adminDao.findAdminById(id);
	}

	public void save(Admin admin) {
		this.adminDao.save(admin);

	}

	public void update(Admin admin) {
		this.adminDao.update(admin);
	}


	public Admin getUserByLoginNameAndPassword(String name, String password) {
		
		return this.adminDao.getUserByLoginNameAndPassword(name,password);
	}


}

action層:
訂單action:OrderAction.java

package cn.itbaizhan.action;

import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import cn.itbaizhan.action.*;
import cn.itbaizhan.po.Commodity;
import cn.itbaizhan.po.OrderForm;
import cn.itbaizhan.po.User;
import cn.itbaizhan.service.OrderFormService;
import cn.itbaizhan.service.UserService;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
@Component("orderFormAction")
@Scope("prototype")//多例,每個請求生成一個新的action
public class OrderFormAction extends ActionSupport {
	private OrderForm orderForm;
	
//要呼叫OrderFormService的方法,所以要宣告,讓spring把其實現類注入
	@Resource(name="orderFormServiceImpl")
	OrderFormService service;
	
	private UserService userservice;
	
	public OrderFormService getService() {
		return service;
	}

	public void setService(OrderFormService service) {
		this.service = service;
	}

	public OrderForm getOrderForm() {
		return orderForm;
	}

	public void setOrderForm(OrderForm orderForm) {
		this.orderForm = orderForm;
	}
	

	@Resource
	public void setUserservice(UserService userservice) {
		this.userservice = userservice;
	}


	public UserService getUserservice() {
		return userservice;
	}

	@Override
	public String execute() throws Exception {
		// 不要直接訪問資料庫,應該訪問業務邏輯層service,而service去訪問DAO,DAO去訪問資料庫
			this.service.save(this.orderForm);
			System.out.println("orderForm:"+orderForm.getUsername());
			ActionContext.getContext().put("ordermessage", "您的訂單已經提交!");
			return "success";
	}
	//列表
	@SuppressWarnings("unchecked")
	public String listOrderForm(){
		Map request = (Map) ActionContext.getContext().get("request");
		
		request.put("listOrderForms", service.findAllOrderForms());
		System.out.println("listOrderForms");
		return "listOrderForm";
	}
	//查詢
	@SuppressWarnings("unchecked")
	public String queryOrderForm(){
		int orderFormId= orderForm.getOrderFormId();
		System.out.println("orderFormId:"+orderFormId);
		String username= orderForm.getUsername();
		System.out.println("username:"+username);
		String submitTime= orderForm.getSubmitTime();
		System.out.println("submitTime:"+submitTime);
		String consignmentTime= orderForm.getConsignmentTime();
		System.out.println("consignmentTime:"+consignmentTime);
	
		OrderForm orderForm=this.service.queryOrderForm(orderFormId);
		Map request=(Map) ActionContext.getContext().get("request");
		
		request.put("queryOrder", orderForm);
		return "queryOrder";
	}
	

	public String deleteOrderForm(){
		this.service.delete(orderForm);
		return "deleteOrderForm";
	}
	public String findOrderFormById(){
		orderForm = this.service.findOrderFormById(orderForm.getOrderFormId());
		return "findOrderForm";
	}
	//付款
	@SuppressWarnings("unchecked")
	public String payOrderForm(){
		double totalprice=orderForm.getTotalPrice();//訂單總金額
		User user=userservice.findUserByName(orderForm.getUsername());
		double money=user.getMoney();//使用者所有金錢
		Map request = (Map) ActionContext.getContext().get("request");
		if(totalprice>money){		
			request.put("nowOrder", orderForm);
			ActionContext.getContext().put("payemessage", "您的金額不足!");
		}
		else
		{
		user.setMoney(money-totalprice);
		userservice.update(user);
		orderForm.setIsPayoff("是");
		this.service.update(orderForm);
		
		System.out.println("orderForm:"+orderForm.getOrderFormId());
		
		request.put("nowOrder", orderForm);
		ActionContext.getContext().put("payemessage", "付款成功!");
		}
		return "payOrderForm";
	}
	
	//訂單列表付款
	@SuppressWarnings("unchecked")
	public String payOrder(){
		double totalprice=orderForm.getTotalPrice();//訂單總金額
		String username=orderForm.getUsername();
		User user=userservice.findUserByName(orderForm.getUsername());
		double money=user.getMoney();//使用者所有金錢
		
		Map request = (Map) ActionContext.getContext().get("request");
		
		if(totalprice>money){
	
			request.put("orderFormByUserName", service.findOrderFormByUserName(username));
			ActionContext.getContext().put("payemessage", "您的金額不足!");
		}
		else
		{
		user.setMoney(money-totalprice);
		userservice.update(user);
		orderForm.setIsPayoff("是");
		this.service.update(orderForm);
		System.out.println("orderForm:"+orderForm.getOrderFormId());
		
		request.put("orderFormByUserName", service.findOrderFormByUserName(username));
		ActionContext.getContext().put("payemessage", "付款成功!");
		}
		return "payOrder";
	}
	
	public String inputOrderForm(){
		return "input";
	}
	//發貨
	@SuppressWarnings("unchecked")
	public String conOrder(){
		orderForm.setIsConsignment("是");
		this.service.update(orderForm);
		Map request = (Map) ActionContext.getContext().get("request");
		request.put("listOrderForms", service.findAllOrderForms());
		
		return "conOrder";
	}
	//提交訂單
	@SuppressWarnings("unchecked")
	public String addOrderForm(){
		this.service.save(this.orderForm);
		
		Map session = ActionContext.getContext().getSession();//獲得session物件
		List<Commodity> car = (List<Commodity>)session.get("car");//取得購物車
		car.clear();
		
		Map request = (Map) ActionContext.getContext().get("request");
		request.put("nowOrder",orderForm);
		return "addOrderForm";
	}
	
	@SuppressWarnings("unchecked")
	public String findOrderFormByUserName(){
		Map request = (Map) ActionContext.getContext().get("request");
		
		request.put("orderFormByUserName",this.service.findOrderFormByUserName(orderForm.getUsername()));
		return "findOrderFormByUserName";
	}
	
}

管理員action:AdminAction.java

package cn.itbaizhan.action;

import java.util.Collection;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import cn.itbaizhan.po.Admin;
import cn.itbaizhan.service.AdminService;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;



@SuppressWarnings("serial")
@Component("adminAction")
@Scope("prototype")//多例,每個請求生成一個新的action
public class AdminAction extends ActionSupport {
	private Admin admin;
	
	//private Role role;
	//private Department department;
//要呼叫UserService的方法,所以要宣告,讓spring把其實現類注入
	@Resource(name="adminServiceImpl")
	AdminService service;
	public AdminService getService() {
		return service;
	}

	public void setService(AdminService service) {
		this.service = service;
	}

	public Admin getAdmin() {
		return admin;
	}

	public void setAdmin(Admin admin) {
		this.admin = admin;
	}
	
	@SuppressWarnings("unchecked")
	public String listAdmin(){
		Map request = (Map) ActionContext.getContext().get("request");
		
		request.put("list", service.findAllAdmins());
		return "listAdmin";
	}
	public String deleteAdmin(){
		this.service.delete(admin);
		return "deleteAdmin";
	}
	public String findAdminById(){
		admin = this.service.findAdminById(admin.getAdminId());
		return "findAdmin";
	}
	public String updateAdmin(){
		//adminpassmessage
		
		this.service.update(admin);
		System.out.println("admin:"+admin.getUsername());
		ActionContext.getContext().put("upAdminMessage", "密碼修改成功!");	
		return "updateAdmin";
		
	}
	public String inputAdmin(){
		return "input";
	}
	
	@SuppressWarnings("unchecked")
	public String addAdmin(){
		Map request = (Map) ActionContext.getContext().get("request");
		
		return "addAdmin";
	}

	//管理員登入
	public String login(){
		String username= admin.getUsername();
		System.out.println(username);
		String password=admin.getPassword();

		Admin admin = service.getUserByLoginNameAndPassword(username,password);
		System.out.print("name:"+username+",pass:"+password+",user:"+admin);
		if(admin == null){
			ActionContext.getContext().put("message", "使用者名稱或密碼錯誤");		
			return "login";
		}else{
			ActionContext.getContext().getSession().put("admin", admin);
			return "index";
		}
	}
	//退出
	public String logout(){
		ActionContext.getContext().getSession().clear();
		return "logout";
	}
}

寫在最後

碼程式碼不容易,需要的同學可以參考學習,全部程式碼不能都貼出,如果需要可以+博主V交流獲取(Code2Life2)
最後,別忘了一鍵三連哦

相關文章