当前位置:   article > 正文

SpringBoot集成Elasticsearch并进行增删改查操作_elasticsearchrepository更新

elasticsearchrepository更新

今天给大家简单的介绍一下SpringBoot如何集成Elasticsearch,并简单的介绍一下基于SpringBoot模式下怎么进行简单的增删改查操作,这边增删改查操作有点类似于JPA的模式。(什么是JPA模式,大家可以自行搜索答案)

史上最全Java技术栈面试题都在这里面了:Java面试题

废话不多说,现在马上开始我们今天的内容。如何新建Springboot项目我这边就不废话了,不会的同学可以看我以前写的教程。

文章地址:IDEA上面如何创建SpringBoot项目-CSDN博客

1.首先是引入相关的依赖,下面是我的pom文件。

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>com.elasticsearch</groupId>
  6. <artifactId>elasticsearch</artifactId>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <packaging>jar</packaging>
  9. <name>ElasticSearch</name>
  10. <description>ElasticSearch project for Spring Boot</description>
  11. <parent>
  12. <groupId>org.springframework.boot</groupId>
  13. <artifactId>spring-boot-starter-parent</artifactId>
  14. <version>2.0.2.RELEASE</version>
  15. <relativePath/> <!-- lookup parent from repository -->
  16. </parent>
  17. <properties>
  18. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  19. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  20. <java.version>1.8</java.version>
  21. </properties>
  22. <dependencies>
  23. <!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
  24. <dependency>
  25. <groupId>com.google.code.gson</groupId>
  26. <artifactId>gson</artifactId>
  27. <version>2.8.0</version>
  28. </dependency>
  29. <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-elasticsearch -->
  30. <dependency>
  31. <groupId>org.springframework.boot</groupId>
  32. <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
  33. <version>2.0.2.RELEASE</version>
  34. </dependency>
  35. <dependency>
  36. <groupId>org.springframework.boot</groupId>
  37. <artifactId>spring-boot-starter-web</artifactId>
  38. </dependency>
  39. <dependency>
  40. <groupId>org.springframework.boot</groupId>
  41. <artifactId>spring-boot-starter-test</artifactId>
  42. <scope>test</scope>
  43. </dependency>
  44. </dependencies>
  45. <build>
  46. <plugins>
  47. <plugin>
  48. <groupId>org.springframework.boot</groupId>
  49. <artifactId>spring-boot-maven-plugin</artifactId>
  50. </plugin>
  51. </plugins>
  52. </build>
  53. </project>

pom.xml文件中最重要的其实就是引入ES(Elasticsearch的简称后面我都这么叫),也就是spring-boot-starter-data-elasticsearch 依赖

2.接下来就是对应的配置文件了,具体配置文件如下所示:

  1. # elasticsearch集群名称,默认的是elasticsearch
  2. spring.data.elasticsearch.cluster-name=my-application
  3. #节点的地址 注意api模式下端口号是9300,千万不要写成9200
  4. spring.data.elasticsearch.cluster-nodes=192.168.11.24:9300
  5. #是否开启本地存储
  6. spring.data.elasticsearch.repositories.enable=true

3.索引对应的实体类如下所示:

  1. package com.elasticsearch.entity;
  2. import org.springframework.data.annotation.Id;
  3. import org.springframework.data.elasticsearch.annotations.Document;
  4. import org.springframework.data.elasticsearch.annotations.Field;
  5. /**
  6. * @author linzhiqiang
  7. * @date 2018/5/16
  8. */
  9. @Document(indexName = "company",type = "employee", shards = 1,replicas = 0, refreshInterval = "-1")
  10. public class Employee {
  11. @Id
  12. private String id;
  13. @Field
  14. private String firstName;
  15. @Field
  16. private String lastName;
  17. @Field
  18. private Integer age = 0;
  19. @Field
  20. private String about;
  21. public String getId() {
  22. return id;
  23. }
  24. public void setId(String id) {
  25. this.id = id;
  26. }
  27. public String getFirstName() {
  28. return firstName;
  29. }
  30. public void setFirstName(String firstName) {
  31. this.firstName = firstName;
  32. }
  33. public String getLastName() {
  34. return lastName;
  35. }
  36. public void setLastName(String lastName) {
  37. this.lastName = lastName;
  38. }
  39. public Integer getAge() {
  40. return age;
  41. }
  42. public void setAge(Integer age) {
  43. this.age = age;
  44. }
  45. public String getAbout() {
  46. return about;
  47. }
  48. public void setAbout(String about) {
  49. this.about = about;
  50. }
  51. }

