elasticsearch?idea中如何创建springbootelasticsearch项目?

图1

elasticsearch?idea中如何创建springbootelasticsearch项目?_第1张图片

创建下图所示的东西

elasticsearch?idea中如何创建springbootelasticsearch项目?_第2张图片

User 内容:

package com.zhiyou100.springbootelaticsearch.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
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;


@Data
@NoArgsConstructor
@AllArgsConstructor
@Document(indexName = "user",type = "aaa")
public class User {

    @Id
    @Field(type = FieldType.Integer)
    private Integer id;

    @Field(type = FieldType.Text)
    private String account;

    @Field(type = FieldType.Keyword)
    private String password;
}

UserRepository内容:

package com.zhiyou100.springbootelaticsearch.Repository;

import com.zhiyou100.springbootelaticsearch.entity.User;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

import java.util.List;

public interface UserRepository extends ElasticsearchRepository {
    //根据账号查询
    List findByAccount(String account);
}

application.properties:

spring.profiles.active=dev

application-dev.yml:

spring:
  data:
    elasticsearch:
      cluster-name: elasticsearch
      cluster-nodes: 192.168.14.167:9300

UserRepositoryTest内容

package com.zhiyou100.springbootelaticsearch.Repository;

import com.zhiyou100.springbootelaticsearch.entity.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.omg.CORBA.PUBLIC_MEMBER;
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;

import static org.junit.Assert.*;
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserRepositoryTest {
    @Autowired
    private UserRepository userRepository;

    @Test
    public void addUser() {
        //测试添加
        User user = new User((int) (Math.random() * 200000 + 1), "昊天至尊", "0000");
        userRepository.save(user);
        System.out.println(user);
    }
    @Test
    public  void testFindAll(){
        Iterable users = userRepository.findAll();
        users.forEach(System.out::println);
    }

    @Test
    public void testFingByAccount(){
        List users = userRepository.findByAccount("昊天至尊");
        users.forEach(System.out::println);
    }
}

pox.xml:

	

    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.6.RELEASE
         
    
    com.zhiyou100
    spring-boot-elaticsearch
    0.0.1-SNAPSHOT
    spring-boot-elaticsearch
    Demo project for Spring Boot

    
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-data-elasticsearch
        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


你可能感兴趣的:(SpringBoot,elastcisearch)