SSM商城項目(五)


1.   學習計划

1、前台系統搭建

2、商城首頁展示

3、Cms系統的實現

a)         內容分類管理

b)         內容管理

4、前台內容動態展示

2.   商城首頁展示

2.1. 工程搭建

參考e3-manager-web工程,搭建e3-portal-web。

復制e3-manager-web的pom.xml、springmvc.xml和web.xml文件。

2.2導入頁面

2.3. 功能分析

請求的url:/index

web.xml中的歡迎頁配置:

IndexController.java
package cn.e3mall.portal.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class IndexController {
    @RequestMapping("/index")
    public String showIndex(){
        return "index";
    }
}

http://localhost:8083

參數:沒有

返回值:String 邏輯視圖

測試:

 

4.   首頁動態展示分析

內容信息要從數據庫中獲得

1、內容需要進行分類

2、分類下有子分類,需要動態管理。

3、分類下有內容列表

4、單點的內容信息

a)         有圖片

b)        有鏈接

c)         有標題

d)        有價格

e)         包含大文本類型,可以作為公告

 

需要一個內容分類表和一個內容表。內容分類和內容表是一對多的關系。

內容分類表,需要存儲樹形結構的數據。

內容分類表:tb_content_category

內容表:tb_content

 

需要有后台來維護內容信息。Cms系統。

 

需要創建一個內容服務系統。可以參考e3-manager創建。

E3-content:聚合工程打包方式pom

|--e3-content-interface jar

|--e3-content-Service  war

4.   內容服務系統創建

4.1. 工程搭建

可以參考e3-manager工程搭建。

 e3-content

 

 e3-content-interface

 e3-content-service

 

復制e3-manager-service以下文件到e3-content-service下,簡單修改applicationContext-service.xml下的包名和接口,修改applicationContext-dao.xml下的部分路徑,配置web.xml。

4.2. E3-content

 Pom文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>cn.e3mall</groupId>
    <artifactId>e3-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>e3-content</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>pom</packaging>
  <dependencies>
        <dependency>
            <groupId>cn.e3mall</groupId>
            <artifactId>e3-common</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>
  <modules>
      <module>e3-content-interface</module>
      <module>e3-content-service</module>
  </modules>
  <!-- 配置tomcat插件 -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <configuration>
                    <port>8084</port>
                    <path>/</path>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

4.2. E3-content-interface

 Pom文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>cn.e3mall</groupId>
    <artifactId>e3-content</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>e3-content-interface</artifactId>
      <dependencies>
        <dependency>
            <groupId>cn.e3mall</groupId>
            <artifactId>e3-manager-pojo</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

4.2. E3-content-service

 Pom文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>cn.e3mall</groupId>
    <artifactId>e3-content</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>e3-content-service</artifactId>
  <packaging>war</packaging>
  <dependencies>
        <dependency>
            <groupId>cn.e3mall</groupId>
            <artifactId>e3-manager-dao</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>cn.e3mall</groupId>
            <artifactId>e3-content-interface</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <!-- spring的依賴 -->
        <!-- Spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jms</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
        </dependency>
        <!-- dubbo相關 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.jboss.netty</groupId>
                    <artifactId>netty</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
        </dependency>
        <dependency>
            <groupId>com.github.sgroschupf</groupId>
            <artifactId>zkclient</artifactId>
        </dependency>
    </dependencies>
</project>

5.   Cms系統實現

5.1. 內容分類管理

5.1.1.    展示內容分類

功能分析

請求的url:/content/category/list

請求的參數:id,當前節點的id。第一次請求是沒有參數,需要給默認值“0”

響應數據:List<EasyUITreeNode>(@ResponseBody)

Json數據。

[{id:1,text:節點名稱,state:open(closed)},

{id:2,text:節點名稱2,state:open(closed)},

{id:3,text:節點名稱3,state:open(closed)}]

業務邏輯:

1、取查詢參數id,parentId

2、根據parentId查詢tb_content_category,查詢子節點列表。

3、得到List<TbContentCategory>

4、把列表轉換成List<EasyUITreeNode>

Dao層

使用逆向工程

Service

參數:long parentId

返回值:List<EasyUITreeNode>

 

package cn.e3mall.content.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.e3mall.common.pojo.EasyUITreeNode;
import cn.e3mall.content.service.ContentCategoryService;
import cn.e3mall.mapper.TbContentCategoryMapper;
import cn.e3mall.pojo.TbContentCategory;
import cn.e3mall.pojo.TbContentCategoryExample;
import cn.e3mall.pojo.TbContentCategoryExample.Criteria;

@Service
public class ContentCategoryServiceImpl implements ContentCategoryService{

    @Autowired
    private TbContentCategoryMapper contentCategoryMapper;

    @Override
    public List<EasyUITreeNode> getContentCategoryList(long parentId) {
        // 1、取查詢參數id,parentId
                // 2、根據parentId查詢tb_content_category,查詢子節點列表。
                TbContentCategoryExample example = new TbContentCategoryExample();
                //設置查詢條件
                Criteria criteria = example.createCriteria();
                criteria.andParentIdEqualTo(parentId);
                //執行查詢
                // 3、得到List<TbContentCategory>
                List<TbContentCategory> list = contentCategoryMapper.selectByExample(example);
                // 4、把列表轉換成List<EasyUITreeNode>ub
                List<EasyUITreeNode> resultList = new ArrayList<>();
                for (TbContentCategory tbContentCategory : list) {
                    EasyUITreeNode node = new EasyUITreeNode();
                    node.setId(tbContentCategory.getId());
                    node.setText(tbContentCategory.getName());
                    node.setState(tbContentCategory.getIsParent()?"closed":"open");
                    //添加到列表
                    resultList.add(node);
                }
                return resultList;
            }

    
}

