Skip to content

Commit 438870d

Browse files
committed
SpringBoot之@elasticsearch完成CURD
1 parent 0792b8f commit 438870d

File tree

4 files changed

+137
-0
lines changed

4 files changed

+137
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
[【SpringBoot2.0系列09】SpringBoot之rabbitmq使用](https://www.jianshu.com/p/0d400d30936b)
2020

2121
[【SpringBoot2.0系列10】SpringBoot之@Scheduled任务调度](https://www.jianshu.com/p/94cc87cf8f18)
22+
23+
[【SpringBoot2.0系列11】SpringBoot之@Elasticsearch完成CURD](https://www.jianshu.com/p/3ec38433d9ad)
24+
25+
2226
[简书地址](https://www.jianshu.com/nb/26935871)
2327

2428
[腾讯云+社区地址](https://cloud.tencent.com/developer/column/4803)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.yukong.chapter10.entity;
2+
3+
import lombok.*;
4+
import lombok.experimental.Accessors;
5+
import org.springframework.data.annotation.Id;
6+
import org.springframework.data.elasticsearch.annotations.Document;
7+
8+
/**
9+
* @author: yukong
10+
* @date: 2018/9/4 14:48
11+
* @description:
12+
*/
13+
@Data
14+
@ToString
15+
@Accessors(chain = true)
16+
@EqualsAndHashCode
17+
@Document(indexName = "goods", type = "computer")
18+
public class Good {
19+
20+
/**
21+
* 主键,注意这个搜索是id类型是string,与我们常用的不同
22+
* Id注解加上后,在Elasticsearch里相应于该列就是主键了,在查询时就可以直接用主键查询
23+
*/
24+
@Id
25+
private String id;
26+
27+
private String name;
28+
29+
private String desc;
30+
31+
private Integer number;
32+
33+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.yukong.chapter10.repository;
2+
3+
import com.yukong.chapter10.entity.Good;
4+
import org.springframework.data.domain.Page;
5+
import org.springframework.data.domain.Pageable;
6+
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
7+
8+
/**
9+
* @author: yukong
10+
* @date: 2018/9/4 14:53
11+
* @description:
12+
*/
13+
public interface GoodRepository extends ElasticsearchRepository<Good, String> {
14+
15+
/**
16+
* 根据商品名称查询 分页
17+
* @param name
18+
* @param pageable
19+
* @return
20+
*/
21+
Page<Good> findByName(String name, Pageable pageable);
22+
23+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.yukong.chapter10.repository;
2+
3+
import com.yukong.chapter10.Chapter10ApplicationTests;
4+
import com.yukong.chapter10.entity.Good;
5+
import org.junit.Assert;
6+
import org.junit.FixMethodOrder;
7+
import org.junit.Test;
8+
import org.junit.runners.MethodSorters;
9+
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.data.domain.Page;
11+
import org.springframework.data.domain.PageRequest;
12+
import org.springframework.data.domain.Pageable;
13+
import org.springframework.stereotype.Component;
14+
15+
16+
import static org.junit.Assert.*;
17+
18+
19+
20+
/**
21+
* @author: yukong
22+
* @date: 2018/9/4 14:56
23+
* @description:
24+
*/
25+
@Component
26+
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
27+
public class GoodRepositoryTest extends Chapter10ApplicationTests {
28+
29+
@Autowired
30+
private GoodRepository goodRepository;
31+
32+
@Test
33+
public void save(){
34+
Good good = new Good();
35+
good.setId("100")
36+
.setName("联想e541")
37+
.setDesc("联想e系列")
38+
.setNumber(10);
39+
Good result = goodRepository.save(good);
40+
Assert.assertNotNull(result);
41+
}
42+
43+
@Test
44+
public void select(){
45+
// 需要注意find方法返回的死Optional对象 需要调用get方法返回具体的实体类对象
46+
Good result = goodRepository.findById("100").get();
47+
Assert.assertNotNull(result);
48+
49+
}
50+
51+
@Test
52+
public void update(){
53+
Good result = goodRepository.findById("100").get();
54+
result.setNumber(300);
55+
// 更新也是调用save方法
56+
Good good = goodRepository.save(result);
57+
Assert.assertNotNull(good);
58+
59+
}
60+
61+
62+
63+
@Test
64+
public void delete(){
65+
goodRepository.deleteById("100");
66+
}
67+
68+
@Test
69+
public void findByName() {
70+
String name = "macbook";
71+
Pageable pageable = new PageRequest(1,1);
72+
Page<Good> goods = goodRepository.findByName(name, pageable);
73+
System.out.println(goods.getContent());
74+
Assert.assertEquals(1, goods.getSize());
75+
76+
}
77+
}

0 commit comments

Comments
 (0)