ssm實戰購物商城系統

項目介紹

本系統使用Spring+SpringMVC+MyBatis架構,數據庫使用MySQL,開發完成了從商家發佈商品,到用戶查看商品並下單購買這樣的一個閉合的流程。javascript

項目適用人羣

正在作畢設的學生,或者須要項目實戰練習的Java學習者css

開發環境

  1. jdk 8
  2. intellij idea
  3. tomcat 8.5.40
  4. mysql 5.7

所用技術

  1. Spring+SpringMVC+MyBatis
  2. layui
  3. jsphtml

    項目架構

    ssm實戰購物商城系統

項目截圖

  • 登陸
    ssm實戰購物商城系統
  • 首頁
    ssm實戰購物商城系統
  • 商品詳情
    ssm實戰購物商城系統
  • 購物車
    ssm實戰購物商城系統
  • 訂單詳情
    ssm實戰購物商城系統

框架配置

  1. applicationContext.xmljava

    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd" >
    <context:annotation-config />
    <context:component-scan base-package="com.yzx.service" />
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
      <property name="driverClassName">  
          <value>com.mysql.jdbc.Driver</value>  
      </property>  
      <property name="url">  
          <value>jdbc:mysql://localhost:3306/market?characterEncoding=UTF-8</value>
      </property>  
      <property name="username">  
          <value>root</value>  
      </property>  
      <property name="password">  
          <value>root123</value>
      </property>   
    </bean>
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="typeAliasesPackage" value="com.yzx.entity" />
        <property name="dataSource" ref="dataSource"/>
        <property name="mapperLocations" value="classpath:com/yzx/mapper/xml/*.xml"/>
        <property name="plugins">
            <array>
              <bean class="com.github.pagehelper.PageInterceptor">
                <property name="properties">
                  <!--使用下面的方式配置參數,一行配置一個 -->
                  <value>
                  </value>
                </property>
              </bean>
            </array>
          </property>       
    </bean>
    <!--增長tkmybatis註解依賴-->
    <bean class="tk.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.yzx.mapper"/>
         <property name="beanName" value="normal"/>
    </bean>
    </beans>
  2. springMVC.xmlmysql

    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
    <context:annotation-config/>
    <mvc:default-servlet-handler/>
    <mvc:annotation-driven/>
    <context:component-scan base-package="com.yzx.controller">
          <context:include-filter type="annotation" 
          expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    <mvc:annotation-driven />
    <mvc:default-servlet-handler />
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
    </beans>
  3. mybatis依賴配置
    // 繼承Mapper,MySqlMapper  其餘業務Mapper繼續「MyMapper」,可直接操做增刪改查方法
    public interface MyMapper<T> extends Mapper<T>,MySqlMapper<T>{
    }

業務代碼

1.首頁代碼-controller層jquery

@RequestMapping(value = {"/","index","main",""})
public ModelAndView index(ModelAndView mav,HttpServletRequest request){
    Person person=(Person)request.getSession().getAttribute("user");
    List<Product> productList= productService.getProductList();
    if(null!=person){
        Trx trx=new Trx();
        if(person.getUserType().equals(0)){ //買家
            trx.setPersonId(person.getId());
        }
        List<Product> productList2=new ArrayList<>();
        List<Trx> trxList=trxService.getTrxList();  //根據當前登陸id查詢是否購買
        if(null!=trxList&&trxList.size()>0){
            for(Product p:productList){
                for(Trx t:trxList){
                    if(t.getProductId().equals(p.getId())){
                        p.setIsSell(1);
                    }
                }
                if(person.getUserType()==1){    //賣家角色 查詢當前商品買過多少
                    Trx t1=new Trx();
                    t1.setProductId(p.getId());
                    List<Trx> list=trxService.getTrxByParm(t1);
                    p.setBuyNum(list.size());
                }
                productList2.add(p);
            }
            mav.addObject("productList",productList2);  //過濾後的商品列表
        }
        mav.addObject("productList",productList);
    }else{
        mav.addObject("productList",productList);   //商品列表
    }
    mav.setViewName("index");
    return mav;
}

1.1 首頁代碼-service層git

@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    ProductMapper productMapper;
    //商品列表
    public List<Product> getProductList(){
        return productMapper.selectAll();
    }
}

1.2 首頁代碼-dao層-Mappergithub

//繼承MyMapper類,獲取超類的增刪改查方法
public interface ProductMapper extends MyMapper<Product> {
}

1.3 首頁代碼-jspweb

<%@ page language="java" contentType="text/html; charset=UTF-8"  pageEncoding="UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
    <meta charset="utf-8"/>
    <title>java</title>
    <link rel="stylesheet" href="/css/style.css"/>
</head>
<body>
<div class="n-support">請使用Chrome、Safari等webkit內核的瀏覽器!</div>
<div class="n-head">
    <div class="g-doc f-cb">
        <c:if test="${not empty user}">
            <div class="user">
                買家你好,<span class="name">${user.nickName}</span>!<a href="logout">[退出]</a>
            </div>

        </c:if>
        <c:if test="${ empty user}">
            請<a href="login">[登陸]</a>
        </c:if>
            <ul class="nav">
                <li><a href="index">首頁</a></li>
                <c:if test="${not empty user}">
                    <c:if test="${user.userType==0}">
                        <li><a href="account">帳務</a></li>
                       <li><a href="shopCart">購物車</a></li>
                    </c:if>
                    <c:if test="${user.userType==1}">
                       <li><a href="publish">發佈</a></li>
                    </c:if>
                </c:if>
            </ul>

    </div>
