电商项目(五)--后台查询商品

女爷i 2023-07-03 02:57 159阅读 0赞

1. 创建backend_item项目

首先对新创建的项目,进行pom文件的修改。

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <parent>
  6. <artifactId>bz_parent</artifactId>
  7. <groupId>com.bjsxt</groupId>
  8. <version>1.0-SNAPSHOT</version>
  9. </parent>
  10. <modelVersion>4.0.0</modelVersion>
  11. <artifactId>backend_item</artifactId>
  12. <dependencies>
  13. <!-- pojo-->
  14. <dependency>
  15. <groupId>com.bjsxt</groupId>
  16. <artifactId>common_pojo</artifactId>
  17. <version>1.0-SNAPSHOT</version>
  18. </dependency>
  19. <!--Spring Boot Web Starter-->
  20. <dependency>
  21. <groupId>org.springframework.boot</groupId>
  22. <artifactId>spring-boot-starter-web</artifactId>
  23. </dependency>
  24. <!--Spring Cloud Eureka Client Starter-->
  25. <dependency>
  26. <groupId>org.springframework.cloud</groupId>
  27. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  28. </dependency>
  29. <!--Spring Cloud OpenFeign Starter-->
  30. <dependency>
  31. <groupId>org.springframework.cloud</groupId>
  32. <artifactId>spring-cloud-starter-openfeign</artifactId>
  33. </dependency>
  34. <!-- utils -->
  35. <dependency>
  36. <groupId>com.bjsxt</groupId>
  37. <artifactId>common_utils</artifactId>
  38. <version>1.0-SNAPSHOT</version>
  39. </dependency>
  40. </dependencies>
  41. </project>

其次,对配置文件进行编写。

  1. spring:
  2. application:
  3. name: backend_item
  4. server:
  5. port: 9020
  6. eureka:
  7. client:
  8. serviceUrl:
  9. defaultZone: http://eureka1:8761/eureka/

进而创建启动类;

  1. package com.bjsxt.backenditem;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
  5. import org.springframework.cloud.openfeign.EnableFeignClients;
  6. /** * backenditem服务 * */
  7. @SpringBootApplication
  8. @EnableEurekaClient
  9. @EnableFeignClients
  10. public class BackendItemApplication {
  11. public static void main(String[] args) {
  12. SpringApplication.run(BackendItemApplication.class,args);
  13. }
  14. }

2. 开发商品管理接口

1. 实现商品查询的接口

1.1 在common_item服务中实现查询商品的服务

首先在common_utils项目中添加PageResult模型

  1. package com.bjsxt.utils;
  2. import java.io.Serializable;
  3. import java.util.List;
  4. import java.util.Objects;
  5. /** * * 分页模型 */
  6. public class PageResult implements Serializable {
  7. private Integer pageIndex;//当前页
  8. private Long totalPage;//总页数
  9. private List result;//结果集
  10. public PageResult() {
  11. }
  12. public PageResult(Integer pageIndex, Long totalPage, List result) {
  13. this.pageIndex = pageIndex;
  14. this.totalPage = totalPage;
  15. this.result = result;
  16. }
  17. @Override
  18. public String toString() {
  19. return "PageResult{" +
  20. "pageIndex=" + pageIndex +
  21. ", totalPage=" + totalPage +
  22. ", result=" + result +
  23. '}';
  24. }
  25. @Override
  26. public boolean equals(Object o) {
  27. if (this == o) return true;
  28. if (o == null || getClass() != o.getClass()) return false;
  29. PageResult that = (PageResult) o;
  30. return Objects.equals(pageIndex, that.pageIndex) &&
  31. Objects.equals(totalPage, that.totalPage) &&
  32. Objects.equals(result, that.result);
  33. }
  34. @Override
  35. public int hashCode() {
  36. return Objects.hash(pageIndex, totalPage, result);
  37. }
  38. public Integer getPageIndex() {
  39. return pageIndex;
  40. }
  41. public void setPageIndex(Integer pageIndex) {
  42. this.pageIndex = pageIndex;
  43. }
  44. public Long getTotalPage() {
  45. return totalPage;
  46. }
  47. public void setTotalPage(Long totalPage) {
  48. this.totalPage = totalPage;
  49. }
  50. public List getResult() {
  51. return result;
  52. }
  53. public void setResult(List result) {
  54. this.result = result;
  55. }
  56. }

创建controller层

  1. package com.bjsxt.item.controller;
  2. import com.bjsxt.item.service.ItemService;
  3. import com.bjsxt.utils.PageResult;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.RequestParam;
  7. import org.springframework.web.bind.annotation.RestController;
  8. @RestController
  9. @RequestMapping("/service/item")
  10. public class ItemController {
  11. @Autowired
  12. private ItemService itemService;
  13. /** * 查询商品的数据 * */
  14. @RequestMapping("/selectTbItemAllByPage")
  15. public PageResult selectTbItemAllByPage(@RequestParam Integer page,@RequestParam Integer rows){
  16. PageResult pageResult = itemService.selectTbItemAllByPage(page, rows);
  17. return pageResult;
  18. }
  19. }

