十四、商城 - 商城架构-SSM(2) 一、商城 - 商城架构 二、品牌列表-后端代码

2023年3月29日

目录 🌻🌻

  • 一、商城 - 商城架构
    • 1.1 工程结构分析与设计
    • 1.2 创建数据库表
    • 1.3 搭建框架
      • 1.3.1 父工程
      • 1.3.2 通用实体类模块
      • 1.3.3 通用数据访问模块
      • 1.3.4 通用工具类模块
      • 1.3.5 商家商品服务接口模块
      • 1.3.6 商家商品服务模块
      • 1.3.7 运营商管理后台
      • 1.3.8 商家管理后台
    • 1.4 实体类与数据访问层模块
      • 1.4.1 生成代码
      • 1.4.2 拷贝代码
      • 1.4.3 修改实体类代码
  • 二、品牌列表-后端代码
    • 2.1 需求分析
    • 2.2 后端代码
      • 2.2.1 服务层接口
      • 2.2.2 服务实现类
      • 2.2.3 控制层代码
    • 2.3 测试
    • 2.4 快捷启动 tomcat

一、商城 - 商城架构

1.1 工程结构分析与设计

工程说明:

  • youlexuan_parent 聚合工程
  • youlexuan_pojo 通用实体类层
  • youlexuan_dao 通用数据访问层
  • youlexuan_xxxxx_interface 某服务层接口
  • youlexuan_xxxxx_service 某服务层实现
  • youlexuan_xxxxx_web 某web工程

1.2 创建数据库表

创建 youlexuan 数据库而后执行 youlexuan_db.sql

1.3 搭建框架

1.3.1 父工程

  • 创建Maven工程 (POM) ,
  • groupId 为com.zql,
  • artifactId 为 youlexuan_parent ,

maven 的准备


在pom.xml中添加锁定版本信息 dependencyManagement 与 pluginManagement。