發布服務

<!-- 聲明需要暴露的服務接口 -->

<dubbo:service interface="cn.e3mall.content.service.ContentCategoryService" ref="contentCategoryServiceImpl" timeout="600000"/>

表現層

E3-manager-web

依賴e3-content-interface模塊

Springmvc.xml中添加服務的引用:

 <dubbo:reference interface="cn.e3mall.content.service.ContentCategoryService" id="contentCategoryService"/>

Controller

    @RequestMapping("/content/category/list")
    @ResponseBody
    public List<EasyUITreeNode> getContentCatList(
            @RequestParam(value="id", defaultValue="0") Long parentId) {
        
        List<EasyUITreeNode> list = contentCategoryService.getContentCategoryList(parentId);
        return list;
    }

 

5.1.2.    新增節點

功能分析

 

 

請求的url:/content/category/create

請求的參數:

Long parentId

String name

響應的結果:

json數據,E3Result,其中包含一個對象,對象有id屬性,新生產的內容分類id

業務邏輯:

1、接收兩個參數:parentId、name

2、向tb_content_category表中插入數據。

a)         創建一個TbContentCategory對象

b)        補全TbContentCategory對象的屬性

c)         向tb_content_category表中插入數據

3、判斷父節點的isparent是否為true,不是true需要改為true。

4、需要主鍵返回。

5、返回E3Result,其中包裝TbContentCategory對象

Dao層

可以使用逆向工程。

需要添加主鍵返回:

 

Service層

參數:parentId、name

返回值:返回E3Result,其中包裝TbContentCategory對象

    @Override
    public E3Result addContentCategory(long parentId, String name) {
        // 1、接收兩個參數:parentId、name
        // 2、向tb_content_category表中插入數據。
        // a)創建一個TbContentCategory對象
        TbContentCategory tbContentCategory = new TbContentCategory();
        // b)補全TbContentCategory對象的屬性
        tbContentCategory.setIsParent(false);
        tbContentCategory.setName(name);
        tbContentCategory.setParentId(parentId);
        //排列序號,表示同級類目的展現次序,如數值相等則按名稱次序排列。取值范圍:大於零的整數
        tbContentCategory.setSortOrder(1);
        //狀態。可選值:1(正常),2(刪除)
        tbContentCategory.setStatus(1);
        tbContentCategory.setCreated(new Date());
        tbContentCategory.setUpdated(new Date());
        // c)向tb_content_category表中插入數據
        contentCategoryMapper.insert(tbContentCategory);
        // 3、判斷父節點的isparent是否為true,不是true需要改為true。
        TbContentCategory parentNode = contentCategoryMapper.selectByPrimaryKey(parentId);
        if (!parentNode.getIsParent()) {
            parentNode.setIsParent(true);
            //更新父節點
            contentCategoryMapper.updateByPrimaryKey(parentNode);
        }
        // 4、需要主鍵返回。
        // 5、返回E3Result,其中包裝TbContentCategory對象
        return E3Result.ok(tbContentCategory);
    }  

發布服務。(前面已經發過了)

表現層

請求的url:/content/category/create

請求的參數:

Long parentId

String name

響應的結果:

json數據,E3Result

@RequestMapping("/create")
    @ResponseBody
    public E3Result createCategory(Long parentId, String name) {
        E3Result result = contentCategoryService.addContentCategory(parentId, name);
        return result;
    }

5.2. 內容管理

新增內容

功能分析

 

新增內容,必須指定一個內容分類。

提交表單請求的url:/content/save

參數:表單的數據。使用pojo接收TbContent

返回值:E3Result(json數據)

業務邏輯:

1、把TbContent對象屬性補全。

2、向tb_content表中插入數據。

3、返回E3Result

Dao

逆向工程

Service

參數:TbContent

返回值:E3Result

@Service
public class ContentServiceImpl implements ContentService {

    @Autowired
    private TbContentMapper contentMapper;
    
    @Override
    public E3Result addContent(TbContent content) {
        //補全屬性
        content.setCreated(new Date());
        content.setUpdated(new Date());
        //插入數據
        contentMapper.insert(content);
        return E3Result.ok();
    }

}

發布服務

<dubbo:service interface="cn.e3mall.content.service.ContentService" ref="contentServiceImpl" timeout="600000"/>

引用服務

<dubbo:reference interface="cn.e3mall.content.service.ContentService" id="contentService" />

Controller

提交表單請求的url:/content/save

參數:表單的數據。使用pojo接收TbContent

返回值:E3Result(json數據)

@Controller
public class ContentController {

    @Autowired
    private ContentService contentService;
    
    @RequestMapping("/content/save")
    @ResponseBody
    public E3Result addContent(TbContent content) {
        E3Result result = contentService.addContent(content);
        return result;
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM