链接:https://pan.baidu.com/s/1-TsJOUmLez9eOr9SB6R2-A
提取码:g5w8
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.2.8.RELEASE
com.hhf.el
elsearchdemo
0.0.1-SNAPSHOT
elsearchdemo
Demo project for Spring Boot
1.11
org.springframework.boot
spring-boot-starter-data-elasticsearch
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-starter-test
test
org.junit.vintage
junit-vintage-engine
org.springframework.boot
spring-boot-maven-plugin
server.port=8080
server.servlet.context-path=/hhf
spring.elasticsearch.rest.uris=http://localhost:9200
spring.elasticsearch.rest.username=elastic
spring.elasticsearch.rest.password=123456
package com.hhf.el.bean;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import lombok.experimental.Accessors;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import java.io.Serializable;
@Data
@Accessors(chain = true)
@Document(indexName = "blog", type = "java")
public class BlogModel implements Serializable {
private static final long serialVersionUID = 6320548148250372657L;
@Id
private String id;
//@Field(type = FieldType.Keyword)
private String title;
private String content;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
package com.hhf.el.dao;
import com.hhf.el.bean.BlogModel;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
public interface BlogRepository extends ElasticsearchRepository {
}
package com.hhf.el.utils;
public class Result {
private int code;
private String msg;
private T data;
public static Result success() {
return new Result();
}
public static Result success(T data) {
return new Result<>(data);
}
public static Result error(int code, String msg) {
return new Result<>(code, msg);
}
public Result(int code, String msg) {
this.code = code;
this.msg = msg;
this.data = null;
}
public Result(T data) {
this.code = 0;
this.msg = "success";
this.data = data;
}
public Result() {
this.code = 0;
this.msg = "success";
this.data = null;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
}
package com.hhf.el.controller;
import com.hhf.el.bean.BlogModel;
import com.hhf.el.dao.BlogRepository;
import com.hhf.el.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
@RestController
@RequestMapping("/blog")
public class BlogController {
@Autowired
private BlogRepository blogRepository;
@PostMapping("/add")
public Result add(@RequestBody BlogModel blogModel) {
blogRepository.save(blogModel);
return Result.success(blogModel);
}
@GetMapping("/get/{id}")
public Result getById(@PathVariable String id) {
if (StringUtils.isEmpty(id))
return Result.error(1,"id为空!");
Optional blogModelOptional = blogRepository.findById(id);
if (blogModelOptional.isPresent()) {
BlogModel blogModel = blogModelOptional.get();
return Result.success(blogModel);
}
return Result.error(2,"未找到!");
}
@GetMapping("/get")
public Result getAll() {
Iterable iterable = blogRepository.findAll();
List list = new ArrayList<>();
iterable.forEach(list::add);
return Result.success(list);
}
@PostMapping("/update")
public Result updateById(@RequestBody BlogModel blogModel) {
String id = blogModel.getId();
if (StringUtils.isEmpty(id))
return Result.error(1,"id为空!");
blogRepository.save(blogModel);
return Result.success(blogModel);
}
@DeleteMapping("/delete/{id}")
public Result deleteById(@PathVariable String id) {
if (StringUtils.isEmpty(id))
return Result.error(1,"id为空!");
blogRepository.deleteById(id);
return Result.success(id);
}
}
1、添加一条数据:localhost:8080/hhf/blog/add
{
"title": "测试",
"content": "测试一下!"
}
2、根据id查找数据 localhost:8080/hhf/blog/get/zBID4XIB_ZjyQfo7NJh8
3、查询所有
4、根据id更新数据localhost:8080/hhf/blog/update
{
"id":"zBID4XIB_ZjyQfo7NJh8",
"title": "测试",
"content": "测试一下66666666666666!"
}
结果:
5、根据id删除 localhost:8080/hhf/blog/delete/zBID4XIB_ZjyQfo7NJh8
查找已删除的数据: