day69_淘淘商城項目_02_dubbo介紹 + dubbo框架整合 + zookeeper + 商品列表查詢實現 + 分頁 + 逆向工程_匠心筆記

  • 課程計劃
    • 次日:商品列表功能實現
  • 一、功能分析
  • 二、服務中間件Dubbo
  • 三、SSM框架整合+Zookeeper
  • 四、Dubbo的Admin管理平臺
  • 五、後臺商品列表查詢功能實現

一、功能分析

1.一、後臺系統所用的技術

  • 框架:Spring + SpringMVC + Mybatis + Dubbo
  • 前端:EasyUI
  • 數據庫:MySQL

1.二、建立數據庫

  • 一、安裝mysql數據庫,要求5.5以上版本。
  • 二、在mysql中建立一個taotao數據庫。
  • 三、將建立數據庫的腳本taotao.sql導入到taotao中。

1.三、Mybatis逆向工程

  • 使用mybatis官方提供的mybatis-generator生成pojo、mapper接口及映射文件。
  • 而且將pojo放到toatao-manager-pojo工程中。
  • 將mapper接口及映射文件放到taotao-manager-dao工程中。
    • 注意1:由於涉及到各個工程(系統)之間來回傳遞對象,因此使用時須要對涉及到的POJO實現序列化接口。
      • 咱們發現mybatis的插件,有一個能夠自動給全部pojo實現序列化接口(example除外)
    • 注意2:若是生成的pojo可以有toString()的重寫會給開發、調試帶來很大的方便,因此咱們也將生成的pojo重寫toString()方法。
      • 一樣使用mybatis的插件。
    • 即咱們在使用Mybatis逆向工程的時候,在其配置文件generatorConfig.xml中配置下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
  PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
  "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">


<generatorConfiguration>
    <context id="testTables" targetRuntime="MyBatis3">
        <!-- 配置生成pojo的序列化的插件,mybatis支持不少插件,這些插件都在 org.mybatis.generator.plugins包下  -->
        <plugin type="org.mybatis.generator.plugins.SerializablePlugin" />

        <!--配置生成pojo的toString()方法的插件,mybatis支持不少插件,這些插件都在 org.mybatis.generator.plugins包下 -->
        <plugin type="org.mybatis.generator.plugins.ToStringPlugin" />

        <commentGenerator>
            <!-- 是否去除自動生成的註釋 true:是 : false:否 -->
            <property name="suppressAllComments" value="true" />
        </commentGenerator>
        <!--數據庫鏈接的信息:驅動類、鏈接地址、用戶名、密碼 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
            connectionURL="jdbc:mysql://localhost:3306/taotao" userId="root" password="root">

        </jdbcConnection>
        <!-- 默認false時,把JDBC DECIMAL 和 NUMERIC 類型解析爲 Integer
              當爲 true時,把JDBC DECIMAL 和  NUMERIC 類型解析爲java.math.BigDecimal -->

        <javaTypeResolver>
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>

        <!-- targetProject:生成PO類的位置 -->
        <javaModelGenerator targetPackage="com.taotao.pojo" targetProject=".\src">
            <!-- enableSubPackages:是否讓schema做爲包的後綴 -->
            <property name="enableSubPackages" value="false" />
            <!-- 從數據庫返回的值被清理先後的空格 -->
            <property name="trimStrings" value="true" />
        </javaModelGenerator>
        <!-- targetProject:mapper映射文件生成的位置 -->
        <sqlMapGenerator targetPackage="com.taotao.mapper" targetProject=".\src">
            <!-- enableSubPackages:是否讓schema做爲包的後綴 -->
            <property name="enableSubPackages" value="false" />
        </sqlMapGenerator>
        <!-- targetPackage:mapper接口生成的位置 -->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.taotao.mapper" targetProject=".\src">
            <!-- enableSubPackages:是否讓schema做爲包的後綴 -->
            <property name="enableSubPackages" value="false" />
        </javaClientGenerator>
        <!-- 指定數據庫表 -->
        <table schema="" tableName="tb_content"></table>
        <table schema="" tableName="tb_content_category"></table>
        <table schema="" tableName="tb_item"></table>
        <table schema="" tableName="tb_item_cat"></table>
        <table schema="" tableName="tb_item_desc"></table>
        <table schema="" tableName="tb_item_param"></table>
        <table schema="" tableName="tb_item_param_item"></table>
        <table schema="" tableName="tb_order"></table>
        <table schema="" tableName="tb_order_item"></table>
        <table schema="" tableName="tb_order_shipping"></table>
        <table schema="" tableName="tb_user"></table>
    </context>
</generatorConfiguration>

使用方法:將Java工程generatorSqlmapCustom導入到eclipse中,運行/generatorSqlmapCustom/src/下的GeneratorSqlmap.java文件生成咱們所需的代碼。php

1.四、系統間通訊問題

1.4.一、分析

  因爲淘淘商城是基於soa的架構,表現層和服務層是不一樣的工程。因此要實現商品列表查詢須要兩個系統之間進行通訊。
  如何實現遠程通訊?css

  • 一、使用Webservice:效率不高,它是基於soap協議(html+xml)。項目中不推薦使用。優勢是:跨語言、跨平臺。適用於兩個公司之間。
  • 二、使用Restful形式的服務:http+json。不少項目中應用。若是服務愈來愈多,服務與服務之間的調用關係複雜,調用服務的URL管理複雜,何時添加機器難以肯定。 須要治療服務。適用於中小型企業。
  • 三、使用Dubbo:使用RPC協議進行遠程調用,直接使用socket通訊傳輸效率高,而且能夠統計出系統之間的調用關係、調用次數,管理服務。適用於大型企業。缺點:不能跨語言。只能java語言。

二、Dubbo

