SpringBoot(2.0.4.RELEASE)+Elasticsearch(6.2.4)+Gradle简单整合

记录一下SpringBoot(2.0.4.RELEASE)+Elasticsearch(6.2.4)+Gradle整合的一个小例子。

1.在Gradle内加入相关jar包的依赖:

compile(‘org.springframework.boot:spring-boot-starter-web‘)
compile(‘org.springframework.boot:spring-boot-starter-thymeleaf‘)
compile(‘org.springframework.boot:spring-boot-starter-data-jpa‘)

//添加Spring Data Elasticsearch依赖
compile(‘org.springframework.boot:spring-boot-starter-data-elasticsearch‘)

//添加JNA依赖
compile(‘net.java.dev.jna:jna:4.3.0‘)

compile(‘com.google.guava:guava:26.0-jre‘)

2.创建实体对象,并加入Elasticsearch的相关注释:

package com.wey.pojo.blog;

import java.io.Serializable;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;

@Document(indexName="blogcenter",type="blog")
//indexName索引名称 可以理解为数据库名 必须为小写不然会报
public class Blog implements Serializable{

    private static final long serialVersionUID = 1L;

    @Id
    private String id;
    private String title;
    private String summary;
    private String content;

    protected Blog() {
        super();
    }

    public Blog(String title, String summary, String content) {
        this.title = title;
        this.summary = summary;
        this.content = 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 getSummary() {
        return summary;
    }

    public void setSummary(String summary) {
        this.summary = summary;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    @Override
    public String toString() {
        return "Blog [id=" + id + ", title=" + title + ", summary=" + summary + ", content=" + content + "]";
    }
}

3.创建Repository

package com.wey.repository;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Component;

import com.wey.pojo.blog.Blog;

public interface BlogRepository extends ElasticsearchRepository<Blog, String> {
}

4.创建Controller并简单的实现添加及查询

@RestController
@RequestMapping("/blogs")
public class BlogController {
     @Autowired
     BlogRepository blogRepository;

    @RequestMapping("/add")
    public Blog add(Blog blog) {
        return blogRepository.save(blog);
    }   

    @GetMapping
    public List<Blog> findAll(){
        Iterable<Blog> elements = blogRepository.findAll();
        ArrayList<Blog> list = Lists.newArrayList(elements);
        return list;
    }

    @GetMapping("/delete/{id}")
    public String remove(@PathVariable(name="id") String id) {
        blogRepository.deleteById(id);
        return "success";
    }
}

5.打开下载好的Elasticsearch(6.2.4)内的elasticsearch.bat文件,等待一会儿直到启动完成。

6.启动SpringBoot应用并简单的测试

添加一条数据:

查询所有数据:

原文地址:https://www.cnblogs.com/foxting/p/9545206.html

时间: 2024-11-06 07:23:48

SpringBoot(2.0.4.RELEASE)+Elasticsearch(6.2.4)+Gradle简单整合的相关文章

springboot gradle简单整合mongodb随记

mongodb与redis的比较:参考https://www.cnblogs.com/chinesern/p/5581422.html 1.安装mongodb服务 参考:http://blog.csdn.net/IT_wanghe/article/details/53884229    不多做说明 启动命令:E:/mongodb/bin>mongod --dbpath E:\mongodb\data\db        自定义安装目录 2.启动mongodb 3.日常添加依赖 4.添加实体类 

spring-boot 2.0 多模块化项目和EurekaServer的搭建

Spring boot由于其 1.易于开发和维护.2.单个微服务启动快.3.局部修改部署容易.4.技术栈不受语言限制等优点受到越来越多公司的重视.spring-boot还集成了许多关于微服务开发的框架(例如配置管理,服务发现,断路器,智能路由,微代理,控制总线,一次性令牌,全局锁,领导选举,分布式 会话,群集状态),使我们部署微服务免去了繁琐的配置. 下面我们来学习利用spring-boot搭建电商项目. Spring Boot 2.0.0-SNAPSHOT 要求 Java 8 和 Spring

001-Spring Cloud Edgware.SR3 升级最新 Finchley.SR1,spring boot 1.5.9.RELEASE 升级2.0.4.RELEASE注意问题点

一.前提 升级前 => 升级后 Spring Boot 1.5.x => Spring Boot 2.0.4.RELEASE Spring Cloud Edgware SR3 => Spring Cloud Finchley.SR1 1.1.Eureka Server ureka Server 依赖更新 升级前: <dependency> <groupId>org.springframework.cloud</groupId> <artifact

基本springboot 2.0版本 spring-cloud的使用

Spring Cloud与Spring Boot版本匹配关系 Spring Cloud Spring Boot Finchley 兼容Spring Boot 2.0.x,不兼容Spring Boot 1.5.x Dalston和Edgware 兼容Spring Boot 1.5.x,不兼容Spring Boot 2.0.x Camden 兼容Spring Boot 1.4.x,也兼容Spring Boot 1.5.x Brixton 兼容Spring Boot 1.3.x,也兼容Spring B

Springboot 2.0.0单元测试

1. 引入spring-boot-starter-test包 1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="

SpringBoot 2.1.1.RELEASE 集成MyBatis

SpringBoot 2.1.1.RELEASE 集成MyBatismaven工程:详细配置见:http://www.qchcloud.cn/system/article/show/63pom.xml <?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www

SpringBoot 2.1.1.RELEASE 集成Druid

SpringBoot 2.1.1.RELEASE 集成Druid详情:http://www.qchcloud.cn/system/article/show/68配置依赖: mysql mysql-connector-java com.alibaba druid 1.1.4 配置applicaton.properties spring.datasource.driverClassName = com.mysql.cj.jdbc.Driver spring.datasource.url = jdbc

SpringBoot 2.1.1.RELEASE 集成quartz

SpringBoot 2.1.1.RELEASE 集成quartzhttp://www.qchcloud.cn/system/article/show/70依赖配置: <!-- 定时任务 --> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> </dependency> 定时任务: @Component

SpringBoot 2.1.1.RELEASE集成devtools

SpringBoot 2.1.1.RELEASE集成devtoolshttp://www.qchcloud.cn/system/article/show/74引入依赖: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> &