</div>
<div class="g-doc">
    <div class="m-tab m-tab-fw m-tab-simple f-cb">
        <div class="tab">
            <ul>
                <li class="z-sel"><a href="index">全部內容</a></li>
                <c:if test="${ user.userType!=1}">
                    <li><a href="isNoBuy">未購買的內容</a></li>
                </c:if>
            </ul>
        </div>
    </div>
    <div class="n-plist">
        <ul class="f-cb" id="plist">
            <c:forEach items="${productList}" var="product" varStatus="st">
                <li id="p-1">
                    <a href="details?pid=${product.id}" class="link">
                        <div class="img"><img src="${product.image}" alt=""></div>
                        <h3>${product.title}</h3>
                        <div class="price"><span class="v-unit">¥</span><span class="v-value">${product.price}</span></div>
                        <c:if test="${ not empty user}">
                            <c:if test="${ user.userType==0 and product.isSell==1}">
                                <span class="had"><b>已購買</b></span>
                            </c:if>
                            <c:if test="${ user.userType==1 and product.isSell==1}">
                                <span class="had"><b>已售出${product.buyNum}</b></span>
                            </c:if>
                        </c:if>
                    </a>
                    <c:if test="${ user.userType==1 and product.isSell==0}">
                        <span class="u-btn u-btn-normal u-btn-xs del" data-del="5">刪除</span>
                    </c:if>
                </li>
            </c:forEach>
        </ul>
    </div>
</div>
<script type="text/javascript" src="/js/global.js"></script>
<script type="text/javascript" src="/js/pageIndex.js"></script>
</body>
</html>

2 購物車列表-controllerajax

@RequestMapping(value = {"shopCart"})
public ModelAndView shopCart(ModelAndView mav, HttpServletRequest request){
    Person person=(Person) request.getSession().getAttribute("user");
    ShopCart shopCart3=new ShopCart();
    shopCart3.setUid(person.getId()+"");
    List<ShopCart> shopCarts=shopCartService.getShopCartList(shopCart3);
    mav.addObject("shopCarts",shopCarts);
    mav.setViewName("shopCart");
    return mav;
}

2.1 購物車列表-jsp頁面

<%@ page language="java" contentType="text/html; charset=UTF-8"  pageEncoding="UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
    <meta charset="utf-8"/>
    <title>java</title>
    <link rel="stylesheet" href="/css/style.css"/>
</head>
<body>
<div class="n-head">
    <div class="g-doc f-cb">
        <c:if test="${not empty user}">
            <div class="user">
                買家你好,<span class="name">${user.nickName}</span>!<a href="logout">[退出]</a>
            </div>

        </c:if>
        <c:if test="${ empty user}">
            請<a href="login">[登陸]</a>
        </c:if>
        <ul class="nav">
            <li><a href="index">首頁</a></li>
            <c:if test="${not empty user}">
                <c:if test="${user.userType==0}">
                    <li><a href="account">帳務</a></li>
                    <li><a href="shopCart">購物車</a></li>
                </c:if>
                <c:if test="${user.userType==1}">
                   <li><a href="publish">發佈</a></li>
                </c:if>
            </c:if>
        </ul>

    </div>
</div>
<div class="g-doc">
    <div class="m-tab m-tab-fw m-tab-simple f-cb" id="settleAccount">
        <h2>已添加到購物車的內容</h2>
    </div>
    <table class="m-table m-table-row n-table g-b3">
        <colgroup><col class="img"/><col/><col class="time"/><col class="price"/></colgroup>
        <thead>
        <tr><th>內容圖片</th><th>內容名稱</th><th>購買時間</th><th>數量</th><th>購買價格</th></tr>
        </thead>
        <tbody>
            <c:forEach items="${shopCarts}" var="cart">
                <tr>
                    <td><a href="details?pid=${cart.pid}"><img src="${cart.image}" alt=""></a></td>
                    <td><h4><a href="details?pid=${cart.pid}">${cart.title}</a></h4></td>
                    <td><span class="v-time">${cart.time}</span></td>
                    <td><span class="v-num">${cart.num}</span></td>
                    <td><span class="v-unit">¥</span><span class="value">${cart.price}</span></td>
                </tr>
            </c:forEach>
        </tbody>
        <tfoot>
        <tr>
            <td><button class="u-btn u-btn-primary" onclick="goout();">退出</button></td>
            <td><button class="u-btn u-btn-primary" onclick="buy();">購買</button></td>
        </tr>
        </tfoot>
    </table>
</div>
<script type="text/javascript" src="/js/global.js"></script>
<script type="text/javascript" src="/js/pageShow.js"></script>
<script type="text/javascript"  src="/js/jquery-3.3.1.js" ></script>

<script type="text/javascript"  >
    function goout() {
        window.history.go(-1);
    }
    function buy(){
        if(confirm("肯定購買嗎?")){
            $.ajax({
                type:"post",
                url:"saveTrx",
                data:{pid:$("#pid").val()},
                dataType:"json",//返回的
                success:function(data) {
                    if(data==0) {
                        window.location.href="account";
                    } else {
                        alert('用戶密碼錯誤,請從新登陸');
                    }
                },
                error:function(msg) {
                    alert(msg);
                }
            });
        }
    }
</script>
</body>
</html>

項目總結

項目作得比較簡易,頁面偏向簡潔,不過有一個完整的商城主流程,此外還編寫了freemaker版本的購物商城代碼,後續上傳源碼。

程序有問題聯繫程序幫

相關文章
相關標籤/搜索