以下模块均继承自此父工程


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.0modelVersion>

    groupId>com.zqlgroupId>
    artifactId>youlexuan_parentartifactId>
    version>1.0version>
    packaging>pompackaging>

    
    properties>
        commons.io.version>2.1commons.io.version>
        junit.version>4.12junit.version>
        spring.version>4.2.4.RELEASEspring.version>
        pagehelper.version>4.0.0pagehelper.version>
        servlet-api.version>2.5servlet-api.version>
        dubbo.version>2.6.4dubbo.version>
        zookeeper.version>3.4.10zookeeper.version>
        curator-recipes.version>4.0.1curator-recipes.version>
        mybatis.version>3.2.8mybatis.version>
        mybatis.spring.version>1.2.2mybatis.spring.version>
        mysql.version>5.1.32mysql.version>
        druid.version>1.0.9druid.version>
        commons-fileupload.version>1.3.1commons-fileupload.version>
        freemarker.version>2.3.23freemarker.version>
        activemq.version>5.11.2activemq.version>
        security.version>3.2.3.RELEASEsecurity.version>
        solrj.version>4.10.3solrj.version>
        ik.version>2012_u6ik.version>
    properties>

    dependencyManagement>
        dependencies>
        
            
            dependency>
                groupId>org.springframeworkgroupId>
                artifactId>spring-contextartifactId>
                version>${spring.version}version>
            dependency>
            dependency>
                groupId>org.springframeworkgroupId>
                artifactId>spring-beansartifactId>
                version>${spring.version}version>
            dependency>
            dependency>
                groupId>org.springframeworkgroupId>
                artifactId>spring-webmvcartifactId>
                version>${spring.version}version>
            dependency>
            dependency>
                groupId>org.springframeworkgroupId>
                artifactId>spring-jdbcartifactId>
                version>${spring.version}version>
            dependency>
            dependency>
                groupId>org.springframeworkgroupId>
                artifactId>spring-aspectsartifactId>
                version>${spring.version}version>
            dependency>
            dependency>
                groupId>org.springframeworkgroupId>
                artifactId>spring-jmsartifactId>
                version>${spring.version}version>
            dependency>
            dependency>
                groupId>org.springframeworkgroupId>
                artifactId>spring-context-supportartifactId>
                version>${spring.version}version>
            dependency>
            dependency>
                groupId>org.springframeworkgroupId>
                artifactId>spring-testartifactId>
                version>${spring.version}version>
            dependency>
            dependency>
                groupId>org.springframeworkgroupId>
                artifactId>spring-webartifactId>
                version>${spring.version}version>
            dependency>
            
            dependency>
                groupId>com.alibabagroupId>
                artifactId>dubboartifactId>
                version>${dubbo.version}version>
            dependency>
            dependency>
                groupId>org.apache.zookeepergroupId>
                artifactId>zookeeperartifactId>
                version>${zookeeper.version}version>
            dependency>
            dependency>
                groupId>org.apache.curatorgroupId>
                artifactId>curator-recipesartifactId>
                version>${curator-recipes.version}version>
            dependency>
            dependency>
                groupId>junitgroupId>
                artifactId>junitartifactId>
                version>4.12version>
            dependency>
            dependency>
                groupId>com.alibabagroupId>
                artifactId>fastjsonartifactId>
                version>1.2.28version>
            dependency>
            dependency>
                groupId>javassistgroupId>
                artifactId>javassistartifactId>
                version>3.11.0.GAversion>
            dependency>
            dependency>
                groupId>commons-codecgroupId>
                artifactId>commons-codecartifactId>
                version>1.10version>
            dependency>
            dependency>
                groupId>javax.servletgroupId>
                artifactId>servlet-apiartifactId>
                version>2.5version>
                scope>providedscope>
            dependency>
            dependency>
                groupId>com.github.pagehelpergroupId>
                artifactId>pagehelperartifactId>
                version>${pagehelper.version}version>
            dependency>
            
            dependency>
                groupId>org.mybatisgroupId>
                artifactId>mybatisartifactId>
                version>${mybatis.version}version>
            dependency>
            dependency>
                groupId>org.mybatisgroupId>
                artifactId>mybatis-springartifactId>
                version>${mybatis.spring.version}version>
            dependency>
            
            dependency>
                groupId>mysqlgroupId>
                artifactId>mysql-connector-javaartifactId>
                version>${mysql.version}version>
            dependency>
            
            dependency>
                groupId>com.alibabagroupId>
                artifactId>druidartifactId>
                version>${druid.version}version>
            dependency>
            dependency>
                groupId>org.csource.fastdfsgroupId>
                artifactId>fastdfsartifactId>
                version>1.2version>
            dependency>
            
            dependency>
                groupId>commons-fileuploadgroupId>
                artifactId>commons-fileuploadartifactId>
                version>${commons-fileupload.version}version>
            dependency>

            dependency>
                groupId>commons-iogroupId>
                artifactId>commons-ioartifactId>
                version>${commons.io.version}version>
            dependency>
            
            
            dependency>
                groupId>redis.clientsgroupId>
                artifactId>jedisartifactId>
                version>2.8.1version>
            dependency>
            dependency>
                groupId>org.springframework.datagroupId>
                artifactId>spring-data-redisartifactId>
                version>1.7.2.RELEASEversion>
            dependency>
            dependency>
                groupId>org.freemarkergroupId>
                artifactId>freemarkerartifactId>
                version>${freemarker.version}version>
            dependency>
            dependency>
                groupId>org.apache.activemqgroupId>
                artifactId>activemq-allartifactId>
                version>${activemq.version}version>
            dependency>
            
            dependency>
                groupId>org.springframework.securitygroupId>
                artifactId>spring-security-webartifactId>
                version>4.1.0.RELEASEversion>
            dependency>
            dependency>
                groupId>org.springframework.securitygroupId>
                artifactId>spring-security-configartifactId>
                version>4.1.0.RELEASEversion>
            dependency>
            dependency>
                groupId>com.github.pengglegroupId>
                artifactId>kaptchaartifactId>
                version>2.3.2version>
                exclusions>
                    exclusion>
                        groupId>javax.servletgroupId>
                        artifactId>javax.servlet-apiartifactId>
                    exclusion>
                exclusions>
            dependency>
            dependency>
                groupId>org.springframework.securitygroupId>
                artifactId>spring-security-casartifactId>
                version>4.1.0.RELEASEversion>
            dependency>
            dependency>
                groupId>org.jasig.cas.clientgroupId>
                artifactId>cas-client-coreartifactId>
                version>3.3.3version>
                
                exclusions>
                    exclusion>
                        groupId>org.slf4jgroupId>
                        artifactId>log4j-over-slf4jartifactId>
                    exclusion>
                exclusions>
            dependency>
            
            dependency>
                groupId>org.apache.solrgroupId>
                artifactId>solr-solrjartifactId>
                version>${solrj.version}version>
            dependency>
            dependency>
                groupId>com.janeluogroupId>
                artifactId>ikanalyzerartifactId>
                version>${ik.version}version>
            dependency>
            dependency>
                groupId>org.apache.httpcomponentsgroupId>
                artifactId>httpcoreartifactId>
                version>4.4.4version>
            dependency>
            dependency>
                groupId>org.apache.httpcomponentsgroupId>
                artifactId>httpclientartifactId>
                version>4.5.3version>
            dependency>
            dependency>
                groupId>dom4jgroupId>
                artifactId>dom4jartifactId>
                version>1.6.1version>
            dependency>
            dependency>
                groupId>xml-apisgroupId>
                artifactId>xml-apisartifactId>
                version>1.4.01version>
            dependency>
        dependencies>
    dependencyManagement>
