栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

3分钟SpringData整合 ElasticSearch 实现CRUD超级详细

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

3分钟SpringData整合 ElasticSearch 实现CRUD超级详细

文章目录

1.导入依赖2.配置 yml3.创建Bean4.创建接口继承 CrudRepository 接口5. 创建service 注入 接口代理类对象6.主启动类上添加 @EnableElasticsearchRepositories7.编写方法名

			⭐️秋日的晚霞⭐️

		⭐️玲珑骰子安红豆 入骨相思知不知⭐️

     如果对你有帮助,给博主一个免费的点赞  
     博客主页   秋日的晚霞
⭐️     更多文章     请关注主页    
     一起走上java学习之路!
     欢迎各位点赞评论收藏⭐️     

1.导入依赖
		
            org.springframework.boot
            spring-boot-starter-data-elasticsearch
            2.6.4
        
2.配置 yml
spring:
  elasticsearch:
    rest:
      uris:
        - http://xxxxx:9200
3.创建Bean
    @document( indexName= xxx) ES 的索引名@Id ES 的文档ID@Field ES的字段映射type = FieldType.Keyword 关键字 不分词 ( ES中没有String 只有 text (分词) 和 keyword ( 不分词 )index 是否能索引analyzer 使用的分词器format 格式转换 pattern 日期格式FieldType.Nested 集合属性 避免查出业务错误
@document(indexName = "goods" , shards = 3,replicas = 2)
public class Goods {
    // 商品Id skuId
    @Id
    private Long id;

    @Field(type = FieldType.Keyword, index = false)
    private String defaultImg;

    //  es 中能分词的字段,这个字段数据类型必须是 text!keyword 不分词!
    @Field(type = FieldType.Text, analyzer = "ik_max_word")
    private String title;

    @Field(type = FieldType.Double)
    private Double price;

    //  @Field(type = FieldType.Date)   6.8.1
    @Field(type = FieldType.Date,format = DateFormat.custom,pattern = "yyyy-MM-dd HH:mm:ss")
    private Date createTime; // 新品

    @Field(type = FieldType.Long)
    private Long tmId;

    @Field(type = FieldType.Keyword)
    private String tmName;

    @Field(type = FieldType.Keyword)
    private String tmLogoUrl;

    @Field(type = FieldType.Long)
    private Long category1Id;

    @Field(type = FieldType.Keyword)
    private String category1Name;

    @Field(type = FieldType.Long)
    private Long category2Id;

    @Field(type = FieldType.Keyword)
    private String category2Name;

    @Field(type = FieldType.Long)
    private Long category3Id;

    @Field(type = FieldType.Keyword)
    private String category3Name;

    //  商品的热度! 我们将商品被用户点查看的次数越多,则说明热度就越高!
    @Field(type = FieldType.Long)
    private Long hotScore = 0L;

    // 平台属性集合对象
    // Nested 支持嵌套查询
    @Field(type = FieldType.Nested)
    private List attrs;

}
4.创建接口继承 CrudRepository 接口

泛型1 : ES对应的javaBean

泛型2 : 文档唯一ID的类型

@Repository
public interface GoodsDao extends CrudRepository {


}

注意 如果想实现分页 请实现 PagingAndSortingRepository 接口

@Repository
public interface GoodsDao extends PagingAndSortingRepository {


}

接口上添加 @Repository 注解

5. 创建service 注入 接口代理类对象
@Service
public class GoodsServiceImpl implements GoodService {

    @Autowired
    private GoodsDao goodsDao;

    @Override
    public boolean onSale(Goods goods) {

        Goods save = goodsDao.save(goods);

       return !StringUtils.isEmpty(save);

    }

}
6.主启动类上添加 @EnableElasticsearchRepositories
@EnableElasticsearchRepositories
@SpringCloudApplication
public class EsListApp {

    public static void main(String[] args) {
        SpringApplication.run(EsListApp.class);
    }

}
7.编写方法名

小提示 先写返回值类型 这样有提示

命名规则 Spring Data Commons - 参考文档

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/762442.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号