SpringBoot2.0之整合ElasticSearch

就类比数据库到时候去实现

服务器端配置 集群名字  与yml名字一致

pom:

<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>
  <groupId>com.toov5</groupId>
  <artifactId>springboot-es</artifactId>
  <version>0.0.1-SNAPSHOT</version>

      <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- springboot 整合es -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>

</project>

项目结构:

Entity:

package com.toov5.entity;

import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;

import lombok.Data;

 @Document(indexName="toov5",type="user")   //索引的名字  类型
 @Data
public class UserEntity {
  @Id
  private String id;
  private String name;
  private Integer age;
  private Integer sex;
}

Dao:

package com.toov5.dao;

import org.springframework.data.repository.CrudRepository;

import com.toov5.entity.UserEntity;

public interface UserDao extends CrudRepository<UserEntity, String> {

}

Controller:

package com.toov5.controller;

import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.toov5.dao.UserDao;
import com.toov5.entity.UserEntity;

@RestController
public class EsController {

    @Autowired
    private UserDao userDao;

    //添加文档
    @RequestMapping("/addUser")
    public UserEntity addUser(@RequestBody UserEntity userEntity) {
      return userDao.save(userEntity);
    } 

    //查询文档
    @RequestMapping("/findById")
    public Optional<UserEntity> findById(String id) {
       return userDao.findById(id);
    }

}

成功:

查看:

查询:

原文地址:https://www.cnblogs.com/toov5/p/10296397.html

时间: 2024-07-30 10:27:56

SpringBoot2.0之整合ElasticSearch的相关文章

每天学点SpringCloud(一):使用SpringBoot2.0.3整合SpringCloud

最近开始学习SpringCloud,在此把我学习的过程记录起来,跟大家分享一下,一起学习.想学习SpringCloud的同学赶快上车吧. 本次学习使用得SpringBoot版本为2.0.3.RELEASE,SpringCloud版本为Finchley.RELEASE 创建父Maven工程首先我们创建一个Maven项目 我们把此项目当做我们项目的父项目,以后我们所有的子项目都应该继承这个项目,来看一下此项目的pom.xml文件都是依赖了什么 <?xml version="1.0"

SpringBoot2.X整合elasticsearch&#39;

SpringBoot默认支持两种技术来和ES交互: 创建项目需要引入ES的启动器 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId></dependency> <!--集合工具包--><dependency> <gro

SpringBoot(2.0.4.RELEASE)+Elasticsearch(6.2.4)+Gradle简单整合

记录一下SpringBoot(2.0.4.RELEASE)+Elasticsearch(6.2.4)+Gradle整合的一个小例子. 1.在Gradle内加入相关jar包的依赖: compile('org.springframework.boot:spring-boot-starter-web') compile('org.springframework.boot:spring-boot-starter-thymeleaf') compile('org.springframework.boot:

spring boot 2.0 整合 elasticsearch NoNodeAvailableException

原文地址:spring boot 2.0 整合 elasticsearch NoNodeAvailableException 原文说的有点问题,下面贴出我的配置: 码云项目地址:https://gitee.com/11230595/springboot-elasticsearch elasticsearch.yml cluster.name: my-applicationnetwork.host: 0.0.0.0 http.port: 9200transport.tcp.port: 9300tr

SpringBoot2.0源码分析(二):整合ActiveMQ分析

SpringBoot具体整合ActiveMQ可参考:SpringBoot2.0应用(二):SpringBoot2.0整合ActiveMQ ActiveMQ自动注入 当项目中存在javax.jms.Message和org.springframework.jms.core.JmsTemplate着两个类时,SpringBoot将ActiveMQ需要使用到的对象注册为Bean,供项目注入使用.一起看一下JmsAutoConfiguration类. @Configuration @Conditional

SpringBoot2.0应用(三):SpringBoot2.0整合RabbitMQ

如何整合RabbitMQ 1.添加spring-boot-starter-amqp <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency> 2.添加配置 spring.rabbitmq.host=localhost spring.rabbitmq.po

SpringBoot2.0+Shiro+JWT 整合

SpringBoot2.0+Shiro+JWT 整合 JSON Web Token(JWT)是一个非常轻巧的规范.这个规范允许我们使用 JWT 在用户和服务器之间传递安全可靠的信息. 我们利用一定的编码生成 Token,并在 Token 中加入一些非敏感信息,将其传递. 安装环境 开发工具:STS Maven版本:apache-maven-3.5.2 java jdk 1.8 MySQL版本:5.7 系统:Windows10 一.新建Maven项目 配置Maven项目的pom.xml文件 <pr

SpringBoot2.0之四 简单整合MyBatis

从最开始的SSH(Struts+Spring+Hibernate),到后来的SMM(SpringMVC+Spring+MyBatis),到目前的S(SpringBoot),随着框架的不断更新换代,也为我们广大的程序猿提供了更多的方便,一起搭建一个从控制层到持久层的项目可能需要一两天的时间,但是采用SpringBoot的方式,我们可能只需要10分钟就能轻松完成一个web项目的搭建,下面我们介绍一下SpringBoot2.0整合MyBatis的方法 一.新建一个项目,引入相关依赖 <!-- 单元测试

SpringBoot2.0整合fastjson的正确姿势

SpringBoot2.0如何集成fastjson?在网上查了一堆资料,但是各文章的说法不一,有些还是错的,可能只是简单测试一下就认为ok了,最后有没生效都不知道.恰逢公司项目需要将JackSon换成fastjson,因此自己来实践一下SpringBoot2.0和fastjson的整合,同时记录下来方便自己后续查阅. 一.Maven依赖说明 SpringBoot的版本为: <version>2.1.4.RELEASE</version> 在pom文件中添加fastjson的依赖: