本帖最后由 我是楠楠 于 2020-5-7 11:09 编辑
【郑州校区】品优购电商系统开发第 1 章分布式框架-Dubbox 四
4.品优购-框架搭建
4.1 工程结构分析与设计
最终完整的工程结构如下:
工程说明:
pinyougou-parent 聚合工程
pinyougou-pojo 通用实体类层
pinyougou-dao 通用数据访问层
pinyougou-xxxxx-interface 某服务层接口
pinyougou-xxxxx-service 某服务层实现
pinyougou-xxxxx-web 某 web 工程
4.2 创建数据库表
执行资源文件夹中 pinyougou-db.sql
4.3 搭建框架
4.3.1 父工程
创建 Maven 工程 pinyougou-parent (POM) ,groupId 为 com.pinyougou ,artifactId 为pinyougou-parent , 在 pom.xml 中 添 加 锁 定 版 本 信 息 dependencyManagement 与
pluginManagement,详见“资源/配置文件/第一天搭建/父工程/pom.xml”。
以下模块均继承自此父工程
4.3.2 通用实体类模块
创建通用实体类模块-pinyougou-pojo
4.3.3 通用数据访问模块
创建通用数据访问模块 pinyougou-dao .添加依赖 Mybatis 和 pinyougou-pojo
[AppleScript] 纯文本查看 复制代码 <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>
将“配置文件/第一天搭建/数据访问层工程”下的配置文件拷贝到 pinyougou-dao 工程
4.3.4 通用工具类模块
创建通用工具类模块 pinyougou-common
4.3.5 商家商品服务接口模块
创建 maven(jar)模块 pinyougou-sellergoods-interface , pom.xml 添加依赖
[AppleScript] 纯文本查看 复制代码 <dependencies>
<dependency>
<groupId>com.pinyougou</groupId>
<artifactId>pinyougou-pojo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
4.3.6 商家商品服务模块
创建 maven(war)模块 pinyougou-sellergoods-service ,pom.xml 引入依赖
[AppleScript] 纯文本查看 复制代码 <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 容器
[AppleScript] 纯文本查看 复制代码 <?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
[url=http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd]http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd[/url]"
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
[AppleScript] 纯文本查看 复制代码 <?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
[url=http://www.springframework.org/schema/beans/spring-beans.xsd]http://www.springframework.org/schema/beans/spring-beans.xsd[/url]
[url=http://www.springframework.org/schema/mvc]http://www.springframework.org/schema/mvc[/url]
[url=http://www.springframework.org/schema/mvc/spring-mvc.xsd]http://www.springframework.org/schema/mvc/spring-mvc.xsd[/url]
[url=http://code.alibabatech.com/schema/dubbo]http://code.alibabatech.com/schema/dubbo[/url]
[url=http://code.alibabatech.com/schema/dubbo/dubbo.xsd]http://code.alibabatech.com/schema/dubbo/dubbo.xsd[/url]
[url=http://www.springframework.org/schema/context]http://www.springframework.org/schema/context[/url]
[url=http://www.springframework.org/schema/context/spring-context.xsd]http://www.springframework.org/schema/context/spring-context.xsd[/url]">
<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>
4.3.7 运营商管理后台
创建 maven(war)模块 pinyougou-manager-web , pom.xml 引入依赖
[AppleScript] 纯文本查看 复制代码 <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 容器
[AppleScript] 纯文本查看 复制代码 <?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
[url]http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd[/url]"
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
[AppleScript] 纯文本查看 复制代码 <?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
[url=http://www.springframework.org/schema/beans/spring-beans.xsd]http://www.springframework.org/schema/beans/spring-beans.xsd[/url]
[url=http://www.springframework.org/schema/mvc]http://www.springframework.org/schema/mvc[/url]
[url=http://www.springframework.org/schema/mvc/spring-mvc.xsd]http://www.springframework.org/schema/mvc/spring-mvc.xsd[/url]
[url=http://code.alibabatech.com/schema/dubbo]http://code.alibabatech.com/schema/dubbo[/url]
[url=http://code.alibabatech.com/schema/dubbo/dubbo.xsd]http://code.alibabatech.com/schema/dubbo/dubbo.xsd[/url]
[url=http://www.springframework.org/schema/context]http://www.springframework.org/schema/context[/url]
[url=http://www.springframework.org/schema/context/spring-context.xsd]http://www.springframework.org/schema/context/spring-context.xsd[/url]">
<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>
4.3.8 商家管理后台
构建 web 模块 pinyougou-shop-web 与运营商管理后台的构建方式类似。区别:
(1)定义 tomcat 的启动端口为 9102
(2)springmvc.xml
[AppleScript] 纯文本查看 复制代码 <!-- 引用 dubbo 服务 -->
<dubbo:application name="pinyougou-shop-web" />
<dubbo:registry address="zookeeper://192.168.25.132:2181"/>
<dubbo:annotation package="com.pinyougou.shop.controller" />
4.4 实体类与数据访问层模块
4.4.1 生成代码
利用反向工程 generatorSqlmapCustom 实现实体类与数据访问层代码的自动生成
4.4.2 拷贝代码
将 com.pinyougou.pojo 包拷贝到 pojo 工程
将 com.pinyougou.mapper 包和 resouce 下的 com.pinyougou.mapper 文件夹拷贝到 dao 工程
4.4.3 修改实体类代码
修改每个实体类,让其实现 Serializable 接口
|