SpringBoot
【黑马程序员SpringBoot2全套视频教程,springboot零基础到项目实战(spring boot2完整版)】
SpringBoot 开发实用篇
文章目录
- SpringBoot
- SpringBoot 开发实用篇
- 4 数据层解决方案
- 4.16 SpringBoot 整合 ES 客户端操作
- 4.16.1 环境准备
- 4.16.2 整合ES
- 4.16.3 小结
4 数据层解决方案
4.16 SpringBoot 整合 ES 客户端操作
4.16.1 环境准备
创建一个新的模块工程
依赖一个也不勾,直接创建
OK, 一个全新的SpringBoot 工程
导入相关依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.11</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
就这四个
创建一个Book实体类
package com.dingjiaxiong.domain;
import lombok.Data;
/**
* ClassName: Book
* date: 2022/10/20 19:49
*
* @author DingJiaxiong
*/
@Data
public class Book {
private Integer id;
private String type;
private String name;
private String description;
}
创建dao
package com.dingjiaxiong.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.dingjiaxiong.domain.Book;
import org.apache.ibatis.annotations.Mapper;
/**
* ClassName: BookDao
* date: 2022/10/20 19:51
*
* @author DingJiaxiong
*/
@Mapper
public interface BookDao extends BaseMapper<Book> {
}
配置
spring:
datasource:
druid:
url: jdbc:mysql://localhost:3306/ssm_db?serverTimezone=UTC
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: 200039
mybatis-plus:
global-config:
db-config:
table-prefix: tbl_
id-type: auto
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
就是这样,测试一下
package com.dingjiaxiong;
import com.dingjiaxiong.dao.BookDao;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class Springboot18EsApplicationTests {
@Autowired
private BookDao bookDao;
@Test
void fn(){
bookDao.selectById(1);
}
}
OK,没问题
4.16.2 整合ES
【导入坐标】
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
【配置】
elasticsearch:
uris: http://localhost:9200
现在就可以进行测试了
当然【这以上的操作都是…过时的,低版本的】【我真的会谢】
重新来
【导入坐标】
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
</dependency>
【修改配置】
不写,甚至uri 都不告诉springboot,就得自己硬编码…离谱【我感觉现在或许有了,但是笔者就不研究了】
再次开始测试
我giao,已经过时了【MD,咋办,硬用】
package com.dingjiaxiong;
import com.dingjiaxiong.dao.BookDao;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class Springboot18EsApplicationTests {
private RestHighLevelClient client;
@Test
void testCreateClient(){
HttpHost host = HttpHost.create("http://localhost:9200");
RestClientBuilder builder = RestClient.builder(host);
client = new RestHighLevelClient(builder);
}
}
运行结果
没报错,说明创建成功了
创建索引
@Test
void testCreateIndex() throws IOException {
HttpHost host = HttpHost.create("http://localhost:9200");
RestClientBuilder builder = RestClient.builder(host);
client = new RestHighLevelClient(builder);
CreateIndexRequest request = new CreateIndexRequest("books");
client.indices().create(request , RequestOptions.DEFAULT);
client.close();
}
测试之前先把之前的books 索引删掉
运行测试
OK,成功了
查一下
OK,一个全新的books 索引
【现在在我们的测试中,每次都要创建客户端,结束后关闭客户端】
有个快捷的办法【Alt + insert】
@BeforeEach
void setUp() {
HttpHost host = HttpHost.create("http://localhost:9200");
RestClientBuilder builder = RestClient.builder(host);
client = new RestHighLevelClient(builder);
}
@AfterEach
void tearDown() throws IOException {
client.close();
}
这样就不用每次都写了
回顾一下
现在是2022年10月20日,可能已经有了
4.16.3 小结
- High Leven Client
- 客户端初始化
- 客户端改进