2.一、什麼是dubbo

  最新中文網址:https://dubbo.incubator.apache.org/zh-cn/index.html
  DUBBO是一個分佈式服務框架,致力於提供高性能和透明化的RPC遠程服務調用方案,是阿里巴巴SOA服務化治理方案的核心框架,天天爲2,000+個服務提供3,000,000,000+次訪問量支持,並被普遍應用於阿里巴巴集團的各成員站點。
  隨着互聯網的發展,網站應用的規模不斷擴大,常規的垂直應用架構已沒法應對,分佈式服務架構以及流動計算架構勢在必行,亟需一個治理系統確保架構有條不紊的演進。html

  • 單一應用架構
    當網站流量很小時,只需一個應用,將全部功能都部署在一塊兒,以減小部署節點和成本。
    此時,用於簡化增刪改查工做量的數據訪問框架(ORM)是關鍵。
  • 垂直應用架構
    當訪問量逐漸增大,單一應用增長機器帶來的加速度愈來愈小,將應用拆成互不相干的幾個應用,以提高效率。
    此時,用於加速前端頁面開發的Web框架(MVC)是關鍵。
  • 分佈式服務架構
    當垂直應用愈來愈多,應用之間交互不可避免,將核心業務抽取出來,做爲獨立的服務,逐漸造成穩定的服務中心,使前端應用能更快速的響應多變的市場需求。
    此時,用於提升業務複用及整合的分佈式服務框架(RPC)是關鍵。
  • 流動計算架構
    當服務愈來愈多,容量的評估,小服務資源的浪費等問題逐漸顯現,此時需增長一個調度中心基於訪問壓力實時管理集羣容量,提升集羣利用率。
    此時,用於提升機器利用率的資源調度和治理中心(SOA)是關鍵。
  • Dubbo就是資源調度和治理中心的管理工具
  • Dubbo 就是相似於webservice的關於系統之間通訊的框架,並能夠統計管理服務直接的調用狀況(包括服務被誰調用了,調用的次數是如何,以及服務的使用情況)。

2.二、Dubbo的架構

  • Apache Dubbo (incubating) |ˈdʌbəʊ| 是一款高性能、輕量級的開源Java RPC框架,它提供了三大核心能力:面向接口的遠程方法調用,智能容錯和負載均衡,以及服務自動註冊和發現。
  • 節點角色說明:
    • Provider: 暴露服務的服務提供方。
    • Consumer: 調用遠程服務的服務消費方。
    • Registry: 服務註冊與發現的註冊中心。
    • Monitor: 統計服務的調用次調和調用時間的監控中心。
    • Container: 服務運行容器。
  • 調用關係說明:
    • 0、服務容器負責啓動,加載,運行服務提供者。
    • 一、服務提供者在啓動時,向註冊中心註冊本身提供的服務。
    • 二、服務消費者在啓動時,向註冊中心訂閱本身所需的服務。
    • 三、註冊中心返回服務提供者地址列表給消費者,若是有變動,註冊中心將基於`長鏈接`推送變動數據給消費者。
    • 四、服務消費者,從提供者地址列表中,基於`軟負載均衡算法`,選一臺提供者進行調用,若是調用失敗,再選另外一臺調用。
    • 五、服務消費者和提供者,在內存中累計調用次數和調用時間,定時每分鐘發送一次`統計`數據到監控中心。

2.三、Dubbo的使用方法

  • spring配置
    • Dubbo採用全Spring配置方式,透明化接入應用,對應用沒有任何API侵入,只需用Spring加載Dubbo的配置便可,Dubbo基於Spring的Schema擴展進行加載。
  • 單一工程中spring的配置local.xml(沒有使用dubbo時):
    <bean id="xxxService" class="com.xxx.XxxServiceImpl" />
    <bean id="xxxAction" class="com.xxx.XxxAction">
        <property name="xxxService" ref="xxxService" />
    </bean>
  • 遠程服務(使用dubbo時):
    在本地服務的基礎上,只需作簡單配置,便可完成遠程化:
    將上面的local.xml配置拆分紅兩份,將服務定義部分放在服務提供方remote-provider.xml,將服務引用部分放在服務消費方remote-consumer.xml。
    並在服務提供方增長暴露服務配置,在服務消費方增長引用服務配置

服務層發佈服務:前端

    <!-- 和本地服務同樣實現遠程服務 -->
    <bean id="xxxService" class="com.xxx.XxxServiceImpl" />
    <!-- 增長暴露遠程服務配置 -->
    <dubbo:service interface="com.xxx.XxxService" ref="xxxService" />

表現層調用服務:java

    <!-- 增長引用遠程服務配置 -->
    <dubbo:reference id="xxxService" interface="com.xxx.XxxService" />
    <!-- 和本地服務同樣使用遠程服務 -->
    <bean id="xxxAction" class="com.xxx.XxxAction">
        <property name="xxxService" ref="xxxService" />
    </bean>

2.四、註冊中心--zookeeper

2.4.一、zookeeper的介紹

  Zookeeper(集羣管理工具:動物管理員)適用於大數據裏面的生態系統。好比:大數據中的hadoop(大象)、Hive(蜂巢)等等動物須要動物管理員來管理哈!
  Dubbo中使用Zookeeper是做爲註冊中心使用的,註冊中心負責服務地址的註冊與查找,至關於目錄服務,服務提供者和消費者只在啓動時與註冊中心交互,註冊中心不轉發請求,壓力較小。使用dubbo-2.3.3以上版本,建議使用Zookeeper註冊中心。
  Zookeeper是Apacahe Hadoop的子項目,是一個樹型的目錄服務,支持變動推送,適合做爲Dubbo服務的註冊中心,工業強度較高(穩定性好),可用於生產環境,並推薦使用。mysql

