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

服务器端配置 集群名字  与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);
} }

成功:

查看:

查询:

最新文章

  1. 第二章 搭建Android开发环境
  2. Testing - Selenium
  3. jquery导航二级栏目下拉
  4. Android实战之你应该使用哪个网络库?
  5. 第11讲- Android中进程及其优先级
  6. JSP通过SmartUpload上传文件实例
  7. cuda vector addition
  8. CodeForces 618C CodeForces 618C
  9. JS脚本语言(全称java script:网页里使用的脚本语言:非常强大的语言):基础语法
  10. MySQL最常用分组聚合函数
  11. VBS基本语法
  12. grunt之watch续
  13. bzoj 2157: 旅游 (LCT 边权)
  14. java学习笔记(五):公共类
  15. Xadmin显示视图
  16. CentOS 删除文件的常用命令
  17. Spring Cloud Netflix概览和架构设计
  18. [Objective-C语言教程]多态(26)
  19. IntelliJ IDEA(2018)安装和破解。
  20. 基于Cocos2d-x-1.0.1的飞机大战游戏开发实例(下)

热门文章

  1. 面试心得与总结—BAT、网易、蘑菇街 - ImportNew
  2. 使用HTML5构建iOS原生APP(2)
  3. IOS控件:计算文字长度(UITextField,UILabel对象 和 IBAction)
  4. hdu3625(第一类斯特林数)
  5. 程序运行时 0xC0000005: 读取位置 0x00000000 时发生访问冲突 ,可能是 com 组件引入各种问题
  6. python系列十六:Python3 面向对象
  7. Spring 框架的AOP之注解的方式
  8. Linux network namespace源码分析
  9. MySQL读写分离之amoeba
  10. 单文件快速体验使用react输出hello_world