学习springboot整合ElasticSearch 7.X版本并通过小demo实现基本的增删改查
- 引入依赖
1 2 3 4
| <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> </dependency>
|
- 修改配置文件
1 2 3 4
| spring: application: rest: uris: http://xx.XX.XX.XX:9200/
|
- 新增一些实体类、to类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| @Data @EqualsAndHashCode(callSuper = false) @TableName("t_user") public class TUser implements Serializable {
private static final long serialVersionUID = 1L;
@TableId(value = "id", type = IdType.AUTO) private Integer id;
private LocalDate birthday;
private String gender;
private String username;
private String password;
private String remark;
private String station;
private String telephone; }
@Data @Document(indexName = "user_info") public class UserInfoTO { @Id private Integer id;
private LocalDate birthday;
private String gender;
private String username;
private String password;
private String remark;
private String station;
private String telephone; }
|
- 实用es新增用户
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| @Repository public interface ITUserEsSevice extends ElasticsearchRepository<UserInfoTO,Integer> { }
@ApiOperation(value = "新增用户") @PostMapping("/saveUser") public void saveUser(@RequestBody TUser tUser){ itUserService.saveUser(tUser); }
@Autowired ITUserEsSevice itUserEsSevice;
@Override public void saveUser(TUser tUser) { UserInfoTO userInfoTO = new UserInfoTO(); BeanUtils.copyProperties(tUser,userInfoTO); itUserEsSevice.delete(userInfoTO); }
|
参考一:Linux环境下安装Elasticsearch
参考二:Linux安装Kibana详细教程
参考三:springboot整合ElasticSearch实战
参考四:Spring Data Elasticsearch 实体类注解说明 此博主包含 Elasticsearch 其它系列博文