2.4.二、環境準備

  • 虛擬機軟件環境:VMware 14 PRO
  • linux環境:CentOS 7.5
  • 環境我已經裝備好了,咱們使用VMWare打開虛擬機。

2.4.三、使用SecureCRT鏈接Linux系統

  • 清除全部文件,咱們先要有一個乾乾淨淨的Linux系統。以下圖:

2.4.四、zookeeper的安裝

  • 第一步:linux上安裝jdk,參考連接:https://www.cnblogs.com/chenmingjun/p/9931593.html
  • 第二步:上傳linux版本的zookeeper壓縮包至linux系統下的/user/local/zookeeper目錄下,解壓縮zookeeper壓縮包
  • 第三步:進入zookeeper解壓目錄,將conf文件夾下zoo_sample.cfg更名爲zoo.cfg,命令:mv zoo_sample.cfg zoo.cfg
  • 第四步:編輯zoo.cfg文件,修改配置dataDir屬性,指定一個真實目錄(進入zookeeper解壓目錄,建立data目錄:mkdir data)
  • 第五步:
    • 啓動zookeeper:bin/zkServer.sh start
    • 關閉zookeeper:bin/zkServer.sh stop
    • 查看zookeeper狀態:bin/zkServer.sh status
    • 注意:若是出現問題,須要關閉linux的防火牆。不然不建議關閉防火牆,而是配置防火牆。

三、SSM框架整合+Zookeeper

3.一、需求

  • 根據商品id查詢商品信息,並將商品信息使用json數據返回。
  • 分析:
    • 請求的url:`/item/{itemId}`  注意:(把參數放在了請求的裏面,使用Restfull軟件架構設計模式)
    • 參數:商品id,從請求的url中得到
    • 返回值:TbItem對象,逆向工程生成的pojo(響應json數據)

3.二、配置對dubbo的依賴

  • 加入dubbo相關的jar包。服務層、表現層都要配置。在服務層、表現層的pom.xml文件中添加依賴。

服務層:taotao-manager-service/pom.xmllinux

    <!-- 配置對dubbo的依賴 -->
    <!-- dubbo相關 -->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>dubbo</artifactId>
        <version>${dubbo.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.zookeeper</groupId>
        <artifactId>zookeeper</artifactId>
        <version>${zookeeper.version}</version>
    </dependency>
    <dependency>
        <groupId>com.github.sgroschupf</groupId>
        <artifactId>zkclient</artifactId>
        <version>${zkclient.version}</version>
    </dependency>

注意:當咱們配置對dubbo的依賴後,要查看dubbo的依賴關係,排除掉dubbo依賴的低版本的jar,好比:spring-2.5.6.SEC03.jar和netty-3.2.5.Final.jar(這一樣也是編程的好習慣)!
排除方法:
方法一:在taotao-manager-service/pom.xml中手動編寫代碼,因此最終pom.xml中要添加的內容以下:git

    <!-- 配置對dubbo的依賴 -->
    <!-- dubbo相關 -->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>dubbo</artifactId>
        <!-- 排除對低版本jar包的依賴 -->
        <exclusions>
            <exclusion>
                <artifactId>spring</artifactId>
                <groupId>org.springframework</groupId>
            </exclusion>
            <exclusion>
                <artifactId>netty</artifactId>
                <groupId>org.jboss.netty</groupId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.apache.zookeeper</groupId>
        <artifactId>zookeeper</artifactId>
    </dependency>
    <dependency>
        <groupId>com.github.sgroschupf</groupId>
        <artifactId>zkclient</artifactId>
    </dependency>

方法二:使用圖形化界面,以下圖所示:github


二種方式本質是同樣的!
表現層:taotao-manager-web/pom.xml
    <!-- 配置對dubbo的依賴 -->
    <!-- dubbo相關 -->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>dubbo</artifactId>
        <!-- 排除對低版本jar包的依賴 -->
        <exclusions>
            <exclusion>
                <artifactId>spring</artifactId>
                <groupId>org.springframework</groupId>
            </exclusion>
            <exclusion>
                <artifactId>netty</artifactId>
                <groupId>org.jboss.netty</groupId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.apache.zookeeper</groupId>
        <artifactId>zookeeper</artifactId>
    </dependency>
    <dependency>
        <groupId>com.github.sgroschupf</groupId>
        <artifactId>zkclient</artifactId>
    </dependency>

3.三、整合思路

  • 一、Dao層:
    mybatis整合spring,經過spring管理SqlSessionFactory、Mapper代理對象。須要mybatis和spring的整合包。由spring建立數據庫鏈接池。
    下圖所示爲文件所對應的工程:
  • 二、Service層:
    全部的service實現類都放到spring容器中管理,並由spring管理事務。發佈dubbo服務
    下圖所示爲文件所對應的工程:

    web.xml中:配置加載spring容器
  • 三、表現層:
    springmvc框架,由springmvc管理Controller。引用dubbo服務
    下圖所示爲文件所對應的工程:

    web.xml中:配置前端控制器,配置URL攔截形式。

3.四、Dao整合

  • 整合後的目錄結構以下:後面的配置文件能夠參考此圖片建立目錄(這樣的目錄結構賞心悅目啊!)

3.4.一、建立SqlMapConfig.xml配置文件

SqlMapConfig.xml(因爲和spring進行整合了,裏面空空如也,可是該文件必需要有!)web

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>

</configuration>

3.4.二、spring整合mybatis

建立applicationContext-dao.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-4.2.xsd
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-4.2.xsd 
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
    http://www.springframework.org/schema/util 
    http://www.springframework.org/schema/util/spring-util-4.2.xsd"
>

    <!-- 配置數據庫鏈接池 -->
    <!-- 加載配置文件 -->
    <context:property-placeholder location="classpath:properties/-*.properties" />
    <!-- 數據庫鏈接池 -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
        destroy-method="close">

        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
        <property name="driverClassName" value="${jdbc.driver}" />
        <property name="maxActive" value="10" />
        <property name="minIdle" value="5" />
    </bean> 
    <!-- 配置讓spring管理sqlsessionfactory,使用mybatis和spring整合包中的 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 數據庫鏈接池 -->
        <property name="dataSource" ref="dataSource" />
        <!-- 加載mybatis的全局配置文件 -->
        <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" />
    </bean> 
    <!-- 配置Mapper映射文件的包掃描器 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.taotao.mapper" />
    </bean>
</beans>

db.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/taotao?characterEncoding=utf-8
jdbc.username=root
jdbc.password=root

備註:
  Druid(德魯伊)是目前最好的數據庫鏈接池,在功能、性能、擴展性方面,都超過其餘數據庫鏈接池,包括DBCP、C3P0、BoneCP、Proxool、JBoss DataSource。
  Druid已經在阿里巴巴部署了超過600個應用,通過多年多生產環境大規模部署的嚴苛考驗。

3.五、Service整合

3.5.一、配置管理Service

applicationContext-service.xml

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

    <!-- 配置包掃描器,掃描全部須要帶@Service註解的類 -->
    <context:component-scan base-package="com.taotao.service" />

    <!-- 使用dubbo發佈服務:須要先引入dubbo的約束 -->
    <!-- 提供方應用信息,用於計算依賴關係 -->
    <dubbo:application name="taotao-manager" />
    <dubbo:registry protocol="zookeeper" address="192.168.5.130:2181" />
    <!-- 用dubbo協議在20880端口暴露服務 -->
    <dubbo:protocol name="dubbo" port="20880" />
    <!-- 聲明須要暴露的服務接口 -->
    <dubbo:service interface="com.taotao.service.ItemService" ref="itemServiceImpl" />
</beans>

3.5.二、配置事務管理

建立applicationContext-transaction.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-4.2.xsd
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-4.2.xsd 
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
    http://www.springframework.org/schema/util 
    http://www.springframework.org/schema/util/spring-util-4.2.xsd"
>


    <!-- 配置事務管理器 :包括:通知、切面-->
    <!-- 事務管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!-- 數據源 -->
        <property name="dataSource" ref="dataSource" />
    </bean>
    <!-- 通知 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <!-- 傳播行爲 -->
            <!-- 增刪改操做須要配置事務 -->
            <tx:method name="save*" propagation="REQUIRED" />
            <tx:method name="insert*" propagation="REQUIRED" />
            <tx:method name="add*" propagation="REQUIRED" />
            <tx:method name="create*" propagation="REQUIRED" />
            <tx:method name="delete*" propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED" />
            <!-- 查詢操做不須要配置事務 -->
            <tx:method name="find*" propagation="SUPPORTS" read-only="true" />
            <tx:method name="select*" propagation="SUPPORTS" read-only="true" />
            <tx:method name="get*" propagation="SUPPORTS" read-only="true" />
        </tx:attributes>
    </tx:advice>
    <!-- 切面 -->
    <aop:config>
        <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.taotao.service.*.*(..))" />
    </aop:config>
