springboot整合spring data elasticsearch

jpa的特性

jpa可以自定义接口查询,也就意味着自己在mappe按照要求写一个方法,service只需要调用传值就可以了

springboot整合spring data elasticsearch_第1张图片

springboot整合spring data elasticsearch_第2张图片

一,创建maven项目导入依赖


    spring-boot-starter-parent
    org.springframework.boot
    2.3.6.RELEASE


    
        org.springframework.boot
        spring-boot-starter-web
    
    
        org.projectlombok
        lombok
        true
    
    
        org.springframework.boot
        spring-boot-starter-test
        test
    
    
        org.springframework.boot
        spring-boot-starter-data-elasticsearch
    

二,创建application.yml配置文件

spring:
  elasticsearch:
    rest:
      uris: http://localhost:9200

三,创建实体类


@Data
@Document(indexName = "goods")
public class Goods implements Serializable {

    @Field(type = FieldType.Keyword)
    private String id;
    @Field(type = FieldType.Text)
    private String goodsName;
    @Field(type = FieldType.Integer)
    private Integer store;
    @Field(type =FieldType.Double)
    private double price;
}

四,创建dao层



@Repository
public interface GoodsDao extends ElasticsearchRepository {
}

五,创建主启动类


@SpringBootApplication
public class ElasticsearchApplication {
    public static void main(String[] args) {
        SpringApplication.run(ElasticsearchApplication.class, args);
    }
}

六,注入测试


@RunWith(SpringRunner.class)
@SpringBootTest(classes = ElasticsearchApplication.class)
public class ElastricsearchTest {

    //注入dao
    @Autowired
    private GoodsDao goodsDao;


    /**
    * @Description: 插入一个
    * @Author: Mr.Zhan
    * @Date: 2022/2/14
    */
       @Test
       public  void  testCreate(){
           Goods goods = new Goods("1","springoot整合",12,300.0);
           goodsDao.save(goods);

       }

       /**
       * @Description: 插入多个
       * @Author: Mr.Zhan
       * @Date: 2022/2/14
       */
          @Test
          public  void  testCerateMany(){
              ArrayList list = new ArrayList<>();
              Goods goods = new Goods("2","springoot整合2",12,300.0);
              Goods goods1 = new Goods("3","springoot整合3",12,300.0);
              list.add(goods);
              list.add(goods1);
              goodsDao.saveAll(list);
          }
          /**
          * @Description: 根据id查询
          * @Author: Mr.Zhan
          * @Date: 2022/2/14
          */
             @Test
             public  void  testFindById(){
                 Optional goods = goodsDao.findById("2");
                 System.out.println("goods.get() = " + goods.get());
             }

             /**
             * @Description: 根据所有查询
             * @Author: Mr.Zhan
             * @Date: 2022/2/14
             */
                @Test
                public  void  testFindAll(){
                    goodsDao.findAll().forEach((z)-> System.out.println("z = " + z));
                }


                /**
                * @Description: 修改
                * @Author: Mr.Zhan
                * @Date: 2022/2/14
                */
                   @Test
                   public  void  testUpdate(){
                       Goods goods = new Goods("3","springoot整合2",12,200.0);
                       goodsDao.save(goods);
                   }

                   /**
                   * @Description: 排序查询
                   * @Author: Mr.Zhan
                   * @Date: 2022/2/14
                   */
                      @Test
                      public  void  testSort(){
                          ArrayList  list= new ArrayList<>();
                          list.add("price");
                          Sort orders = new Sort(Sort.Direction.ASC,list);
                          goodsDao.findAll(orders);
                      }

                      /**
                      * @Description: 分页查询
                      * @Author: Mr.Zhan
                      * @Date: 2022/2/14
                      */
                         @Test
                         public  void  testPageResult(){
                             Pageable pageable = Pageable.unpaged();
                             goodsDao.findAll(Pageable.unpaged()).get().forEach((s)-> System.out.println("s = " + s));
                         }
}

你可能感兴趣的:(springboot整合,spring,boot,jenkins,后端)