From d1e687da739d2ae6c07a7003205a79d305001c43 Mon Sep 17 00:00:00 2001 From: zhh Date: Tue, 19 Jun 2018 17:27:41 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=95=86=E5=93=81=E5=AF=BC?= =?UTF-8?q?=E5=85=A5es=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mall-search/pom.xml | 18 +- .../mall/search/MallSearchApplication.java | 2 + .../mall/search/config/Swagger2Config.java | 38 ++++ .../controller/EsProductController.java | 28 +++ .../macro/mall/search/dao/EsProductDao.java | 13 ++ .../macro/mall/search/domain/EsProduct.java | 190 ++++++++++++++++++ .../repository/EsProductRepository.java | 11 + .../mall/search/service/EsProductService.java | 12 ++ .../service/impl/EsProductServiceImpl.java | 28 +++ .../src/main/resources/application.properties | 18 ++ .../src/main/resources/dao/EsProductDao.xml | 33 +++ .../search/MallSearchApplicationTests.java | 13 +- 12 files changed, 402 insertions(+), 2 deletions(-) create mode 100644 mall-search/src/main/java/com/macro/mall/search/config/Swagger2Config.java create mode 100644 mall-search/src/main/java/com/macro/mall/search/controller/EsProductController.java create mode 100644 mall-search/src/main/java/com/macro/mall/search/dao/EsProductDao.java create mode 100644 mall-search/src/main/java/com/macro/mall/search/domain/EsProduct.java create mode 100644 mall-search/src/main/java/com/macro/mall/search/repository/EsProductRepository.java create mode 100644 mall-search/src/main/java/com/macro/mall/search/service/EsProductService.java create mode 100644 mall-search/src/main/java/com/macro/mall/search/service/impl/EsProductServiceImpl.java create mode 100644 mall-search/src/main/resources/dao/EsProductDao.xml diff --git a/mall-search/pom.xml b/mall-search/pom.xml index 8d622d5..6ca9f8f 100644 --- a/mall-search/pom.xml +++ b/mall-search/pom.xml @@ -38,12 +38,28 @@ org.springframework.boot spring-boot-starter-web - org.springframework.boot spring-boot-starter-test test + + + com.github.pagehelper + pagehelper-spring-boot-starter + 1.2.3 + + + + io.springfox + springfox-swagger2 + 2.6.1 + + + io.springfox + springfox-swagger-ui + 2.6.1 + diff --git a/mall-search/src/main/java/com/macro/mall/search/MallSearchApplication.java b/mall-search/src/main/java/com/macro/mall/search/MallSearchApplication.java index 3f820d8..5588e09 100644 --- a/mall-search/src/main/java/com/macro/mall/search/MallSearchApplication.java +++ b/mall-search/src/main/java/com/macro/mall/search/MallSearchApplication.java @@ -1,9 +1,11 @@ package com.macro.mall.search; +import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication +@MapperScan({"com.macro.mall.mapper","com.macro.mall.search.dao"}) public class MallSearchApplication { public static void main(String[] args) { diff --git a/mall-search/src/main/java/com/macro/mall/search/config/Swagger2Config.java b/mall-search/src/main/java/com/macro/mall/search/config/Swagger2Config.java new file mode 100644 index 0000000..a89caeb --- /dev/null +++ b/mall-search/src/main/java/com/macro/mall/search/config/Swagger2Config.java @@ -0,0 +1,38 @@ +package com.macro.mall.search.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import springfox.documentation.builders.ApiInfoBuilder; +import springfox.documentation.builders.PathSelectors; +import springfox.documentation.builders.RequestHandlerSelectors; +import springfox.documentation.service.ApiInfo; +import springfox.documentation.spi.DocumentationType; +import springfox.documentation.spring.web.plugins.Docket; +import springfox.documentation.swagger2.annotations.EnableSwagger2; + +/** + * Swagger2API文档的配置 + * Created by macro on 2018/4/26. + */ +@Configuration +@EnableSwagger2 +public class Swagger2Config { + @Bean + public Docket createRestApi(){ + return new Docket(DocumentationType.SWAGGER_2) + .apiInfo(apiInfo()) + .select() + .apis(RequestHandlerSelectors.basePackage("com.macro.mall.search.controller")) + .paths(PathSelectors.any()) + .build(); + } + + private ApiInfo apiInfo() { + return new ApiInfoBuilder() + .title("mall搜索系统") + .description("mall搜索模块") + .contact("macro") + .version("1.0") + .build(); + } +} diff --git a/mall-search/src/main/java/com/macro/mall/search/controller/EsProductController.java b/mall-search/src/main/java/com/macro/mall/search/controller/EsProductController.java new file mode 100644 index 0000000..8a3e3b7 --- /dev/null +++ b/mall-search/src/main/java/com/macro/mall/search/controller/EsProductController.java @@ -0,0 +1,28 @@ +package com.macro.mall.search.controller; + +import com.macro.mall.search.service.EsProductService; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.ResponseBody; + +/** + * 搜索商品管理Controller + * Created by macro on 2018/6/19. + */ +@Controller +@Api(tags = "EsProductController", description = "搜索商品管理") +@RequestMapping("/search/product") +public class EsProductController { + @Autowired + private EsProductService esProductService; + @ApiOperation(value = "导入所有数据库中商品到ES") + @RequestMapping(value = "/importAll", method = RequestMethod.POST) + @ResponseBody + public Object importAllList() { + return esProductService.importAll(); + } +} diff --git a/mall-search/src/main/java/com/macro/mall/search/dao/EsProductDao.java b/mall-search/src/main/java/com/macro/mall/search/dao/EsProductDao.java new file mode 100644 index 0000000..d460e46 --- /dev/null +++ b/mall-search/src/main/java/com/macro/mall/search/dao/EsProductDao.java @@ -0,0 +1,13 @@ +package com.macro.mall.search.dao; + +import com.macro.mall.search.domain.EsProduct; + +import java.util.List; + +/** + * 搜索系统中的商品管理自定义Dao + * Created by macro on 2018/6/19. + */ +public interface EsProductDao { + List getAllEsProductList(); +} diff --git a/mall-search/src/main/java/com/macro/mall/search/domain/EsProduct.java b/mall-search/src/main/java/com/macro/mall/search/domain/EsProduct.java new file mode 100644 index 0000000..cb51212 --- /dev/null +++ b/mall-search/src/main/java/com/macro/mall/search/domain/EsProduct.java @@ -0,0 +1,190 @@ +package com.macro.mall.search.domain; + +import org.springframework.data.elasticsearch.annotations.Document; + +import java.io.Serializable; +import java.math.BigDecimal; +import java.util.List; + +/** + * 搜索中的商品信息 + * Created by macro on 2018/6/19. + */ +@Document(indexName = "pms", type = "product") +public class EsProduct implements Serializable { + private static final long serialVersionUID = -1L; + private Long id; + private String productSn; + private Long brandId; + private String brandName; + private Long productCategoryId; + private String productCategoryName; + private String pic; + private String name; + private String subTitle; + private BigDecimal price; + private Integer sale; + private Integer newStatus; + private Integer recommandStatus; + private Integer stock; + private Integer promotionType; + private Integer sort; + private List attrValueList; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getProductSn() { + return productSn; + } + + public void setProductSn(String productSn) { + this.productSn = productSn; + } + + public Long getBrandId() { + return brandId; + } + + public void setBrandId(Long brandId) { + this.brandId = brandId; + } + + public String getBrandName() { + return brandName; + } + + public void setBrandName(String brandName) { + this.brandName = brandName; + } + + public Long getProductCategoryId() { + return productCategoryId; + } + + public void setProductCategoryId(Long productCategoryId) { + this.productCategoryId = productCategoryId; + } + + public String getProductCategoryName() { + return productCategoryName; + } + + public void setProductCategoryName(String productCategoryName) { + this.productCategoryName = productCategoryName; + } + + public String getPic() { + return pic; + } + + public void setPic(String pic) { + this.pic = pic; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getSubTitle() { + return subTitle; + } + + public void setSubTitle(String subTitle) { + this.subTitle = subTitle; + } + + public BigDecimal getPrice() { + return price; + } + + public void setPrice(BigDecimal price) { + this.price = price; + } + + public Integer getSale() { + return sale; + } + + public void setSale(Integer sale) { + this.sale = sale; + } + + public Integer getNewStatus() { + return newStatus; + } + + public void setNewStatus(Integer newStatus) { + this.newStatus = newStatus; + } + + public Integer getRecommandStatus() { + return recommandStatus; + } + + public void setRecommandStatus(Integer recommandStatus) { + this.recommandStatus = recommandStatus; + } + + public Integer getStock() { + return stock; + } + + public void setStock(Integer stock) { + this.stock = stock; + } + + public Integer getPromotionType() { + return promotionType; + } + + public void setPromotionType(Integer promotionType) { + this.promotionType = promotionType; + } + + public Integer getSort() { + return sort; + } + + public void setSort(Integer sort) { + this.sort = sort; + } + + public List getAttrValueList() { + return attrValueList; + } + + public void setAttrValueList(List attrValueList) { + this.attrValueList = attrValueList; + } + + static class EsProductAttrValue { + private Long id; + private String value; + + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + } +} diff --git a/mall-search/src/main/java/com/macro/mall/search/repository/EsProductRepository.java b/mall-search/src/main/java/com/macro/mall/search/repository/EsProductRepository.java new file mode 100644 index 0000000..9db35ba --- /dev/null +++ b/mall-search/src/main/java/com/macro/mall/search/repository/EsProductRepository.java @@ -0,0 +1,11 @@ +package com.macro.mall.search.repository; + +import com.macro.mall.search.domain.EsProduct; +import org.springframework.data.elasticsearch.repository.ElasticsearchCrudRepository; + +/** + * 商品ES操作类 + * Created by macro on 2018/6/19. + */ +public interface EsProductRepository extends ElasticsearchCrudRepository { +} diff --git a/mall-search/src/main/java/com/macro/mall/search/service/EsProductService.java b/mall-search/src/main/java/com/macro/mall/search/service/EsProductService.java new file mode 100644 index 0000000..0ae3694 --- /dev/null +++ b/mall-search/src/main/java/com/macro/mall/search/service/EsProductService.java @@ -0,0 +1,12 @@ +package com.macro.mall.search.service; + +/** + * 商品搜索管理Service + * Created by macro on 2018/6/19. + */ +public interface EsProductService { + /** + * 从数据库中导入所有商品到ES + */ + Object importAll(); +} diff --git a/mall-search/src/main/java/com/macro/mall/search/service/impl/EsProductServiceImpl.java b/mall-search/src/main/java/com/macro/mall/search/service/impl/EsProductServiceImpl.java new file mode 100644 index 0000000..c27ec58 --- /dev/null +++ b/mall-search/src/main/java/com/macro/mall/search/service/impl/EsProductServiceImpl.java @@ -0,0 +1,28 @@ +package com.macro.mall.search.service.impl; + +import com.macro.mall.search.dao.EsProductDao; +import com.macro.mall.search.domain.EsProduct; +import com.macro.mall.search.repository.EsProductRepository; +import com.macro.mall.search.service.EsProductService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; + +/** + * 商品搜索管理Service实现类 + * Created by macro on 2018/6/19. + */ +@Service +public class EsProductServiceImpl implements EsProductService { + @Autowired + private EsProductDao productDao; + @Autowired + private EsProductRepository productRepository; + @Override + public Object importAll() { + List esProductList = productDao.getAllEsProductList(); + Iterable esProductIterable = productRepository.save(esProductList); + return esProductIterable; + } +} diff --git a/mall-search/src/main/resources/application.properties b/mall-search/src/main/resources/application.properties index e69de29..4d68062 100644 --- a/mall-search/src/main/resources/application.properties +++ b/mall-search/src/main/resources/application.properties @@ -0,0 +1,18 @@ +#===server start=== +server.port=8081 +#===server end=== + +#===datasource start=== +spring.datasource.url=jdbc:mysql://localhost:3306/mall +spring.datasource.username=root +spring.datasource.password=root +#===datasource end=== + +#===mybatis start=== +mybatis.mapper-locations=classpath:dao/*.xml,classpath*:com/**/mapper/*.xml +#===mybatis end=== + +#===es start=== +spring.data.elasticsearch.repositories.enabled = true +spring.data.elasticsearch.cluster-nodes = 127.0.0.1:9300 +#===es end=== \ No newline at end of file diff --git a/mall-search/src/main/resources/dao/EsProductDao.xml b/mall-search/src/main/resources/dao/EsProductDao.xml new file mode 100644 index 0000000..fe82fba --- /dev/null +++ b/mall-search/src/main/resources/dao/EsProductDao.xml @@ -0,0 +1,33 @@ + + + + + + + + + + \ No newline at end of file diff --git a/mall-search/src/test/java/com/macro/mall/search/MallSearchApplicationTests.java b/mall-search/src/test/java/com/macro/mall/search/MallSearchApplicationTests.java index d605fcd..5852032 100644 --- a/mall-search/src/test/java/com/macro/mall/search/MallSearchApplicationTests.java +++ b/mall-search/src/test/java/com/macro/mall/search/MallSearchApplicationTests.java @@ -1,16 +1,27 @@ package com.macro.mall.search; +import com.macro.mall.search.dao.EsProductDao; +import com.macro.mall.search.domain.EsProduct; import org.junit.Test; import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; +import java.util.List; + @RunWith(SpringRunner.class) @SpringBootTest public class MallSearchApplicationTests { - + @Autowired + private EsProductDao productDao; @Test public void contextLoads() { } + @Test + public void testGetAllEsProductList(){ + List esProductList = productDao.getAllEsProductList(); + System.out.print(esProductList); + } }