ssm實(shí)戰(zhàn)購物商城系統(tǒng)

本系統(tǒng)使用Spring+SpringMVC+MyBatis架構(gòu)梁丘,數(shù)據(jù)庫使用MySQL,開發(fā)完成了從商家發(fā)布商品烦粒,到用戶查看商品并下單購買這樣的一個(gè)閉合的流程祝高。

開發(fā)環(huán)境

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

所用技術(shù)

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

項(xiàng)目架構(gòu)

項(xiàng)目結(jié)構(gòu).png

項(xiàng)目截圖

  • 登錄


    登錄.png
  • 首頁


    買家-首頁.png
  • 商品詳情


    商品詳情.png
  • 購物車


    購物車.png
  • 訂單詳情


    訂單詳情.png

框架配置

  1. applicationContext.xml
<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">
                  <!--使用下面的方式配置參數(shù)恬偷,一行配置一個(gè) -->
                  <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>
  1. springMVC.xml
<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>   
  1. mybatis依賴配置
// 繼承Mapper失乾,MySqlMapper  其他業(yè)務(wù)Mapper繼續(xù)“MyMapper”宪哩,可直接操作增刪改查方法
public interface MyMapper<T> extends Mapper<T>,MySqlMapper<T>{
}

業(yè)務(wù)代碼

1.首頁代碼-controller層

@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();  //根據(jù)當(dāng)前登錄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){    //賣家角色 查詢當(dāng)前商品買過多少
                    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層

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

1.2 首頁代碼-dao層-Mapper

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

1.3 首頁代碼-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-support">請(qǐng)使用Chrome、Safari等webkit內(nèi)核的瀏覽器锁孟!</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}">
            請(qǐng)<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">賬務(wù)</a></li>
                       <li><a href="shopCart">購物車</a></li>
                    </c:if>
                    <c:if test="${user.userType==1}">
                       <li><a href="publish">發(fā)布</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">所有內(nèi)容</a></li>
                <c:if test="${ user.userType!=1}">
                    <li><a href="isNoBuy">未購買的內(nèi)容</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 購物車列表-controller

@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}">
            請(qǐng)<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">賬務(wù)</a></li>
                    <li><a href="shopCart">購物車</a></li>
                </c:if>
                <c:if test="${user.userType==1}">
                   <li><a href="publish">發(fā)布</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>已添加到購物車的內(nèi)容</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>內(nèi)容圖片</th><th>內(nèi)容名稱</th><th>購買時(shí)間</th><th>數(shù)量</th><th>購買價(jià)格</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('用戶密碼錯(cuò)誤圆恤,請(qǐng)重新登錄');
                    }
                },
                error:function(msg) {
                    alert(msg);
                }
            });
        }
    }
</script>
</body>
</html>

項(xiàng)目總結(jié)

項(xiàng)目做得比較簡易突倍,頁面偏向簡潔,不過有一個(gè)完整的商城主流程盆昙,此外還編寫了freemaker版本的購物商城代碼羽历,后續(xù)上傳源碼。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末淡喜,一起剝皮案震驚了整個(gè)濱河市秕磷,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌炼团,老刑警劉巖澎嚣,帶你破解...
    沈念sama閱讀 219,270評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異瘟芝,居然都是意外死亡易桃,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,489評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門锌俱,熙熙樓的掌柜王于貴愁眉苦臉地迎上來晤郑,“玉大人,你說我怎么就攤上這事贸宏》泛海” “怎么了?”我有些...
    開封第一講書人閱讀 165,630評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵锚赤,是天一觀的道長。 經(jīng)常有香客問我褐鸥,道長线脚,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,906評(píng)論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮浑侥,結(jié)果婚禮上姊舵,老公的妹妹穿的比我還像新娘。我一直安慰自己寓落,他們只是感情好括丁,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,928評(píng)論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著伶选,像睡著了一般史飞。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上仰税,一...
    開封第一講書人閱讀 51,718評(píng)論 1 305
  • 那天构资,我揣著相機(jī)與錄音,去河邊找鬼陨簇。 笑死吐绵,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的河绽。 我是一名探鬼主播己单,決...
    沈念sama閱讀 40,442評(píng)論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼耙饰!你這毒婦竟也來了纹笼?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,345評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤榔幸,失蹤者是張志新(化名)和其女友劉穎允乐,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體削咆,經(jīng)...
    沈念sama閱讀 45,802評(píng)論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡牍疏,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,984評(píng)論 3 337
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了拨齐。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片鳞陨。...
    茶點(diǎn)故事閱讀 40,117評(píng)論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖瞻惋,靈堂內(nèi)的尸體忽然破棺而出厦滤,到底是詐尸還是另有隱情,我是刑警寧澤歼狼,帶...
    沈念sama閱讀 35,810評(píng)論 5 346
  • 正文 年R本政府宣布掏导,位于F島的核電站,受9級(jí)特大地震影響羽峰,放射性物質(zhì)發(fā)生泄漏趟咆。R本人自食惡果不足惜添瓷,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,462評(píng)論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望值纱。 院中可真熱鬧鳞贷,春花似錦、人聲如沸虐唠。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,011評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽疆偿。三九已至咱筛,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間翁脆,已是汗流浹背眷蚓。 一陣腳步聲響...
    開封第一講書人閱讀 33,139評(píng)論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留反番,地道東北人沙热。 一個(gè)月前我還...
    沈念sama閱讀 48,377評(píng)論 3 373
  • 正文 我出身青樓,卻偏偏與公主長得像罢缸,于是被迫代替她去往敵國和親篙贸。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,060評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容