springboot2.2.2集成6.5 Elasticsearch
1.0POM文件
<!-- spring-boot --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-undertow</artifactId> <scope>compile</scope> </dependency> <!-- elasticSearch --> <!-- es核心jar包 --> <dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-elasticsearch</artifactId> </dependency>
2.YML文件
spring: profiles: active: dev-win redis: database: 0 host: xxx lettuce: pool: max-active: 10 max-idle: 10 max-wait: 5000ms min-idle: 5 password: '' port: 6379 timeout: 3000 session: redis: flush-mode: on_save namespace: clearing store-type: redis timeout: 86400 data: elasticsearch: cluster-nodes: 192.168.1.234:9300 cluster-name: docker-cluster repositories: enabled: true
3.domain
@Document(indexName = "test-boot",type = "goods",shards = 1,replicas = 0, refreshInterval = "-1") @Data public class Elastic implements Serializable { @Id private String id; @Field private String name; @Field private String content; @Field private String price; }
4.Dao
@Component public interface ESDao extends ElasticsearchRepository<Elastic, String> { Elastic queryEmployeeById(String id); }
5.controller
@RestController @RequestMapping("/es") public class ElasticSearchController { @Autowired private ESDao er; //增加 @RequestMapping("/add/{id}") public String add(@PathVariable("id")String id){ Elastic employee=new Elastic(); employee.setId(id); employee.setName("Y.S.K"); employee.setContent("ooo"); employee.setPrice("26"); er.save(employee); System.err.println("add a obj"); return "success"; } //删除 @RequestMapping("/delete") public String delete(){ Elastic employee=new Elastic(); employee.setId("1"); er.delete(employee); return "success"; } //局部更新 @RequestMapping("/update") public String update(){ Elastic employee=er.queryEmployeeById("1"); employee.setName("哈哈"); er.save(employee); System.err.println("update a obj"); return "success"; } //查询 @RequestMapping("/query/{id}") public Elastic query(@PathVariable("id")String id){ Elastic accountInfo=er.queryEmployeeById(id); System.err.println(JSONUtil.parseObj(accountInfo)); return accountInfo; } }
标签:
springboot
, elasticsearch
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!