project>

1.3.2 通用实体类模块

创建通用实体类模块 youlexuan_pojo

1.3.3 通用数据访问模块

1、创建通用数据访问模块 youlexuan_dao


2、添加依赖Mybatis和youlexuan_pojo


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">
    parent>
        artifactId>youlexuan_parentartifactId>
        groupId>com.zqlgroupId>
        version>1.0version>
    parent>
    modelVersion>4.0.0modelVersion>

    artifactId>youlexuan_daoartifactId>

    dependencies>
        
        dependency>
            groupId>org.mybatisgroupId>
            artifactId>mybatisartifactId>
        dependency>
        dependency>
            groupId>org.mybatisgroupId>
            artifactId>mybatis-springartifactId>
        dependency>
        dependency>
            groupId>com.github.pagehelpergroupId>
            artifactId>pagehelperartifactId>
        dependency>
        
        dependency>
            groupId>mysqlgroupId>
            artifactId>mysql-connector-javaartifactId>
        dependency>
        
        dependency>
            groupId>com.alibabagroupId>
            artifactId>druidartifactId>
        dependency>
        dependency>
            groupId>com.zqlgroupId>
            artifactId>youlexuan_pojoartifactId>
            version>1.0version>
        dependency>
    dependencies>

project>

3、拷贝 数据访问层配置文件

1.3.4 通用工具类模块

创建通用工具类模块 youlexuan_common

1.3.5 商家商品服务接口模块

创建maven(jar)模块 youlexuan_sellergoods_interface

pom.xml添加依赖

dependencies>
    dependency>
        groupId>com.zqlgroupId>
        artifactId>youlexuan_daoartifactId>
        version>1.0version>
    dependency>
dependencies>

1.3.6 商家商品服务模块

1、创建maven(war)模块 youlexuan_sellergoods_service


