From 05563b81a429d28f595c1330b06ee415d8279787 Mon Sep 17 00:00:00 2001 From: 17693089504 <2290100824@qq.com> Date: Thu, 25 Feb 2021 21:15:24 +0800 Subject: [PATCH] =?UTF-8?q?luoliang=20=E7=AC=AC=E4=B8=80=E6=AC=A1=E6=8F=90?= =?UTF-8?q?=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mall/xiaomi/XiaomiMasterApplication.java | 2 +- .../xiaomi/controller/CarouselController.java | 31 ++++++- .../xiaomi/controller/ProductController.java | 53 ++++++++++++ .../mall/xiaomi/exception/XmException.java | 20 +++++ .../xiaomi/exception/XmExceptionHandler.java | 30 +++++++ .../mall/xiaomi/mapper/CarouselMapper.java | 14 ++++ .../com/mall/xiaomi/mapper/ProductMapper.java | 44 ++++++++++ .../java/com/mall/xiaomi/pojo/Category.java | 6 +- .../mall/xiaomi/service/CarouselService.java | 12 ++- .../mall/xiaomi/service/ProductService.java | 29 +++++++ .../service/impl/CarouselServiceImpl.java | 31 +++++++ .../service/impl/ProductServiceImpl.java | 83 +++++++++++++++++++ src/main/java/com/mall/xiaomi/vo/CartVo.java | 1 + src/main/resources/application.yml | 5 +- 14 files changed, 353 insertions(+), 8 deletions(-) create mode 100644 src/main/java/com/mall/xiaomi/controller/ProductController.java create mode 100644 src/main/java/com/mall/xiaomi/exception/XmException.java create mode 100644 src/main/java/com/mall/xiaomi/exception/XmExceptionHandler.java create mode 100644 src/main/java/com/mall/xiaomi/mapper/ProductMapper.java create mode 100644 src/main/java/com/mall/xiaomi/service/ProductService.java create mode 100644 src/main/java/com/mall/xiaomi/service/impl/CarouselServiceImpl.java create mode 100644 src/main/java/com/mall/xiaomi/service/impl/ProductServiceImpl.java diff --git a/src/main/java/com/mall/xiaomi/XiaomiMasterApplication.java b/src/main/java/com/mall/xiaomi/XiaomiMasterApplication.java index 3611415..3afbc92 100644 --- a/src/main/java/com/mall/xiaomi/XiaomiMasterApplication.java +++ b/src/main/java/com/mall/xiaomi/XiaomiMasterApplication.java @@ -9,7 +9,7 @@ import org.springframework.scheduling.annotation.EnableScheduling; import tk.mybatis.spring.annotation.MapperScan; @SpringBootApplication -@MapperScan("com.mall.xiaomi.mapper") +@MapperScan(basePackages = {"com.mall.xiaomi.mapper"}) @EnableScheduling public class XiaomiMasterApplication { diff --git a/src/main/java/com/mall/xiaomi/controller/CarouselController.java b/src/main/java/com/mall/xiaomi/controller/CarouselController.java index ca81945..bc849f4 100644 --- a/src/main/java/com/mall/xiaomi/controller/CarouselController.java +++ b/src/main/java/com/mall/xiaomi/controller/CarouselController.java @@ -1,5 +1,15 @@ package com.mall.xiaomi.controller; +import com.mall.xiaomi.pojo.Carousel; +import com.mall.xiaomi.service.CarouselService; +import com.mall.xiaomi.util.ResultMessage; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; + /** * @ClassName CarouselController * @Description TODO @@ -7,6 +17,25 @@ package com.mall.xiaomi.controller; * @Data 2021/2/24 14:40 * @Version 1.0 */ +@RestController + public class CarouselController { - + + @Autowired + private CarouselService carouselService; + @Autowired + private ResultMessage resultMessage; + + @GetMapping("/resources/carousel") + public ResultMessage carousels(){ + List carousels = carouselService.selectAll(); + if (carousels != null){ + resultMessage.success("200", carousels); + return resultMessage; + }else { + resultMessage.success("400", null); + return resultMessage; + } + + } } diff --git a/src/main/java/com/mall/xiaomi/controller/ProductController.java b/src/main/java/com/mall/xiaomi/controller/ProductController.java new file mode 100644 index 0000000..afba43a --- /dev/null +++ b/src/main/java/com/mall/xiaomi/controller/ProductController.java @@ -0,0 +1,53 @@ +package com.mall.xiaomi.controller; + +import com.mall.xiaomi.pojo.Product; +import com.mall.xiaomi.service.ProductService; +import com.mall.xiaomi.util.ResultMessage; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; + +/** + * @ClassName ProductController + * @Description TODO + * @action luoliang + * @Data 2021/2/25 16:42 + * @Version 1.0 + */ + +@RestController +@RequestMapping("/product") +public class ProductController { + + @Autowired + private ResultMessage resultMessage; + @Autowired + private ProductService productService; + + @GetMapping("/category/limit/{categoryId}") + public ResultMessage getProductByCategoryId(@PathVariable Integer categoryId) { + List list = productService.getProductByCategoryId(categoryId); + if (list != null){ + resultMessage.success("200",list); + return resultMessage; + }else { + resultMessage.success("200",null); + return resultMessage; + } + } + @GetMapping("/category/hot") + public ResultMessage getHotProduct(){ + List hotProduct = productService.getHotProduct(); + if (hotProduct != null){ + resultMessage.success("200",hotProduct); + return resultMessage; + }else { + resultMessage.success("200",null); + return resultMessage; + } + } +} diff --git a/src/main/java/com/mall/xiaomi/exception/XmException.java b/src/main/java/com/mall/xiaomi/exception/XmException.java new file mode 100644 index 0000000..e2da38d --- /dev/null +++ b/src/main/java/com/mall/xiaomi/exception/XmException.java @@ -0,0 +1,20 @@ +package com.mall.xiaomi.exception; + +import lombok.AllArgsConstructor; +import lombok.Generated; +import lombok.Getter; +import lombok.NoArgsConstructor; + +/** + * @ClassName XmException + * @Description TODO + * @action luoliang + * @Data 2021/2/25 19:17 + * @Version 1.0 + */ +@AllArgsConstructor +@NoArgsConstructor +@Getter +public class XmException extends RuntimeException { + private ExceptionEnum exceptionEnum; +} diff --git a/src/main/java/com/mall/xiaomi/exception/XmExceptionHandler.java b/src/main/java/com/mall/xiaomi/exception/XmExceptionHandler.java new file mode 100644 index 0000000..1512ca2 --- /dev/null +++ b/src/main/java/com/mall/xiaomi/exception/XmExceptionHandler.java @@ -0,0 +1,30 @@ +package com.mall.xiaomi.exception; + +import com.mall.xiaomi.util.ResultMessage; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.ResponseBody; + +/** + * @ClassName XmExceptionHandler + * @Description TODO + * @action luoliang + * @Data 2021/2/25 19:18 + * @Version 1.0 + */ +@ControllerAdvice +@ResponseBody +public class XmExceptionHandler { + + @Autowired + private ResultMessage resultMessage; + @ExceptionHandler(XmException.class) + public ResultMessage handleException(XmException e){ + ExceptionEnum em = e.getExceptionEnum(); + resultMessage.fail(em.getCode() + "",em.getMsg()); + return resultMessage; + } +} diff --git a/src/main/java/com/mall/xiaomi/mapper/CarouselMapper.java b/src/main/java/com/mall/xiaomi/mapper/CarouselMapper.java index faf96f8..e3f6716 100644 --- a/src/main/java/com/mall/xiaomi/mapper/CarouselMapper.java +++ b/src/main/java/com/mall/xiaomi/mapper/CarouselMapper.java @@ -1,5 +1,11 @@ package com.mall.xiaomi.mapper; +import com.mall.xiaomi.pojo.Carousel; +import org.apache.ibatis.annotations.Mapper; +import org.apache.ibatis.annotations.Select; + +import java.util.List; + /** * @ClassName CarouselMapper * @Description TODO @@ -7,5 +13,13 @@ package com.mall.xiaomi.mapper; * @Data 2021/2/24 14:36 * @Version 1.0 */ + public interface CarouselMapper { + /** + * 轮播图 + * @return + */ + @Select("select * from carousel") + List selectAll(); + } diff --git a/src/main/java/com/mall/xiaomi/mapper/ProductMapper.java b/src/main/java/com/mall/xiaomi/mapper/ProductMapper.java new file mode 100644 index 0000000..6597bf2 --- /dev/null +++ b/src/main/java/com/mall/xiaomi/mapper/ProductMapper.java @@ -0,0 +1,44 @@ +package com.mall.xiaomi.mapper; + +import com.mall.xiaomi.pojo.Product; + +import org.apache.ibatis.annotations.Select; + +import tk.mybatis.mapper.common.Mapper; +import tk.mybatis.mapper.entity.Example; + +import java.util.List; + +/** + * @ClassName ProductMapper + * @Description TODO + * @action luoliang + * @Data 2021/2/24 16:40 + * @Version 1.0 + */ +public interface ProductMapper extends Mapper { + /** + * 获取各类商品数据封装 + * @param categoryId + * @return + */ + @Select("SELECT * FROM product WHERE category_id=#{categoryId}") + List getProductByCategoryId(Integer categoryId); + + /** + * 获取商品id + * @return + */ + @Select("select product_id from product") + List getHotProduct(); + + /** + * + * @param example + * @return + */ + /* @Select("select * from product where category_id=#{categoryId}") + List selectByExample(Example example);*/ + + +} diff --git a/src/main/java/com/mall/xiaomi/pojo/Category.java b/src/main/java/com/mall/xiaomi/pojo/Category.java index 2b60ea1..bdeb458 100644 --- a/src/main/java/com/mall/xiaomi/pojo/Category.java +++ b/src/main/java/com/mall/xiaomi/pojo/Category.java @@ -2,16 +2,14 @@ package com.mall.xiaomi.pojo; import lombok.Data; -import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; -import javax.persistence.Id; -import javax.persistence.Table; +import javax.persistence.*; @Data @Table(name = "category") public class Category { @Id @GeneratedValue(strategy = GenerationType.IDENTITY,generator = "JDBC") + @Column(name = "carousel_id") private Integer categoryId; private String categoryName; diff --git a/src/main/java/com/mall/xiaomi/service/CarouselService.java b/src/main/java/com/mall/xiaomi/service/CarouselService.java index f88169c..c92d8c8 100644 --- a/src/main/java/com/mall/xiaomi/service/CarouselService.java +++ b/src/main/java/com/mall/xiaomi/service/CarouselService.java @@ -1,5 +1,9 @@ package com.mall.xiaomi.service; +import com.mall.xiaomi.pojo.Carousel; + +import java.util.List; + /** * @ClassName CarouselService * @Description TODO @@ -7,5 +11,11 @@ package com.mall.xiaomi.service; * @Data 2021/2/24 14:41 * @Version 1.0 */ -public class CarouselService { +public interface CarouselService { + /** + * 轮播图 + * @return + */ + List selectAll(); } + diff --git a/src/main/java/com/mall/xiaomi/service/ProductService.java b/src/main/java/com/mall/xiaomi/service/ProductService.java new file mode 100644 index 0000000..ea00eb2 --- /dev/null +++ b/src/main/java/com/mall/xiaomi/service/ProductService.java @@ -0,0 +1,29 @@ +package com.mall.xiaomi.service; + +import com.mall.xiaomi.pojo.Product; +import org.apache.ibatis.annotations.Select; + +import java.util.List; + +/** + * @ClassName ProductService + * @Description TODO + * @action luoliang + * @Data 2021/2/25 16:39 + * @Version 1.0 + */ +public interface ProductService { + /** + * 获取各类商品数据封装 + * @param categoryId + * @return + */ + + List getProductByCategoryId(Integer categoryId); + + /** + * 获取商品id + * @return + */ + List getHotProduct(); +} diff --git a/src/main/java/com/mall/xiaomi/service/impl/CarouselServiceImpl.java b/src/main/java/com/mall/xiaomi/service/impl/CarouselServiceImpl.java new file mode 100644 index 0000000..9974298 --- /dev/null +++ b/src/main/java/com/mall/xiaomi/service/impl/CarouselServiceImpl.java @@ -0,0 +1,31 @@ +package com.mall.xiaomi.service.impl; + +import com.mall.xiaomi.mapper.CarouselMapper; +import com.mall.xiaomi.pojo.Carousel; +import com.mall.xiaomi.service.CarouselService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; + +/** + * @ClassName CarouselServiceImpl + * @Description TODO + * @action luoliang + * @Data 2021/2/24 17:46 + * @Version 1.0 + */ +@Service +public class CarouselServiceImpl implements CarouselService { + + @Autowired + private CarouselMapper carouselMapper; + /** + * 轮播图演示 + * @return + */ + @Override + public List selectAll() { + return carouselMapper.selectAll(); + } +} diff --git a/src/main/java/com/mall/xiaomi/service/impl/ProductServiceImpl.java b/src/main/java/com/mall/xiaomi/service/impl/ProductServiceImpl.java new file mode 100644 index 0000000..eeb9034 --- /dev/null +++ b/src/main/java/com/mall/xiaomi/service/impl/ProductServiceImpl.java @@ -0,0 +1,83 @@ +package com.mall.xiaomi.service.impl; + +import com.github.pagehelper.PageHelper; +import com.github.pagehelper.PageInfo; +import com.mall.xiaomi.exception.ExceptionEnum; +import com.mall.xiaomi.exception.XmException; +import com.mall.xiaomi.mapper.ProductMapper; +import com.mall.xiaomi.pojo.Product; +import com.mall.xiaomi.service.ProductService; +import org.apache.commons.lang3.ArrayUtils; +import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import tk.mybatis.mapper.entity.Example; + +import java.util.List; +import java.util.stream.Collectors; + +/** + * @ClassName ProductServiceImpl + * @Description TODO + * @action luoliang + * @Data 2021/2/25 16:39 + * @Version 1.0 + */ +@Service +public class ProductServiceImpl implements ProductService { + + @Autowired + private ProductMapper productMapper; + + /** + * 获取各类商品数据封装 + * @param categoryId + * @return + */ +/* + @Override + public List getProductByCategoryId(Integer categoryId) { + return productMapper.getProductByCategoryId(categoryId); + + } +*/ + + @Override + public List getProductByCategoryId(Integer categoryId) { + List list = null; + Example example = new Example(Product.class); + example.orderBy("productSales").desc(); + example.createCriteria().andEqualTo("categoryId", categoryId); + PageHelper.startPage(0, 8); + try { + list = productMapper.selectByExample(example); + if (ArrayUtils.isEmpty(list.toArray())) { + throw new XmException(ExceptionEnum.GET_PRODUCT_NOT_FOUND); + } + } catch (Exception e) { + e.printStackTrace(); + throw new XmException(ExceptionEnum.GET_PRODUCT_ERROR); + } + return list; + } + + @Override + public List getHotProduct() { + List list = null; + Example example = new Example(Product.class); + example.orderBy("productSales").desc(); + PageHelper.startPage(0,8); + try { + list = productMapper.selectByExample(example); + if (ArrayUtils.isEmpty(list.toArray())) { + throw new XmException(ExceptionEnum.GET_PRODUCT_NOT_FOUND); + } + } catch (Exception e) { + e.printStackTrace(); + throw new XmException(ExceptionEnum.GET_PRODUCT_ERROR); + } + return list; + } + + +} diff --git a/src/main/java/com/mall/xiaomi/vo/CartVo.java b/src/main/java/com/mall/xiaomi/vo/CartVo.java index f8fbf8e..4e9f0dc 100644 --- a/src/main/java/com/mall/xiaomi/vo/CartVo.java +++ b/src/main/java/com/mall/xiaomi/vo/CartVo.java @@ -8,4 +8,5 @@ package com.mall.xiaomi.vo; * @Version 1.0 */ public class CartVo { + } diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index b75dfe6..17cf0d2 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -1,8 +1,11 @@ server: port: 3000 spring: + application: + name: emp-service datasource: - url: jdbc:mysql://10.12.151.254:3306/xiaomi?characterEncoding=UTF8 + driver-class-name: com.mysql.cj.jdbc.Driver + url: jdbc:mysql://localhost:3306/xiaomi?serverTimezone=UTC username: root password: 123456 redis: -- Gitee