springboot整合spring data elasticsearch

发布时间:2024年01月17日

jpa的特性

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

一,创建maven项目导入依赖

<parent>
    <artifactId>spring-boot-starter-parent</artifactId>
    <groupId>org.springframework.boot</groupId>
    <version>2.3.6.RELEASE</version>
</parent>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
    </dependency>
</dependencies>

二,创建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<Goods,String> {
}

五,创建主启动类


@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<Goods> 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> 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<String>  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));
                         }
}

文章来源:https://blog.csdn.net/weixin_45766875/article/details/135642834
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。