2、pom.xml引入依赖 (注:要加 war)


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">
    parent>
        artifactId>youlexuan_parentartifactId>
        groupId>com.zqlgroupId>
        version>1.0version>
    parent>
    modelVersion>4.0.0modelVersion>

    artifactId>youlexuan_sellergoods_serviceartifactId>

    packaging>warpackaging>
    properties>
        webVersion>3.0webVersion>
    properties>
    dependencies>
        
        dependency>
            groupId>org.springframeworkgroupId>
            artifactId>spring-contextartifactId>
        dependency>
        dependency>
            groupId>org.springframeworkgroupId>
            artifactId>spring-beansartifactId>
        dependency>
        dependency>
            groupId>org.springframeworkgroupId>
            artifactId>spring-webmvcartifactId>
        dependency>
        dependency>
            groupId>org.springframeworkgroupId>
            artifactId>spring-jdbcartifactId>
        dependency>
        dependency>
            groupId>org.springframeworkgroupId>
            artifactId>spring-aspectsartifactId>
        dependency>
        dependency>
            groupId>org.springframeworkgroupId>
            artifactId>spring-jmsartifactId>
        dependency>
        dependency>
            groupId>org.springframeworkgroupId>
            artifactId>spring-context-supportartifactId>
        dependency>
        dependency>
            groupId>org.springframeworkgroupId>
            artifactId>spring-testartifactId>
        dependency>
        
        dependency>
            groupId>com.alibabagroupId>
            artifactId>dubboartifactId>
        dependency>
        dependency>
            groupId>org.apache.zookeepergroupId>
            artifactId>zookeeperartifactId>
        dependency>
        dependency>
            groupId>org.apache.curatorgroupId>
            artifactId>curator-recipesartifactId>
        dependency>
        dependency>
            groupId>junitgroupId>
            artifactId>junitartifactId>
        dependency>
        dependency>
            groupId>com.alibabagroupId>
            artifactId>fastjsonartifactId>
        dependency>
        dependency>
            groupId>javassistgroupId>
            artifactId>javassistartifactId>
        dependency>
        dependency>
            groupId>commons-codecgroupId>
            artifactId>commons-codecartifactId>
        dependency>
        dependency>
            groupId>javax.servletgroupId>
            artifactId>servlet-apiartifactId>
            scope>providedscope>
        dependency>
        dependency>
            groupId>com.zqlgroupId>
            artifactId>youlexuan_commonartifactId>
            version>1.0version>
        dependency>
        dependency>
            groupId>com.zqlgroupId>
            artifactId>youlexuan_daoartifactId>
            version>1.0version>
        dependency>
        dependency>
            groupId>com.zqlgroupId>
            artifactId>youlexuan_sellergoods_interfaceartifactId>
            version>1.0version>
        dependency>
    dependencies>
    build>
        plugins>
            
            plugin>
                groupId>org.apache.tomcat.mavengroupId>
                artifactId>tomcat7-maven-pluginartifactId>
                configuration>
                    path>/path>
                    port>9001port>
                configuration>
            plugin>
        plugins>
    build>
project>

3、创建spring配置文件

创建包 com.zql.sellergoods.service.impl

在src/main/resources下创建 spring/applicationContext_service.xml


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="youlexuan_sellergoods_service"/>
    dubbo:registry address="zookeeper://192.168.188.180:2181"/>
    dubbo:annotation package="com.zql.sellergoods.service.impl" />
beans>

4、创建web.xml

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


web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

    
    context-param>
        param-name>contextConfigLocationparam-name>
        param-value>classpath*:spring/applicationContext_*.xmlparam-value>
    context-param>
    listener>
        listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
    listener>
web-app>

注意:加载spring配置文件必须增加classpath*:spring/applicationContext*.xml
要不然加载数据库驱动会保存。

1.3.7 运营商管理后台

创建maven(war)模块 youlexuan_manager_web


