一、系统架构分析

1、SOA架构

1.1什么是SOA架构
SOA是Service-Oriented Architecture的首字母简称,它是一种支持面向服务的架构样式。从服务、基于服务开发和服务的结果来看,面向服务是一种思考方式。其实SOA架构更多应用于互联网项目开发。
为什么互联网项目会采用SOA架构呢?随着互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,迫切需一个治理系统确保架构有条不紊的演进。

2、数据库表结构分析

表名称

含义

tb_brand

品牌

tb_specification

规格

tb_specification_option

规格选项

tb_type_template

类型模板:用于关联品牌和规格

tb_item_cat

商品分类

tb_seller

商家

tb_goods

商品

tb_goods_desc

商品详情

tb_item

商品明细

tb_content

内容(广告)

tb_content_category

内容(广告)类型

tb_user

用户

tb_order

订单

tb_order_item

订单明细

tb_pay_log

支付日志

二、框架组合

本电商系统采用当前流行的前后端编程架构。
后端框架采用Spring +SpringMVC+mybatis +Dubbox 。前端采用angularJS + Bootstrap。

三、框架搭建

3.1工程结构分析与设计

最终完整的工程结构如下:

电商平台的系统架构图 电商平台技术架构_电商平台的系统架构图


工程说明:

pinyougou-parent 聚合工程

pinyougou-common 通用工程

pinyougou-pojo 通用实体类层

pinyougou-dao 通用数据访问层

pinyougou-xxxxx-interface 某服务层接口

pinyougou-xxxxx-service 某服务层实现

pinyougou-xxxxx-web 某web工程

3.2创建数据库表

执行资源文件夹中pinyougou-db.sql

3.3搭建框架

3.3.1父工程

创建Maven工程pinyougou-parent (POM) ,groupId 为com.pinyougou ,artifactId 为pinyougou-parent , 在pom.xml中添加锁定版本信息dependencyManagement与pluginManagement,详见“资源/配置文件/第一天搭建/父工程/pom.xml”。
以下模块均继承自此父工程

3.3.2通用实体类模块

创建通用实体类模块-pinyougou-pojo

3.3.3通用数据访问模块

创建通用数据访问模块pinyougou-dao .在pom文件中添加依赖Mybatis和pinyougou-pojo

<dependencies>
 		<!-- Mybatis -->
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis</artifactId>
		</dependency>
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis-spring</artifactId>			
		</dependency>
		<dependency>
			<groupId>com.github.miemiedev</groupId>
			<artifactId>mybatis-paginator</artifactId>
		</dependency>		
		<!-- MySql -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
		</dependency>
		<!-- 连接池 -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid</artifactId>
		</dependency>	
		<dependency>
			<groupId>com.pinyougou</groupId>
			<artifactId>pinyougou-pojo</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency> 
  </dependencies>

3.3.4通用工具类模块

创建通用工具类模块pinyougou-common

3.3.5商家商品服务接口模块

创建maven(jar)模块pinyougou-sellergoods-interface , pom.xml添加依赖

<dependencies>
  	<dependency>
  		<groupId>com.pinyougou</groupId>
  		<artifactId>pinyougou-pojo</artifactId>
  		<version>0.0.1-SNAPSHOT</version>
  	</dependency>
</dependencies>

3.3.6商家商品服务模块

创建maven(war)模块pinyougou-sellergoods-service ,pom.xml引入依赖

<dependencies>
	<!-- 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>
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-test</artifactId>		
	</dependency>
	<!-- dubbo相关 -->
	<dependency>
		<groupId>com.alibaba</groupId>
		<artifactId>dubbo</artifactId>	
	</dependency>
	<dependency>
		<groupId>org.apache.zookeeper</groupId>
		<artifactId>zookeeper</artifactId>	
	</dependency>
	<dependency>
		<groupId>com.github.sgroschupf</groupId>
		<artifactId>zkclient</artifactId>		
	</dependency>
	<dependency>
		<groupId>junit</groupId>
		<artifactId>junit</artifactId>
	</dependency>
	<dependency>
		<groupId>com.alibaba</groupId>
		<artifactId>fastjson</artifactId>
	</dependency>
	<dependency>
		<groupId>javassist</groupId>
		<artifactId>javassist</artifactId>		
	</dependency>
	<dependency>
	    <groupId>commons-codec</groupId>
	    <artifactId>commons-codec</artifactId>	  
	</dependency>
	<dependency>
		<groupId>javax.servlet</groupId>
		<artifactId>servlet-api</artifactId>	
		<scope>provided</scope>
	</dependency>  
  	<dependency>
  		<groupId>com.pinyougou</groupId>
  		<artifactId>pinyougou-common</artifactId>
  		<version>0.0.1-SNAPSHOT</version>
  	</dependency>
  	<dependency>
  		<groupId>com.pinyougou</groupId>
  		<artifactId>pinyougou-dao</artifactId>
  		<version>0.0.1-SNAPSHOT</version>
  	</dependency>
  	<dependency>
  		<groupId>com.pinyougou</groupId>
  		<artifactId>pinyougou-sellergoods-interface</artifactId>
  		<version>0.0.1-SNAPSHOT</version>
  	</dependency>
  </dependencies>  
   <build>
		<plugins>
			<!-- 配置Tomcat插件 -->
			<plugin>
				<groupId>org.apache.tomcat.maven</groupId>
				<artifactId>tomcat7-maven-plugin</artifactId>
				<configuration>
					<path>/</path>
					<port>9001</port>
				</configuration>
			</plugin>
		</plugins>
    </build>
</project>

在webapps下创建WEB-INF/web.xml ,加载spring容器

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	version="2.5">	
	<!-- 加载spring容器 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath*:spring/applicationContext*.xml</param-value>
	</context-param>
	<listener>
	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
</web-app>

创建包com.pinyougou.sellergoods.service.impl
在src/main/resources下创建spring/applicationContext-service.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

     <dubbo:protocol name="dubbo" port="20881"></dubbo:protocol>
	<dubbo:application name="pinyougou-sellergoods-service"/>  
    <dubbo:registry address="zookeeper://192.168.25.129:2181"/>
<dubbo:annotation package="com.pinyougou.sellergoods.service.impl" />  
</beans>

3.3.7运营商管理后台

创建maven(war)模块pinyougou-manager-web , pom.xml引入依赖

<dependencies>
	<!-- 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>
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-test</artifactId>		
	</dependency>
	<!-- dubbo相关 -->
	<dependency>
		<groupId>com.alibaba</groupId>
		<artifactId>dubbo</artifactId>	
	</dependency>
	<dependency>
		<groupId>org.apache.zookeeper</groupId>
		<artifactId>zookeeper</artifactId>	
	</dependency>
	<dependency>
		<groupId>com.github.sgroschupf</groupId>
		<artifactId>zkclient</artifactId>		
	</dependency>
	<dependency>
		<groupId>junit</groupId>
		<artifactId>junit</artifactId>
	</dependency>
	<dependency>
		<groupId>com.alibaba</groupId>
		<artifactId>fastjson</artifactId>
	</dependency>
	<dependency>
		<groupId>javassist</groupId>
		<artifactId>javassist</artifactId>		
	</dependency>
	<dependency>
	    <groupId>commons-codec</groupId>
	    <artifactId>commons-codec</artifactId>	  
	</dependency>
	<dependency>
		<groupId>javax.servlet</groupId>
		<artifactId>servlet-api</artifactId>	
		<scope>provided</scope>
	</dependency>  
  	<dependency>
  		<groupId>com.pinyougou</groupId>
  		<artifactId>pinyougou-common</artifactId>
  		<version>0.0.1-SNAPSHOT</version>
  	</dependency>
  	<dependency>
  		<groupId>com.pinyougou</groupId>
  		<artifactId>pinyougou-sellergoods-interface</artifactId>
  		<version>0.0.1-SNAPSHOT</version>
  	</dependency>
  </dependencies>
  <build>
	 <plugins>
		<!-- 配置Tomcat插件 -->
		<plugin>
			<groupId>org.apache.tomcat.maven</groupId>
			<artifactId>tomcat7-maven-plugin</artifactId>
			<configuration>
				<path>/</path>
				<port>9101</port>
			</configuration>
		</plugin>
	 </plugins>
  </build>

