springboot+elasticsearch基本使用

  1. 导入maven坐标

     		<dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
            </dependency>
    
  2. application.properties配置文件中配置

    #elasticsearch配置
    spring.data.elasticsearch.cluster-name = elasticsearch
    spring.data.elasticsearch.cluster-nodes = 127.0.0.1:9300
    
  3. 创建dao

    public interface ProductResiporty extends ElasticsearchRepository<Productpojo,Integer> {
    
    }
    
    
  4. 创建pojo类

    package com.czxy.web.repertory;
    
    import org.springframework.data.annotation.Id;
    import org.springframework.data.elasticsearch.annotations.Document;
    import org.springframework.data.elasticsearch.annotations.Field;
    import org.springframework.data.elasticsearch.annotations.FieldType;
    import org.springframework.format.annotation.DateTimeFormat;
    
    import javax.persistence.Column;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import java.util.Date;
    
    
    @Document(indexName = "promition",type = "tt")
    
    public class Productpojo {
        @Id
        private Integer id;
        
        @Field(type = FieldType.Auto )
        private String titleImg; // 宣传图片
    
        @Field(type = FieldType.Keyword )
        private String activeScope;// 活动范围
    
        @Field(type = FieldType.Date )
        private Date endDate; // 失效时间
        
        @Field(type = FieldType.Text )
        private String updateUser;// 更新人 后续与后台用户关联
    
        @Field(type = FieldType.Integer )
        private String status = "1"; // 状态 可取值:0 未开始  1.进行中 2. 已结束
    
    	//省略setter个getter方法
    
    
  5. controller层同步数据库中的数据到elasticsearch中

    package com.czxy.web.repertory;
    
    import com.czxy.dao.text_delivery.PromotionMapper;
    import com.czxy.domain.teke_delivery.Promotion;
    
    import org.springframework.beans.BeanUtils;
    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.ArrayList;
    import java.util.List;
    
    @RestController
    @RequestMapping("/estest")
    public class SpringTaskController {
        @Autowired
        private PromotionMapper productMapper;
    
        @Autowired
        private ProductResiporty productResiporty;
    
       @GetMapping("/dj")
        public void DjTask() {
            List<Promotion> products = productMapper.selectAll();
            List<Productpojo> list = new ArrayList<>();
    
            for (Promotion product : products) {
                Productpojo productpojo = new Productpojo();
                BeanUtils.copyProperties(product, productpojo);
                list.add(productpojo);
            }
            productResiporty.saveAll(list);
        }
    
    
    
    }
    
    

猜你喜欢

转载自blog.csdn.net/qq_42917455/article/details/84197170