</beans>

3.5.三、配置web.xml

因爲taotao-manager-service是一個聚合工程中的web工程,因此咱們須要配置下該工程的web.xml
web.xml

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

    id="WebApp_ID" version="2.5">

    <display-name>taotao-manage-servicer</display-name>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <!-- 初始化spring容器:也即加載spring容器,taotao-manager-service是web工程,須要在web容器中運行 -->
    <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>

3.5.四、spring父子容器的關係

  • 思考:以上咱們的服務層已經配置好了,在表現層的springmvc咱們還須要初始化spring容器嗎?即springmvc能夠初始化一個容器嗎?亦即咱們還須要寫上面web.xml中的那段代碼嗎?
    答:在表現層的springmvc咱們不須要初始化spring容器了。爲何呢?緣由以下圖所示:

    訪問規則:子容器能夠訪問父容器的對象,父容器不能夠訪問子容器的對象。
    即:spring父容器裏面的對象能夠注入spring子容器中,而spring子容器中的對象不能夠注入spring父容器中。

3.六、表現層整合

3.6.一、配置管理Controller

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-4.2.xsd
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc-4.2.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-4.2.xsd"
>

    <!-- 配置包掃描器,掃描全部須要帶@Controller註解的類 -->
    <context:component-scan base-package="com.taotao.controller" />
    <!-- 配置註解驅動 -->
    <mvc:annotation-driven />
    <!-- 配置視圖解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <!-- 引用dubbo服務 :須要先引入dubbo的約束-->
    <dubbo:application name="taotao-manager-web"/>
    <dubbo:registry protocol="zookeeper" address="192.168.5.130:2181"/> 
    <dubbo:reference interface="com.taotao.service.ItemService" id="itemService" />
</beans>

注意:若是沒有添加interface的依賴,還須要在表現層工程的pom文件中添加taotao-manager-interface工程的依賴。
添加的內容以下:

    <!-- 配置對interface的依賴:表現層調用服務要經過該接口 -->
    <dependency>
        <groupId>com.taotao</groupId>
        <artifactId>taotao-manager-interface</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>

3.6.二、配置web.xml

