SpringBoot 集成 Elasticsearch 并进行增删改查操作,java 引用类型和基本类型的区别面试
2.接下来就是对应的配置文件了,具体配置文件如下所示:
elasticsearch 集群名称,默认的是 elasticsearch
spring.data.elasticsearch.cluster-name=my-application
#节点的地址 注意 api 模式下端口号是 9300,千万不要写成 9200
spring.data.elasticsearch.cluster-nodes=192.168.11.24:9300
#是否开启本地存储
spring.data.elasticsearch.repositories.enable=true
3.索引对应的实体类如下所示:
package com.elasticsearch.entity;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
/**
@author linzhiqiang
@date 2018/5/16
*/
@Document(indexName = "company",type = "employee", shards = 1,replicas = 0, refreshInterval = "-1")
public class Employee {
@Id
private String id;
@Field
private String firstName;
@Field
private String lastName;
@Field
private Integer age = 0;
@Field
private String about;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getL
《一线大厂 Java 面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》
【docs.qq.com/doc/DSmxTbFJ1cmN1R2dB】 完整内容开源分享
astName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getAbout() {
return about;
}
public void setAbout(String about) {
this.about = about;
}
}
4.实体类对应的 dao 接口如下所示:
package com.elasticsearch.dao;
/**
Created by 19130 on 2018/5/16.
*/
import com.elasticsearch.entity.Employee;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Component;
/**
@author linzhiqiang
*/
@Component
public interface EmployeeRepository extends ElasticsearchRepository<Employee,String>{
/**
查询雇员信息
@param id
@return
*/
Employee queryEmployeeById(String id);
}
5.实体类对应的控制类如下所示:
package com.elasticsearch.controller;
import com.elasticsearch.dao.EmployeeRepository;
import com.elasticsearch.entity.Employee;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
@author linzhiqiang
*/
@RestController
@RequestMapping("es")
public class EmployeeController {
@Autowired
private EmployeeRepository employeeRepository;
/**
添加
@return
*/
@RequestMapping("add")
public String add() {
Employee employee = new Employee();
employee.setId("1");
employee.setFirstName("xuxu");
employee.setLastName("zh");
employee.setAge(26);
employee.setAbout("i am in peking");
employeeRepository.save(employee);
System.err.println("add a obj");
return "success";
}
/**
删除
@return
*/
@RequestMapping("delete")
public String delete() {
Employee employee = employeeRepository.queryEmployeeById("1");
employeeRepository.delete(employee);
return "success";
}
/**
局部更新
@return
*/
@RequestMapping("update")
public String update() {
Employee employee = employeeRepository.queryEmployeeById("1");
employee.setFirstName("哈哈");
employeeRepository.save(employee);
System.err.println("update a obj");
return "success";
}
/**
查询
@return
*/
@RequestMapping("query")
public Employee query() {
Employee accountInfo = employeeRepository.queryEmployeeById("1");
System.err.println(new Gson().toJson(accountInfo));
return accountInfo;
}
}
以上所有的增删改查操作都要基于搭建好的 ES 系统(关于 Linux 上面如何搭建 ES 系统大家可以自行 google)
关于我踩过的坑:
1.ES 中 API 的端口号是 9300 而不是 9200。
2.ES 系统中 Elasticsearch.yml 配置文件中要加入 network.host: 0.0.0.0,否则外网地址访问不了。
3.最新的资料一定要去官网上面查看,博客上面好多都是过时的。官网地址:https://www.elastic.co
我的面试宝典:一线互联网大厂 Java 核心面试题库
以下是我个人的一些做法,希望可以给各位提供一些帮助:
整理了很长一段时间,拿来复习面试刷题非常合适,其中包括了 Java 基础、异常、集合、并发编程、JVM、Spring 全家桶、MyBatis、Redis、数据库、中间件 MQ、Dubbo、Linux、Tomcat、ZooKeeper、Netty 等等,且还会持续的更新...可 star 一下!

283 页的 Java 进阶核心 pdf 文档
Java 部分:Java 基础,集合,并发,多线程,JVM,设计模式
数据结构算法:Java 算法,数据结构
开源框架部分:Spring,MyBatis,MVC,netty,tomcat
分布式部分:架构设计,Redis 缓存,Zookeeper,kafka,RabbitMQ,负载均衡等
微服务部分:SpringBoot,SpringCloud,Dubbo,Docker

还有源码相关的阅读学习

评论