1、pom.xml引入依赖 (注:要加 war)


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">
    parent>
        artifactId>youlexuan_parentartifactId>
        groupId>com.zqlgroupId>
        version>1.0version>
    parent>
    modelVersion>4.0.0modelVersion>

    artifactId>youlexuan_manager_webartifactId>
    
    packaging>warpackaging>
    properties>
        webVersion>3.0webVersion>
    properties>
    dependencies>
        
        dependency>
            groupId>org.springframeworkgroupId>
            artifactId>spring-contextartifactId>
        dependency>
        dependency>
            groupId>org.springframeworkgroupId>
            artifactId>spring-beansartifactId>
        dependency>
        dependency>
            groupId>org.springframeworkgroupId>
            artifactId>spring-webmvcartifactId>
        dependency>
        dependency>
            groupId>org.springframeworkgroupId>
            artifactId>spring-jdbcartifactId>
        dependency>
        dependency>
            groupId>org.springframeworkgroupId>
            artifactId>spring-aspectsartifactId>
        dependency>
        dependency>
            groupId>org.springframeworkgroupId>
            artifactId>spring-jmsartifactId>
        dependency>
        dependency>
            groupId>org.springframeworkgroupId>
            artifactId>spring-context-supportartifactId>
        dependency>
        dependency>
            groupId>org.springframeworkgroupId>
            artifactId>spring-testartifactId>
        dependency>
        
        dependency>
            groupId>com.alibabagroupId>
            artifactId>dubboartifactId>
        dependency>
        dependency>
            groupId>org.apache.zookeepergroupId>
            artifactId>zookeeperartifactId>
        dependency>
        dependency>
            groupId>org.apache.curatorgroupId>
            artifactId>curator-recipesartifactId>
        dependency>
        dependency>
            groupId>junitgroupId>
            artifactId>junitartifactId>
        dependency>
        dependency>
            groupId>com.alibabagroupId>
            artifactId>fastjsonartifactId>
        dependency>
        dependency>
            groupId>javassistgroupId>
            artifactId>javassistartifactId>
        dependency>
        dependency>
            groupId>commons-codecgroupId>
            artifactId>commons-codecartifactId>
        dependency>
        dependency>
            groupId>javax.servletgroupId>
            artifactId>servlet-apiartifactId>
            scope>providedscope>
        dependency>
        dependency>
            groupId>com.zqlgroupId>
            artifactId>youlexuan_commonartifactId>
            version>1.0version>
        dependency>
        dependency>
            groupId>com.zqlgroupId>
            artifactId>youlexuan_sellergoods_interfaceartifactId>
            version>1.0version>
        dependency>
    dependencies>
    build>
        plugins>
            
            plugin>
                groupId>org.apache.tomcat.mavengroupId>
                artifactId>tomcat7-maven-pluginartifactId>
                configuration>
                    path>/path>
                    port>9101port>
                configuration>
            plugin>
        plugins>
    build>

project>

2、创建 springmvc 配置文件

创建包 com.zql.sellergoods.controller

在src/main/resources下创建 spring/springmvc.xml


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:mvc="http://www.springframework.org/schema/mvc"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
		http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd
		http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    context:component-scan base-package="com.zql.sellergoods.controller"/>

    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>WriteMapNullValuevalue>
                        value>WriteDateUseDateFormatvalue>
                    array>
                property>
            bean>
        mvc:message-converters>
    mvc:annotation-driven>
    
    dubbo:application name="youlexuan_manager_web" />
    dubbo:registry address="zookeeper://192.168.188.180:2181"/>
    dubbo:annotation package="com.zql.sellergoods.controller" />
beans>

注意配置:fastjSON处理json

3、创建web.xml

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


