1. 程式人生 > >Java之品優購課程講義_day01(8)

Java之品優購課程講義_day01(8)

4.1.1 商家管理後臺

構建 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"  />
4.4實體類與資料訪問層模組
4.4.1生成程式碼
利用反向工程 generatorSqlmapCustom 實現實體類與資料訪問層程式碼的自動生成

4.4.1 拷貝程式碼

將 com.pinyougou.pojo 包拷貝到 pojo 工程

將 com.pinyougou.mapper 包和 resouce 下的 com.pinyougou.mapper 資料夾拷貝到 dao 工程

4.4.2 修改實體類程式碼

修改每個實體類,讓其實現  Serializable 介面

5.品牌列表-後端程式碼

5.1 需求分析

完成品牌管理的後端程式碼,在瀏覽器可查詢品牌的資料(json 格式)


5.2 資料庫表

tb_brand品牌表

欄位

型別

長度

含義

Id

Bigint

主鍵

Name

Varchar

255

品牌名稱

First_char

Varchar

1

品牌首字母

5.1 後端程式碼5.2.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();
 
}
5.2.1 服務實現類
在 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);
 
}
 
}
5.2.1 控制層程式碼
在 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();
 
}
 
}
測試
啟動 pinyougou-sellergoods-service
啟動 pinyougou-manager-web
位址列輸入 http://localhost:9101/brand/findAll.do

 
可以看到瀏覽器輸出了 json 資料。


附錄:常見錯誤
1.在註冊中心找不到對應的服務

java.lang.IllegalStateException: Failed to check the status of the service com.pinyougou.sellergoods.service.BrandService. No provider available for the service com.pinyougou.sellergoods.service.BrandService from the url zookeeper://192.168.25.129:2181/com.alibaba.dubbo.registry.RegistryService?application=pinyo ugou-manager- web&dubbo=2.8.4&interface=com.pinyougou.sellergoods.service.BrandService&methods=updat e,get,delete,selectOptionList,add,getListByPage&pid=3980&revision=0.0.1- SNAPSHOT&side=consumer×tamp=1501146823396 to the consumer 172.16.17.14 use dubbo version 2.8.4

這種錯誤是服務層程式碼沒有成功註冊到註冊中心導致,請檢查一下你的服務層程式碼是否添加了@service 註解,並且該註解的包一定是 com.alibaba.dubbo.config.annotation 包,不是
org.springframework.stereotype.Service,這個地方極容易出錯。另外還有一個原因就是你的 服務層工程由於某些原因沒有正常啟動,也無法註冊到註冊中心裡。
無法連線到註冊中心
請檢查 IP 與埠是否填寫正確,檢查註冊中心是否正常啟動