4.实体类对应的dao接口如下所示:

  1. package com.elasticsearch.dao;
  2. /**
  3. * Created by 19130 on 2018/5/16.
  4. */
  5. import com.elasticsearch.entity.Employee;
  6. import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
  7. import org.springframework.stereotype.Component;
  8. /**
  9. * @author linzhiqiang
  10. */
  11. @Component
  12. public interface EmployeeRepository extends ElasticsearchRepository<Employee,String>{
  13. /**
  14. * 查询雇员信息
  15. * @param id
  16. * @return
  17. */
  18. Employee queryEmployeeById(String id);
  19. }

5.实体类对应的控制类如下所示:

  1. package com.elasticsearch.controller;
  2. import com.elasticsearch.dao.EmployeeRepository;
  3. import com.elasticsearch.entity.Employee;
  4. import com.google.gson.Gson;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. import org.springframework.web.bind.annotation.RestController;
  8. /**
  9. * @author linzhiqiang
  10. */
  11. @RestController
  12. @RequestMapping("es")
  13. public class EmployeeController {
  14. @Autowired
  15. private EmployeeRepository employeeRepository;
  16. /**
  17. * 添加
  18. * @return
  19. */
  20. @RequestMapping("add")
  21. public String add() {
  22. Employee employee = new Employee();
  23. employee.setId("1");
  24. employee.setFirstName("xuxu");
  25. employee.setLastName("zh");
  26. employee.setAge(26);
  27. employee.setAbout("i am in peking");
  28. employeeRepository.save(employee);
  29. System.err.println("add a obj");
  30. return "success";
  31. }
  32. /**
  33. * 删除
  34. * @return
  35. */
  36. @RequestMapping("delete")
  37. public String delete() {
  38. Employee employee = employeeRepository.queryEmployeeById("1");
  39. employeeRepository.delete(employee);
  40. return "success";
  41. }
  42. /**
  43. * 局部更新
  44. * @return
  45. */
  46. @RequestMapping("update")
  47. public String update() {
  48. Employee employee = employeeRepository.queryEmployeeById("1");
  49. employee.setFirstName("哈哈");
  50. employeeRepository.save(employee);
  51. System.err.println("update a obj");
  52. return "success";
  53. }
  54. /**
  55. * 查询
  56. * @return
  57. */
  58. @RequestMapping("query")
  59. public Employee query() {
  60. Employee accountInfo = employeeRepository.queryEmployeeById("1");
  61. System.err.println(new Gson().toJson(accountInfo));
  62. return accountInfo;
  63. }
  64. }

以上所有的增删改查操作都要基于搭建好的ES系统(关于Linux上面如何搭建ES系统大家可以自行google)

关于我踩过的坑:

1.ES中API的端口号是9300而不是9200。

2.ES系统中Elasticsearch.yml配置文件中要加入network.host: 0.0.0.0,否则外网地址访问不了。

3.最新的资料一定要去官网上面查看,博客上面好多都是过时的。官网地址:Elasticsearch Platform — Find real-time answers at scale | Elastic

4.注意JDK、ES、Springboot三者之间的版本,很多时候错误都是版本冲突引起的。

下一节课程将会给大家介绍一下,Java是如何通过ES提供的API进行索引的增删改查操作。(和今天的模式不一样哦

对文章有什么疑问或者想要看更多文章可以加我订阅号,欢迎大家的踩踩~

 
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/68775
推荐阅读
相关标签
  

闽ICP备14008679号