5.品牌列表-后端代码
5.1 需求分析
完成品牌管理的后端代码,在浏览器可查询品牌的数据(json 格式)
5.2 数据库表
tb_brand 品牌表
5.3 后端代码
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 findAll();
}
5.2.2 服务实现类
在 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 findAll() {
return brandMapper.selectByExample(null);
}
}
5.2.3 控制层代码
在 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 findAll(){
return brandService.findAll();
}
}
5.4 测试
启动 pinyougou-sellergoods-service
启动 pinyougou-manager-web
地址栏输入 http://localhost:9101/brand/findAll.do
附录:常见错误
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,这个地方极容易出错。另外还有一个原因就是你的服务层工程由于某些原因没有正常启动,也无法注册到注册中心里。
2.无法连接到注册中心
org.I0Itec.zkclient.exception.ZkTimeoutException: Unable to connect to zookeeper server within
timeout: 5000 org.I0Itec.zkclient.ZkClient.connect(ZkClient.java:876)
org.I0Itec.zkclient.ZkClient.(ZkClient.java:98)
org.I0Itec.zkclient.ZkClient.(ZkClient.java:92)
org.I0Itec.zkclient.ZkClient.(ZkClient.java:80)
com.alibaba.dubbo.remoting.zookeeper.zkclient.ZkclientZookeeperClient.(ZkclientZook
eeperClient.java:26)
请检查 IP 与端口是否填写正确,检查注册中心是否正常启动