web.xml

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

    id="WebApp_ID" version="2.5">

    <display-name>taotao-manager-web</display-name>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <!-- 配置解決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>
    </filter>
    <filter-mapping>
        <filter-name>characterEncodingFilter</filter-name>
        <!-- /表示攔截全部請求,但不攔截jsp,/*表示攔截全部請求 -->
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <!-- 配置springmvc的前端控制器 -->
    <servlet>
        <servlet-name>taotao-manager-web</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- contextConfigLocation不是必須的, 若是不配置contextConfigLocation, 
             springmvc的配置文件默認在:WEB-INF/servlet的name+"-servlet.xml" -->

        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring/springmvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup><!-- 1表示tomcat啓動以後就會加載servlet前端控制器,不然表示第一次請求的時候才加載 -->>
    </servlet>
    <servlet-mapping>
        <servlet-name>taotao-manager-web</servlet-name>
        <!-- /表示攔截全部請求,但不攔截jsp,/*表示攔截全部請求 -->
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

注意:其中配置項<load-on-startup>1</load-on-startup>表示tomcat啓動以後就會加載這個servlet前端控制器,若是不配置的話,就表示第一次請求的時候才加載這個servlet前端控制器。

3.6.三、編寫Controller

@Controller
public class ItemController {

    @Autowired
    private ItemService itemService;

    /**
     * 根據商品id查詢商品信息,並將商品信息使用json數據格式返回。
     * @param itemId
     * @return
     */

    @RequestMapping("/item/{itemId}")
    @ResponseBody // 在後臺,把JavaBean強制轉換成json格式數據返回給前臺頁面。
    public TbItem getItemById(@PathVariable Long itemId) {
        TbItem item = itemService.getItemById(itemId);
        return item;
    }
}

注意:使用註解@ResponseBody可能會出現訪問406錯誤,90%的狀況下,是因爲沒有把jackson註解的jar包沒有引入。
  @ResponseBody的做用:在後臺,把JavaBean強制轉換成json格式數據返回給前臺頁面。沒有該jar,就不能把對象轉成一個json格式的數據(json串)。服務器會出現406錯誤。
  本工程taotao-manager-web中有jackson的jar依賴,由於工程taotao-manager-web依賴工程taotao-common了,而在工程taotao-common依賴了jackson的jar包。

3.七、dubbo服務調用測試

3.7.一、添加日誌文件log4j.properties

  • 測試以前咱們先將log4j的日誌配置文件log4j.properties拷貝至要啓動工程的classpath目錄下,以下圖所示:

    這樣工程啓動報錯就能輸出錯誤信息,便於咱們解決問題!

3.7.二、啓動工程

  • 注意:使用tomcat插件要先啓動服務層工程taotao-manager,再啓動表現層工程taotao-manager-web。
  • 控制檯會報錯以下圖:

    是因爲目標主機沒法訪問,在cmd中都沒法ping通,多是遠程主機沒有啓動,咱們啓動遠程主機,啓動zookeeper服務,問題仍舊在,爲何呢?忽然想到了,遠程主機的防火牆沒有關閉,把端口2181屏蔽掉了。因爲本博主使用的CentOS 7.5,防火牆使用的是firewalld,咱們使用命令的方式來添加端口(修改後須要重啓firewalld服務),操做以下:
  [root@itheima ~]# cd /etc/firewalld/zones/
  [root@itheima zones]# firewall-cmd --permanent --add-port=2181/tcp 
  [root@itheima zones]# service firewalld restart
  Redirecting to /bin/systemctl restart firewalld.service
  [root@itheima zones]
  • 再次啓動服務層工程taotao-manager,啓動成功,沒有問題。
  • 而後咱們啓動表現層工程taotao-manager-web

3.7.三、測試訪問

  • 測試訪問地址:localhost:8081/item/830972
    測試報錯以下圖所示:

    緣由是:映射文件並無拷貝到classpath下致使。
  • 解決mapper映射文件不發佈的問題:
    在taotao-manager-dao工程中的pom文件中添加以下內容:
    <build>
        <resources>
            <!-- 若是不添加此節點,mybatis的mapper.xml文件都會被漏掉 -->
            <!-- 注意:配置了此方式,原來的默認的資源拷貝行爲將無效了 -->
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            <!-- 若是不添加此節點,src/main/resources目錄下的配置文件將被忽略 -->
            <resource>
                <directory>src/main/resource</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
    </build>

taotao-manager-dao咱們在表現層也會用到,因此改好後須要選擇taotao-manager-dao工程進行安裝,選中工程右鍵 Run as --> Maven install
注意:因爲taotao-manager是一個聚合工程,taotao-manager-dao是該聚合工程中的模塊工程,因此taotao-manager-dao中有修改的話,taotao-manager-dao也不須要安裝,因此咱們直接重啓工程taotao-manager便可,命令:clean tomcat7:run
再次測試訪問:localhost:8081/item/830972,出現了新的錯誤,以下:


緣由是:由於涉及到各個工程(系統)之間來回傳遞對象,因此使用時須要對涉及到的POJO實現序列化接口。
  解決方式一:因此咱們須要修pojo類,實現序列化接口。
  解決方式二:咱們發現mybatis的插件,有一個能夠自動給全部pojo實現序列化接口和序列化版本id(example除外)。即咱們在使用Mybatis逆向工程的時候,在其配置文件generatorConfig.xml中配置下:
    <!-- 配置pojo的序列化 -->
    <plugin type="org.mybatis.generator.plugins.SerializablePlugin" />

修改完成後,因爲咱們在表現層也會用到taotao-manager-pojo,因此咱們須要從新安裝下,選中工程右鍵 Run as --> Maven install,安裝完成後而後咱們重啓服務端工程和表現層工程。整合成功!
瀏覽器效果以下圖:
瀏覽器美化後的json格式數據:


瀏覽器原始的json格式數據:

四、Dubbo的Admin管理平臺

  • Dubbo監控中心其實就是一個web工程,目前推薦將Dubbo監控中心和註冊中心安裝在同一臺服務器上,這樣能夠不須要任何配置。

