SpringBoot2.0之整合ElasticSearch
2024-09-01 07:20:48
就类比数据库到时候去实现
服务器端配置 集群名字 与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);
} }
成功:
查看:
查询:
最新文章
- 第二章 搭建Android开发环境
- Testing - Selenium
- jquery导航二级栏目下拉
- Android实战之你应该使用哪个网络库?
- 第11讲- Android中进程及其优先级
- JSP通过SmartUpload上传文件实例
- cuda vector addition
- CodeForces 618C CodeForces 618C
- JS脚本语言(全称java script:网页里使用的脚本语言:非常强大的语言):基础语法
- MySQL最常用分组聚合函数
- VBS基本语法
- grunt之watch续
- bzoj 2157: 旅游 (LCT 边权)
- java学习笔记(五):公共类
- Xadmin显示视图
- CentOS 删除文件的常用命令
- Spring Cloud Netflix概览和架构设计
- [Objective-C语言教程]多态(26)
- IntelliJ IDEA(2018)安装和破解。
- 基于Cocos2d-x-1.0.1的飞机大战游戏开发实例(下)
热门文章
- 面试心得与总结—BAT、网易、蘑菇街 - ImportNew
- 使用HTML5构建iOS原生APP(2)
- IOS控件:计算文字长度(UITextField,UILabel对象 和 IBAction)
- hdu3625(第一类斯特林数)
- 程序运行时 0xC0000005: 读取位置 0x00000000 时发生访问冲突 ,可能是 com 组件引入各种问题
- python系列十六:Python3 面向对象
- Spring 框架的AOP之注解的方式
- Linux network namespace源码分析
- MySQL读写分离之amoeba
- 单文件快速体验使用react输出hello_world