web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

    servlet>
        servlet-name>springmvcservlet-name>
        servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
        init-param>
            param-name>contextConfigLocationparam-name>
            param-value>classpath:spring/springmvc.xmlparam-value>
        init-param>
    servlet>
    servlet-mapping>
        servlet-name>springmvcservlet-name>
        url-pattern>*.dourl-pattern>
    servlet-mapping>
    context-param>
        param-name>contextConfigLocationparam-name>
        param-value>classpath:spring/spring-*.xmlparam-value>
    context-param>
    listener>
        listener-class>
            org.springframework.web.context.ContextLoaderListener
        listener-class>
    listener>
    filter>
        filter-name>characterEncodingFilterfilter-name>
        filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
        init-param>
            param-name>encodingparam-name>
            param-value>UTF-8param-value>
        init-param>
        init-param>
            param-name>forceEncodingparam-name>
            param-value>trueparam-value>
        init-param>
    filter>
    filter-mapping>
        filter-name>characterEncodingFilterfilter-name>
        url-pattern>/*url-pattern>
    filter-mapping>
web-app>

1.3.8 商家管理后台

1、构建web模块 youlexuan_shop_web


与运营商管理后台的构建方式类似。

2、修改Pom.xml (注:要加 war)


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">
    parent>
        artifactId>youlexuan_parentartifactId>
        groupId>com.zqlgroupId>
        version>1.0version>
    parent>
    modelVersion>4.0.0modelVersion>

    artifactId>youlexuan_shop_webartifactId>

    packaging>warpackaging>
    properties>
        webVersion>3.0webVersion>
    properties>
    dependencies>
        
        dependency>
            groupId>org.springframeworkgroupId>
            artifactId>spring-contextartifactId>
        dependency>
        dependency>
            groupId>org.springframeworkgroupId>
            artifactId>spring-beansartifactId>
        dependency>
        dependency>
            groupId>org.springframeworkgroupId>
            artifactId>spring-webmvcartifactId>
        dependency>
        dependency>
            groupId>org.springframeworkgroupId>
            artifactId>spring-jdbcartifactId>
        dependency>
        dependency>
            groupId>org.springframeworkgroupId>
            artifactId>spring-aspectsartifactId>
        dependency>
        dependency>
            groupId>org.springframeworkgroupId>
            artifactId>spring-jmsartifactId>
        dependency>
        dependency>
            groupId>org.springframeworkgroupId>
            artifactId>spring-context-supportartifactId>
        dependency>
        dependency>
            groupId>org.springframeworkgroupId>
            artifactId>spring-testartifactId>
        dependency>
        
        dependency>
            groupId>com.alibabagroupId>
            artifactId>dubboartifactId>
        dependency>
        dependency>
            groupId>org.apache.zookeepergroupId>
            artifactId>zookeeperartifactId>
        dependency>
        dependency>
            groupId>org.apache.curatorgroupId>
            artifactId>curator-recipesartifactId>
        dependency>
        dependency>
            groupId>junitgroupId>
            artifactId>junitartifactId>
        dependency>
        dependency>
            groupId>com.alibabagroupId>
            artifactId>fastjsonartifactId>
        dependency>
        dependency>
            groupId>javassistgroupId>
            artifactId>javassistartifactId>
        dependency>
        dependency>
            groupId>commons-codecgroupId>
            artifactId>commons-codecartifactId>
        dependency>
        dependency>
            groupId>javax.servletgroupId>
            artifactId>servlet-apiartifactId>
            scope>providedscope>
        dependency>
        dependency>
            groupId>com.zqlgroupId>
            artifactId>youlexuan_commonartifactId>
            version>1.0version>
        dependency>
        dependency>
            groupId>com.zqlgroupId>
            artifactId>youlexuan_sellergoods_interfaceartifactId>
            version>1.0version>
        dependency>
    dependencies>
    build>
        plugins>
            
            plugin>
                groupId>org.apache.tomcat.mavengroupId>
                artifactId>tomcat7-maven-pluginartifactId>
                configuration>
                    path>/path>
                    port>9102port>
                configuration>
            plugin>
        plugins>
    build>

project>

注意:定义tomcat的启动端口为9102

3、创建springmvc配置文件

在src/main下创建包:com.zql.shop.controller

在src/main/resources下创建 spring/springmvc.xml


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">

    context:component-scan base-package="com.zql.shop.controller"/>
    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>WriteMapNullValuevalue>
                        value>WriteDateUseDateFormatvalue>
                    array>
                property>
            bean>
        mvc:message-converters>
    mvc:annotation-driven>
    
    dubbo:application name="youlexuan_shop_web" />
    dubbo:registry address="zookeeper://192.168.188.180:2181"/>
    dubbo:annotation package="com.zql.shop.controller" />
beans>

4、创建web.xml

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


web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

    servlet>
        servlet-name>springmvcservlet-name>
        servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
        init-param>
            param-name>contextConfigLocationparam-name>
            param-value>classpath:spring/springmvc.xmlparam-value>
        init-param>
    servlet>
    servlet-mapping>
        servlet-name>springmvcservlet-name>
        url-pattern>*.dourl-pattern>
    servlet-mapping>
    context-param>
        param-name>contextConfigLocationparam-name>
        param-value>classpath:spring/spring-*.xmlparam-value>
    context-param>
    listener>
        listener-class>
            org.springframework.web.context.ContextLoaderListener
        listener-class>
    listener>
    filter>
        filter-name>characterEncodingFilterfilter-name>
        filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
        init-param>
            param-name>encodingparam-name>
            param-value>UTF-8param-value>
        init-param>
        init-param>
            param-name>forceEncodingparam-name>
            param-value>trueparam-value>
        init-param>
    filter>
    filter-mapping>
        filter-name>characterEncodingFilterfilter-name>
        url-pattern>/*url-pattern>
    filter-mapping>
web-app>

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

1.4.1 生成代码

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

(1) 导入IDEA 进行自己对应的配置

(2) 一键生成

1.4.2 拷贝代码

  • 将com.zql.pojo包拷贝到pojo工程
  • 将com.zql.mapper 包和 resouce 下的 com.zql.mapper 文件夹拷贝到 dao 工程(拷贝时确认连同包路径一起拷贝,可能出现包路径作为资源文件路径情况,出现错误绑定失败错误)

(3) 生成 优乐选 代码 👉🏾👉🏾点我

copy 如下位置 👇🏾👇🏾

1.4.3 修改实体类代码

修改每个实体类,让其实现 Serializable 接口(次要,因为使用的都实现了Serializable 接口)

二、品牌列表-后端代码

2.1 需求分析

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

5.2数据库表

tb_brand 品牌表

字段 类型 长度 含义
id Bigint 主键
name Varchar 255 品牌名称
first_char Varchar 1 品牌首字母

2.2 后端代码

2.2.1 服务层接口

在 youlexuan_sellergoods_interface 工程创建 BrandService 接口

包名 :com.zql.sellergoods.service

package com.zql.sellergoods.service;

import com.zql.pojo.TbBrand;

import java.util.List;

/**
 * 品牌服务层接口
 * @Author:Daniel
 * @Version 1.0
 */