4.一、環境準備

  • 一、準備好war包
  • 二、準備好tomcat
  • 三、使用SecureCRT上傳到linux系統中
  • 四、須要安裝先tomcat
      即解壓tomcat,命令:tar -zxvf apache-tomcat-7.0.47.tar.gz
      而後刪除tomcat壓縮包,命令:rm -rf apache-tomcat-7.0.47.tar.gz

4.二、部署監控中心

  • 部署監控中心,拷貝dubbo-admin-2.5.4.war包到/usr/local/tomcat/apache-tomcat-7.0.47/webapps/下,並起個別名,名字太長了(下面演示本博主沒有起別名,以爲名字還能接受)
[root@itheima tomcat]# ll
總用量 26296
drwxr-xr-x. 9 root root      160 11月 10 15:04 apache-tomcat-7.0.47
-rw-r--r--. 1 root root 26924417 9月  26 22:11 dubbo-admin-2.5.4.war
[root@itheima tomcat]# cp dubbo-admin-2.5.4.war apache-tomcat-7.0.47/webapps/
[root@itheima tomcat]# rm -rf dubbo-admin-2.5.4.war 
[root@itheima tomcat]# ll apache-tomcat-7.0.47/webapps/
總用量 26304
drwxr-xr-x. 13 root root     4096 11月 10 15:04 docs
drwxr-xr-x.  8 root root      149 11月 10 15:18 dubbo-admin-2.5.4
-rw-r--r--.  1 root root 26924417 11月 10 15:18 dubbo-admin-2.5.4.war
drwxr-xr-x.  7 root root      111 11月 10 15:04 examples
drwxr-xr-x.  5 root root       87 11月 10 15:04 host-manager
drwxr-xr-x.  5 root root      103 11月 10 15:04 manager
drwxr-xr-x.  3 root root     4096 11月 10 15:04 ROOT
[root@itheima tomcat]
  • 啓動tomcat(注意:要先啓動註冊中心zookeeper)
[root@itheima tomcat]# cd apache-tomcat-7.0.47/bin/
[root@itheima bin]# ./startup.sh    --啓動tomcat
Using CATALINA_BASE:   /usr/local/tomcat/apache-tomcat-7.0.47
Using CATALINA_HOME:   /usr/local/tomcat/apache-tomcat-7.0.47
Using CATALINA_TMPDIR: /usr/local/tomcat/apache-tomcat-7.0.47/temp
Using JRE_HOME:        /usr/local/java/jdk1.7.0_80/jre
Using CLASSPATH:       /usr/local/tomcat/apache-tomcat-7.0.47/bin/bootstrap.jar:/usr/local/tomcat/apache-tomcat-7.0.47/bin/tomcat-juli.jar
[root@itheima bin]# ps -ef|grep tomcat  --查看Tomcat的日誌
root       4741      1 18 15:07 pts/1    00:00:03 /usr/local/java/jdk1.7.0_80/jre/bin/java -Djava.util.logging.config.file=/usr/local/tomcat/apache-tomcat-7.0.47/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.endorsed.dirs=/usr/local/tomcat/apache-tomcat-7.0.47/endorsed -classpath /usr/local/tomcat/apache-tomcat-7.0.47/bin/bootstrap.jar:/usr/local/tomcat/apache-tomcat-7.0.47/bintomcat-juli.jar -Dcatalina.base=/usr/local/tomcat/apache-tomcat-7.0.47 -Dcatalina.home=/usr/local/tomcat/apache-tomcat-7.0.47 -Djava.io.tmpdir=/usr/local/tomcat/apache-tomcat-7.0.47/temp org.apache.catalina.startup.Bootstrap start
root       4761   3081  0 15:07 pts/1    00:00:00 grep --color=auto tomcat
[root@itheima bin]# 

4.三、測試訪問

  • 測試訪問:http://192.168.5.130:8080/dubbo-admin-2.5.4/,瀏覽器彈出訪問鏈接超時
    緣由是:遠程主機的防火牆沒有關閉,把端口8080屏蔽掉了。因爲本博主使用的CentOS 7.5,防火牆使用的是firewalld,咱們使用命令的方式來添加端口(修改後須要重啓firewalld服務),操做以下:
[root@itheima ~]# cd /etc/firewalld/zones/
[root@itheima zones]# firewall-cmd --permanent --add-port=8080/tcp 
[root@itheima zones]# service firewalld restart
Redirecting to /bin/systemctl restart firewalld.service
[root@itheima zones]
  • 同理,增長端口8080、808一、20880、7070(dubbo監控使用端口)
  • 查看tomcat目錄下logs日誌命令:tail -f logs/catalina.out
  • 再次訪問::http://192.168.5.130:8080/dubbo-admin-2.5.4/,依然不行,tomcat主頁能夠訪問可是項目dubbo-admin-2.5.4沒法訪問,通過幾個小時的谷歌和百度並嘗試,發現了最終緣由:dubbo-admin不能在jdk1.8的環境下運行,mmp!因而我卸載了jdk1.8,從新安裝了jdk1.7。問題解決了!!!彈出了我想要的界面:
      用戶名:root
      密碼:root
  • 相關截圖以下:

    成功登陸界面以下:

    點擊【服務治理】 --> 【服務】,就能看見咱們發佈的服務了

    點擊【該服務】,能夠看到本地電腦的IP和端口號

    點擊【本地電腦的IP】,查看【服務提供者】的一些信息

    查看【服務消費者】的一些信息
    查看【應用】

    若是監控中心和註冊中心在同一臺服務器上,能夠不須要任何配置。
    若是不在同一臺服務器,須要修改配置文件:
    文件位置:/usr/local/tomcat/apache-tomcat-7.0.86/webapps/dubbo-admin-2.5.4/WEB-INF/dubbo.properties

    若是要運行監控中心,必須先啓動zookeeper。
    監控中心對於dubbo的服務的調用來講不是必須的,不安裝也能夠運行。安裝的目的是爲了更好的統計其調用的次數,方便管理。