在webapps下创建WEB-INF/web.xml ,加载spring容器

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	version="2.5">	
   <!-- 解决post乱码 -->
	<filter>
		<filter-name>CharacterEncodingFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>utf-8</param-value>
		</init-param>
		<init-param>  
            <param-name>forceEncoding</param-name>  
            <param-value>true</param-value>  
        </init-param>  
	</filter>
	<filter-mapping>
		<filter-name>CharacterEncodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>	
  <servlet>
  	<servlet-name>springmvc</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  	<!-- 指定加载的配置文件 ,通过参数contextConfigLocation加载-->
  	<init-param>
  		<param-name>contextConfigLocation</param-name>
  		<param-value>classpath:spring/springmvc.xml</param-value>
  	</init-param>
  </servlet>
  <servlet-mapping>
  	<servlet-name>springmvc</servlet-name>
  	<url-pattern>*.do</url-pattern>
  </servlet-mapping>
</web-app>
创建包com.pinyougou.manager.controller
在src/main/resources下创建spring/springmvc.xml 
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
	<mvc:annotation-driven>
	  <mvc:message-converters register-defaults="true">
	    <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">  
	      <property name="supportedMediaTypes" value="application/json"/>
	      <property name="features">
	        <array>
	          <value>WriteMapNullValue</value>
	          <value>WriteDateUseDateFormat</value>
	        </array>
	      </property>
	    </bean>
	  </mvc:message-converters>  
	</mvc:annotation-driven>
	<!-- 引用dubbo 服务 -->
	<dubbo:application name="pinyougou-manager-web" />
	<dubbo:registry address="zookeeper://192.168.25.132:2181"/>
	<dubbo:annotation package="com.pinyougou.manager.controller" />  	
</beans>

3.3.8商家管理后台

构建web模块pinyougou-shop-web 与运营商管理后台的构建方式类似。区别:
(1)定义tomcat的启动端口为9102
(2)springmvc.xml

<!-- 引用dubbo 服务 -->
<dubbo:application name="pinyougou-shop-web" />
<dubbo:registry address="zookeeper://192.168.25.132:2181"/>
<dubbo:annotation package="com.pinyougou.shop.controller" />

3.4实体类与数据访问层模块

3.4.1 生成代码

利用反向工程generatorSqlmapCustom实现实体类与数据访问层代码的自动生成

3.4.2 拷贝代码

将com.pinyougou.pojo包拷贝到pojo工程
将com.pinyougou.mapper包和resouce下的com.pinyougou.mapper文件夹拷贝到dao工程

3.4.3 修改实体类代码

修改每个实体类,让其实现Serializable接口

4.品牌列表-后端代码

4.1需求分析

完成品牌管理的后端代码,在浏览器可查询品牌的数据(json格式)

4.2数据库表

tb_brand 品牌表

字段

类型

长度

含义

Id

Bigint

主键

Name

Varchar

255

品牌名称

First_char

Varchar

1

品牌首字母

4.3后端代码

4.3.1 服务层接口

在pinyougou-sellergoods-interface 工程创建BrandService接口

package com.pinyougou.sellergoods.service;
import java.util.List;
import com.pinyougou.pojo.TbBrand;
/**
 * 品牌服务层接口
 * @author Administrator
 *
 */
public interface BrandService {
	/**
	 * 返回全部列表
	 * @return
	 */
	public List<TbBrand> findAll();
}

4.3.2 服务实现类

在pinyougou-sellergoods-service 工程创建BrandServiceImpl类

package com.pinyougou.sellergoods.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import com.alibaba.dubbo.config.annotation.Service;
import com.pinyougou.mapper.TbBrandMapper;
import com.pinyougou.pojo.TbBrand;
import com.pinyougou.sellergoods.service.BrandService;
@Service
public class BrandServiceImpl implements BrandService {
	@Autowired
	private TbBrandMapper brandMapper;
	@Override
	public List<TbBrand> findAll() {
		return brandMapper.selectByExample(null);
	}
}

4.3.3 控制层代码

在pinyougou-manager-web工程创建com.pinyougou.manager.controller包,包下创建BrandController类

package com.pinyougou.manager.controller;
import java.util.List;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.pinyougou.pojo.TbBrand;
import com.pinyougou.sellergoods.service.BrandService;
/**
 * 品牌controller
 * @author Administrator
 */
@RestController
@RequestMapping("/brand")
public class BrandController {
	@Reference
	private BrandService brandService;
	/**
	 * 返回全部列表
	 * @return
	 */
	@RequestMapping("/findAll")
	public List<TbBrand> findAll(){			
		return brandService.findAll();
	}
}

4.4测试

启动pinyougou-sellergoods-service
启动pinyougou-manager-web
地址栏输入http://localhost:9101/brand/findAll.do

可以看到浏览器输出了json数据。

电商平台的系统架构图 电商平台技术架构_java_02