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品牌表
Java之品优购课程讲义_day01(8)
品牌首字母
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

Java之品优购课程讲义_day01(8)

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

附录:常见错误
1.在注册中心找不到对应的服务
[AppleScript] 纯文本查看 复制代码
?
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 与端口是否填写正确,检查注册中心是否正常启动

猜你喜欢

转载自blog.51cto.com/13517854/2150529