public interface BrandService {

    /**
     * 返回全部列表
     * @return
     */
    public ListTbBrand> findAll();
}

2.2.2 服务实现类

在 youlexuan_sellergoods_service 工程创建 BrandServiceImpl 类

包名 com.zql.sellergoods.service.impl (应该搭建框架的时候创建了包名)

package com.zql.sellergoods.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.zql.mapper.TbBrandMapper;
import com.zql.pojo.TbBrand;
import com.zql.sellergoods.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @Author:Daniel
 * @Version 1.0
 */

@Service  //dubbo
public class BrandServiceImpl implements BrandService {
    
    @Autowired
    private TbBrandMapper brandMapper;
    @Override
    public ListTbBrand> findAll() {
        
        return brandMapper.selectByExample(null);
    }
}

2.2.3 控制层代码

在 youlexuan_manager_web 工程创建 com.zql.sellergoods.controller 包,包下创建BrandController类 (应该搭建框架的时候创建了包名)

package com.zql.sellergoods.controller;

import com.zql.pojo.TbBrand;
import com.zql.sellergoods.service.BrandService;
import jdk.nashorn.internal.ir.annotations.Reference;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 品牌controller
 * @Author:Daniel
 * @Version 1.0
 */
@RestController   //可点击看源码了解哈哈
@RequestMapping("/brand")
public class BrandController {

    @Reference  //dubbo
    private BrandService  brandService;
    @RequestMapping("/findAll")
    public ListTbBrand> findAll(){

        return brandService.findAll();
    }
}

2.3 测试

  1. 整体安装哈哈

  1. 启动 youlexuan_sellergoods_service

  1. 启动youlexuan_manager_web

(1) 地址栏输入 http://192.168.188.180:8080/dubbo 查看是否有服务提供者

(2) 地址栏输入 http://localhost:9101/brand/findAll.do

报错解决👉👉 十一、org.apache.ibatis.binding.BindingException…

(3)再次查看是否有消费者 👇🏾👇🏾

2.4 快捷启动 tomcat

(1) 快捷启动 youlexuan_sellergoods_service

(2) 快捷启动 youlexuan_manager_web 同(1)

OK 项目架构起初成功,可以看到浏览器输出了 json 数据。 ✌✌

本节源码

服务器托管,北京服务器托管,服务器租用 http://www.hhisp.net

hackdl

咨询热线/微信 13051898268