创建service层

  1. package com.bjsxt.item.service;
  2. import com.bjsxt.utils.PageResult;
  3. public interface ItemService {
  4. PageResult selectTbItemAllByPage(Integer page,Integer rows);
  5. }

创建serviceImpl层

  1. package com.bjsxt.item.service.impl;
  2. import com.bjsxt.item.service.ItemService;
  3. import com.bjsxt.mapper.TbItemMapper;
  4. import com.bjsxt.pojo.TbItem;
  5. import com.bjsxt.pojo.TbItemExample;
  6. import com.bjsxt.utils.PageResult;
  7. import com.github.pagehelper.PageHelper;
  8. import com.github.pagehelper.PageInfo;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.stereotype.Service;
  11. import java.util.List;
  12. @Service
  13. public class ItemServiceImpl implements ItemService {
  14. @Autowired
  15. private TbItemMapper tbItemMapper;
  16. //查询所有的商品信息
  17. @Override
  18. public PageResult selectTbItemAllByPage(Integer page, Integer rows) {
  19. //开启分页插件
  20. PageHelper.startPage(page, rows);
  21. TbItemExample example = new TbItemExample();
  22. example.createCriteria().andStatusEqualTo((byte)1);
  23. List<TbItem> tbItems = tbItemMapper.selectByExample(example);
  24. PageInfo<TbItem> pageInfo = new PageInfo<>();
  25. PageResult pageResult = new PageResult();
  26. pageResult.setPageIndex(page);
  27. pageResult.setTotalPage(pageInfo.getTotal());
  28. pageResult.setResult(tbItems);
  29. System.out.println(pageResult);
  30. return pageResult;
  31. }
  32. }

1.2 在backend_item服务中实现商品查询

创建controller

  1. package com.bjsxt.backenditem.controller;
  2. import com.bjsxt.backenditem.service.ItemService;
  3. import com.bjsxt.utils.Result;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.RequestParam;
  7. import org.springframework.web.bind.annotation.RestController;
  8. @RestController
  9. @RequestMapping("/backend/item")
  10. public class ItemController {
  11. @Autowired
  12. private ItemService itemService;
  13. /** * 查询商品且分页显示 */
  14. @RequestMapping("/selectTbItemAllByPage")
  15. public Result selectTbItemAllByPage(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "2") Integer rows){
  16. try{
  17. Result result = itemService.selectTbItemAllByPage(page, rows);
  18. return result;
  19. }catch (Exception e){
  20. e.printStackTrace();
  21. }
  22. return Result.build(500,"error");
  23. }
  24. }

创建service

  1. package com.bjsxt.backenditem.service;
  2. import com.bjsxt.utils.Result;
  3. public interface ItemService {
  4. //查询所有的商品并分页
  5. Result selectTbItemAllByPage(Integer page,Integer rows);
  6. }

创建serviceImpl

  1. package com.bjsxt.backenditem.service.impl;
  2. import com.bjsxt.backenditem.feign.CommonItemFeignClient;
  3. import com.bjsxt.backenditem.service.ItemService;
  4. import com.bjsxt.utils.PageResult;
  5. import com.bjsxt.utils.Result;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.stereotype.Service;
  8. @Service
  9. public class ItemServiceImpl implements ItemService {
  10. @Autowired
  11. private CommonItemFeignClient commonItemFeignClient;
  12. @Override
  13. public Result selectTbItemAllByPage(Integer page, Integer rows) {
  14. PageResult pageResult = commonItemFeignClient.selectTbItemAllByPage(page, rows);
  15. if(pageResult!=null && pageResult.getResult()!=null && pageResult.getResult().size()>0){
  16. return Result.ok(pageResult);
  17. }
  18. return Result.error("查无结果");
  19. }
  20. }

创建FeignClient
在这里插入图片描述

  1. package com.bjsxt.backenditem.feign;
  2. import com.bjsxt.utils.PageResult;
  3. import org.springframework.cloud.openfeign.FeignClient;
  4. import org.springframework.web.bind.annotation.GetMapping;
  5. import org.springframework.web.bind.annotation.RequestParam;
  6. @FeignClient(value = "common-item")
  7. public interface CommonItemFeignClient {
  8. @GetMapping("/service/item/selectTbItemAllByPage")
  9. PageResult selectTbItemAllByPage(@RequestParam("page") Integer page, @RequestParam("rows") Integer rows);
  10. }

注意:
在这里插入图片描述
效果图:
在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,159人围观)

还没有评论,来说两句吧...

相关阅读