提交 d1e687da 编写于 作者: Z zhh

添加商品导入es功能

上级 f960972e
......@@ -38,12 +38,28 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--MyBatis分页插件-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.3</version>
</dependency>
<!--Swagger-UI API文档生产工具-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
</dependency>
</dependencies>
<build>
......
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) {
......
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();
}
}
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();
}
}
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<EsProduct> getAllEsProductList();
}
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<EsProductAttrValue> 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<EsProductAttrValue> getAttrValueList() {
return attrValueList;
}
public void setAttrValueList(List<EsProductAttrValue> 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;
}
}
}
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<EsProduct,Long> {
}
package com.macro.mall.search.service;
/**
* 商品搜索管理Service
* Created by macro on 2018/6/19.
*/
public interface EsProductService {
/**
* 从数据库中导入所有商品到ES
*/
Object importAll();
}
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<EsProduct> esProductList = productDao.getAllEsProductList();
Iterable<EsProduct> esProductIterable = productRepository.save(esProductList);
return esProductIterable;
}
}
#===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
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.macro.mall.search.dao.EsProductDao">
<resultMap id="esProductListMap" type="com.macro.mall.search.domain.EsProduct" autoMapping="true">
<id column="id" jdbcType="BIGINT" property="id" />
<collection property="attrValueList" columnPrefix="attr_" resultMap="com.macro.mall.mapper.PmsProductAttributeValueMapper.BaseResultMap">
</collection>
</resultMap>
<select id="getAllEsProductList" resultMap="esProductListMap">
select
p.id id,
p.product_sn productSn,
p.brand_id brandId,
p.brand_name brandName,
p.product_category_id productCategoryId,
p.product_category_name productCategoryName,
p.pic pic,
p.name name,
p.sub_title subTitle,
p.price price,
p.sale sale,
p.new_status newStatus,
p.recommand_status recommandStatus,
p.stock stock,
p.promotion_type promotionType,
p.sort sort,
a.id attr_id,
a.value attr_value
from pms_product p
left join pms_product_attribute_value a on p.id = a.product_id
where delete_status = 0 and publish_status = 1
</select>
</mapper>
\ No newline at end of file
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<EsProduct> esProductList = productDao.getAllEsProductList();
System.out.print(esProductList);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册