elasticsearch?idea中如何创建springbootelasticsearch项目?
2019-07-23 21:16
375 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/Meet_again007/article/details/97035153
图1
创建下图所示的东西
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<User,Integer> { //根据账号查询 List<User> 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<User> users = userRepository.findAll(); users.forEach(System.out::println); } @Test public void testFingByAccount(){ List<User> users = userRepository.findByAccount("昊天至尊"); users.forEach(System.out::println); } }
pox.xml:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.6.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.zhiyou100</groupId> <artifactId>spring-boot-elaticsearch</artifactId> <version>0.0.1-SNAPSHOT</version> <name>spring-boot-elaticsearch</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> </dependency> <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> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
相关文章推荐
- SpringBoot项目通过 spring data elasticsearch使用elasticsearch
- 如何用IDEA创建第一个Spring Boot项目
- idea如何创建springboot项目?
- springboot-redis?idea上如何创建springboot-redis项目?
- idea创建springboot项目(多个模块)
- Idea如何创建SpringBoot工程
- Idea使用Maven创建多模块SpringBoot项目问题记录
- IDEA 创建Spring Boot 项目
- 使用idea创建springboot项目并打成war包发布到tomcat8上
- 使用IDEA创建SpringBoot项目的自定义注解
- idea创建spring boot+mybatis(oracle)+themeleaf项目
- 用Intelij idea 创建 Spring Boot项目一工程项目名称: OpenFirein(1-1)
- 使用IDEA创建springboot项目时,出现错误Cannot download 'https://start.spring.io': connect timed out
- 使用idea创建maven或者spring boot多模块(modul)项目
- idea创建springboot项目(maven)
- 使用IDEA创建一个Spring Boot项目
- 使用idea+gradle创建springboot项目
- Maven项目中pom文件的parent标签报错如何解决 (eclipse中创建springboot项目时,pom中的parent标签报错)
- 三分钟学会用idea创建springboot项目
- IDEA创建基于springboot的多个module聚合项目,并生成war包