五、後臺商品列表查詢功能

5.一、整合靜態頁面

靜態頁面位置:如圖:


使用方法:把靜態頁面添加到taotao-manager-web工程中的WEB-INF下,以下圖所示:

因爲在web.xml中定義的url攔截形式爲 「/」表示攔截全部的url請求,包括靜態資源例如css、js等。因此須要在springmvc.xml中添加資源映射標籤:
    <!-- 配置資源映射標籤 -->
    <mvc:resources location="/WEB-INF/js/" mapping="/js/**" />
    <mvc:resources location="/WEB-INF/css/" mapping="/css/**" />

5.二、展現後臺首頁+展現菜單頁面

5.2.一、功能分析

  • 首頁的請求的url:/
  • 請求參數:無
  • 返回值:index.jsp (首頁)

5.2.二、Controller

/**
 * 頁面展現Controller
 * @author  chenmingjun
 * @date    2018年11月9日下午7:59:33
 * @version 1.0
 */


@Controller
public class PageController {

    /**
     * 展現後臺首頁
     * @return
     */

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

    /**
     * 展現後臺菜單各頁面
     * @param page
     * @return
     */

    @RequestMapping("/{page}")
    public String showPage(@PathVariable String page) {
        return page;
    }

注意:要先刪掉webapp下測試用的的index.jsp。

5.三、後臺商品列表查詢功能分析

5.3.一、商品列表頁面

  • 瀏覽器界面效果:
  • 瀏覽器請求的網址:http://localhost:8081/item/list?page=1&rows=30
    • 對應的jsp:
      item-list.jsp
    • 請求的url:
      /item/list
    • 請求的參數:
      page=1&rows=30
  • 服務端響應的json數據格式:
    Easyui中datagrid控件要求的數據格式爲:
    {total:"2",rows:[{"id":"1","name":"張三"},{"id":"2","name":"李四"}]}

5.3.二、響應的json數據格式EasyUIDataGridResult

建立商品列表查詢時的返回的數據類EasyUIDataGridResult(包裝類),將該類放入taotao-common中,由於服務層和表現層都要用它!

package com.taotao.common.pojo;

import java.io.Serializable;
import java.util.List;

/**
 * 列表查詢時的返回的數據類
 * @author    chenmingjun
 * @date    2018年11月11日上午11:54:31
 * @version 1.0
 */

public class EasyUIDataGridResult implements Serializable{

    private Long total;

    private List<?> rows;

    private static final long serialVersionUID = 1L;

    public Long getTotal() {
        return total;
    }

    public void setTotal(Long total) {
        this.total = total;
    }

    public List<?> getRows() {
        return rows;
    }

    public void setRows(List<?> rows) {
        this.rows = rows;
    }

    @Override
    public String toString() {
        return "EasyUIDataGridResult [total=" + total + ", rows=" + rows + "]";
    }
}

5.3.三、分頁處理

  • 因爲逆向工程生成的代碼是不支持分頁處理的,若是想進行分頁須要本身編寫mapper,這樣就失去逆向工程的意義了。爲了提升開發效率可使用mybatis的分頁插件PageHelper。

5.四、Mybatis的分頁插件PageHelper

5.4.一、Mybatis分頁插件PageHelper的說明

  • 若是你也在用Mybatis,建議嘗試該分頁插件,這個必定是最方便使用的分頁插件。
  • 該插件目前支持Oracle、Mysql、MariaDB、SQLite、Hsqldb、PostgreSQL六種數據庫分頁。不支持SQLServer數據庫(微軟)。

5.4.二、使用方法

Maven中央倉庫最近更新的Artifact:http://maven.outofmemory.cn/
第一步:把PageHelper依賴的jar包添加到工程中。官方提供的代碼對逆向工程支持的很差,使用參考資料中的pagehelper-fix。


注意:咱們使用的PageHelper3.4.2-fix在Maven的中央倉庫下載不到(由於有一點小bug,被咱們修改過了,從新起了個名字),因此須要使用參考資料中的工程 ,將其導入到eclipse中,再使用maven安裝到本地倉庫。 即選中項目 --> 右鍵 --> Run As --> Maven install
第二步:在/taotao-manager-dao/pom.xml文件中添加對該分頁插件的依賴,以前咱們配置該文件的時候已經配置依賴過了。
雖然添加過依賴了,可是爲了筆記記錄的完整,咱們將添加的內容展現出來,以下:
    <!-- 添加對分頁插件pagehelper的依賴 -->
    <dependency>
        <groupId>com.github.pagehelper</groupId>
        <artifactId>pagehelper</artifactId>
    </dependency>

第三步:在Mybatis的全局配置文件中配置對攔截器插件的依賴。
配置文件位置:/taotao-manager-service/src/main/resources/mybatis/SqlMapConfig.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>
    <!-- 配置攔截器插件:做用是在執行sql語句以前,將sql語句攔截下來,在sql語句中拼接上limit -->   
    <plugins>
        <!-- com.github.pagehelper爲PageHelper類所在包名 -->
        <plugin interceptor="com.github.pagehelper.PageHelper">
            <!-- 設置支持數據庫類型 :目前該分頁插件支持Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六種數據庫-->        
            <property name="dialect" value="mysql"/>
        </plugin>
    </plugins>
</configuration>

第四步:在代碼中使用,參考「分頁測試」的代碼。

5.4.三、分頁測試

在/taotao-manager-service/src/test/java/com/taotao/pagehelper/中新建PagehelperTest.java文件,文件內容以下:

    @Test
    public void pagehelperTest() throws Exception 
{
        // 一、先在Mybatis的配置文件中配置攔截器插件:做用是在執行sql語句以前,將sql語句攔截下來,在sql語句中拼接上limit

        // 二、在執行查詢以前設置分頁條件,使用Pagehelper的靜態方法
        PageHelper.startPage(110);

        // 三、執行查詢,使用mapper,須要初始化一個spring容器,從spring容器中的獲得這個mapper的代理對象
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-dao.xml");
        TbItemMapper itemMapper = applicationContext.getBean(TbItemMapper.class);
        // 建立TbItemExample對象
        TbItemExample example = new TbItemExample();
        // 調用createCriteria()方法,建立條件查詢對象,本測試沒有條件,能夠不用調用,表示查詢全部
        // Criteria criteria = example.createCriteria();
        List<TbItem> list = itemMapper.selectByExample(example); // 本測試沒有條件,表示查詢全部

        // 四、取出分頁信息,分頁後,實際返回的結果list類型是Page<E>,若是想取出分頁信息
        // 方式一:須要強制轉換爲Page<E>
        // Page<TbItem> listIteam = (Page<TbItem>)list;
        // 方式二:咱們使用PageInfo對象對查詢出來的結果進行包裝,推薦使用第二種
        PageInfo<TbItem> pageInfo = new PageInfo<>(list);
        // PageInfo中包含了很是全面的分頁屬性,下面演示幾個
        System.out.println("總記錄數:" + pageInfo.getTotal());
        System.out.println("總頁數:" + pageInfo.getPages());
        System.out.println("當前頁數:" + pageInfo.getPageNum());
        System.out.println("每頁記錄數:" + pageInfo.getPageSize());
    }

5.五、Service層

  參數:Integer page、Integer rows
  業務邏輯:查詢全部商品列表,要進行分頁處理。
  返回值:EasyUIDataGridResult

5.5.一、接口代碼

    /**
     * 分頁查詢後臺商品列表信息
     * @param page
     * @param rows
     * @return
     */

    EasyUIDataGridResult getItemList(Integer page, Integer rows);

5.5.二、接口實現類代碼

    @Override
    public EasyUIDataGridResult getItemList(Integer page, Integer rows) {
        // 設置分頁信息,使用PageHelper
        if (page == null) {
            page = 1;
        }
        if (rows == null) {
            rows = 30;
        }
        PageHelper.startPage(page, rows);
        // 執行查詢,不須要設置查詢條件,即查詢全部
        TbItemExample example = new TbItemExample();
        List<TbItem> list = itemMapper.selectByExample(example);
        // 取分頁信息
        PageInfo<TbItem> pageInfo = new PageInfo<>(list);
        // 建立返回結果對象
        EasyUIDataGridResult result = new EasyUIDataGridResult();
        // 給返回的結果對象設置值(即封裝數據)
        result.setTotal(pageInfo.getTotal());
        result.setRows(list);

        return result;
    }

5.5.三、發佈服務

在taotao-manager-service中的applicationContext-service.xml中發佈服務。
注意address的值:使用本身的zookeeper所在的系統的ip地址和端口。


因爲taotao-manager-interface工程中新加了代碼,因此須要向本地倉庫更新下:
選中taotao-manager-interface工程右鍵 --> Run As --> Maven install

5.六、表現層

5.6.一、Controller

一、初始化表格請求的url:/item/list
二、Datagrid默認請求參數:
  1)page:當前的頁碼,從1開始。
  2)rows:每頁顯示的記錄數。
三、服務器響應的數據:json格式的數據。EasyUIDataGridResult

    /**
     * 分頁查詢後臺商品列表信息,並將商品列表信息使用json數據格式返回。
     * @param page
     * @param rows
     * @return
     */

    @RequestMapping("/item/list")
    @ResponseBody // 在後臺,把JavaBean強制轉換成json格式數據返回給前臺頁面。
    public EasyUIDataGridResult getItemList(Integer page, Integer rows) {
        EasyUIDataGridResult result = itemService.getItemList(page, rows);
        return result;
    }

5.6.二、引用服務

在taotao-manager-web工程中的springmvc.xml中引入服務。
注意address的值:使用本身的zookeeper所在的系統的ip地址和端口。

5.七、測試

1)須要先啓動zookeeper,再啓動服務層,再啓動表現層。
若是先啓動表現層,後啓動服務層,會報錯,可是不影響使用。
2)爲了更方便的進行測試,表現層工程和服務層工程屬於不一樣的工程,要debug的時候須要設置源碼,以下:
Debug設置源代碼,涉及到工程都要添加,爲了方便,能夠添加全部的工程。
以工程taotao-manager爲例:
步驟一:


步驟二:

步驟三:

5.八、解決錯誤+緣由

1)表現層警告:


說明:
  反序列化時,使用 Page<E>,在表現層沒有這個類,可是咱們有這個類的父類ArrayList,由於該類Page繼承ArrayList,以下圖,因此說這個警告咱們能夠不用管!若想解決這個警告,咱們能夠在web層(表現層)加入對分頁pagehelper的jar包的依賴(該包中有該類)。

2)表現層異常:


dubbo服務調用超時時間默認1秒,即表現層調用服務層,服務層在1秒鐘內不響應(重試3次),表現層就拋出異常。
因爲咱們要使用debug模式調試代碼,因此須要設置服務調用超時時間,dubbo服務調用超時時間默認1秒,咱們設置成300秒。便於咱們調試。

5.九、安裝maven工程時跳過測試

  • 一、maven命令安裝jar包時跳過測試:clean install -DskipTests
    表示先清理再安裝,打包時跳過測試。
  • 二、還可使用跳過測試的插件:maven-sufire-plugin
相關文章
相關標籤/搜索