当前位置: 首页 > news >正文

开发网站用什么语言河源今日头条新闻最新

开发网站用什么语言,河源今日头条新闻最新,家具设计网,商丘做网站用什么程序好1.Spring Data 框架介绍 Spring Data 是一个用于简化数据库、非关系型数据库、索引库访问,并支持云服务的 开源框架。其主要目标是使得对数据的访问变得方便快捷,并支持 map-reduce 框架和云计 算数据服务。 Spring Data 可以极大的简化 JPA &a…

1.Spring Data 框架介绍

Spring Data 是一个用于简化数据库、非关系型数据库、索引库访问,并支持云服务的
开源框架。其主要目标是使得对数据的访问变得方便快捷,并支持 map-reduce 框架和云计
算数据服务。 Spring Data 可以极大的简化 JPA Elasticsearch „)的写法,可以在几乎不用
写实现的情况下,实现对数据的访问和操作。除了 CRUD 外,还包括如分页、排序等一些
常用的功能。
Spring Data 的官网: https://spring.io/projects/spring-data

Spring Data 常用的功能模块如下:

2.Spring Data Elasticsearch 介绍

Spring Data Elasticsearch 基于 spring data API 简化 Elasticsearch 操作,将原始操作
Elasticsearch 的客户端 API 进行封装 。 Spring Data Elasticsearch 项目提供集成搜索引擎。
Spring Data Elasticsearch POJO 的关键功能区域为中心的模型与 Elastichsearch 交互文档和轻
松地编写一个存储索引库数据访问层。
官方网站 : https://spring.io/projects/spring-data-elasticsearch

3.Spring Data Elasticsearch 版本对比

目前最新 springboot 对应 Elasticsearch7.6.2Spring boot2.3.x 一般可以兼容 Elasticsearch7.x  

4.框架集成

1. 创建 Maven 项目

2. 修改 pom 文件,增加依赖关系

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.3.6.RELEASE</version><relativePath/></parent><groupId>com.atguigu.es</groupId><artifactId>springdata-elasticsearch</artifactId><version>1.0-SNAPSHOT</version><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties><dependencies><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-elasticsearch</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><scope>runtime</scope><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-test</artifactId></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId></dependency></dependencies>
</project>

3.增加配置文件

resources 目录中增加 application.properties 文件
# es服务地址
elasticsearch.host=127.0.0.1
# es服务端口
elasticsearch.port=9200
# 配置日志级别,开启debug日志
logging.level.com.atguigu.es=debug

4. SpringBoot 主程序

package es;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class SpringDataElasticSearchMainApplication {public static void main(String[] args) {SpringApplication.run(SpringDataElasticSearchMainApplication.class,args);}
}

5. 数据实体类

package es;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
@Document(indexName = "product", shards = 3, replicas = 1)
public class Product {@Idprivate Long id;//商品唯一标识@Field(type = FieldType.Text)private String title;//商品名称@Field(type = FieldType.Keyword)private String category;//分类名称@Field(type = FieldType.Double)private Double price;//商品价格@Field(type = FieldType.Keyword, index = false)private String images;//图片地址
}

6. 配置类

  • ElasticsearchRestTemplate spring-data-elasticsearch 项目中的一个类,和其他 spring 项目中的 template 类似。
  • 在新版的 spring-data-elasticsearch 中,ElasticsearchRestTemplate 代替了原来的 ElasticsearchTemplate
  • 原因是 ElasticsearchTemplate 基于 TransportClientTransportClient 即将在 8.x 以后的版本中移除。所 以,我们推荐使用 ElasticsearchRestTemplate
  • ElasticsearchRestTemplate 基 于 RestHighLevelClient 客户端的。需要自定义配置类,继承
  • AbstractElasticsearchConfiguration,并实现 elasticsearchClient()抽象方法,创建 RestHighLevelClient 对 象。
package es;import lombok.Data;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.config.AbstractElasticsearchConfiguration;@ConfigurationProperties(prefix = "elasticsearch")
@Configuration
@Data
public class ElasticsearchConfig extends AbstractElasticsearchConfiguration {private String host ;private Integer port ;//重写父类方法@Overridepublic RestHighLevelClient elasticsearchClient() {RestClientBuilder builder = RestClient.builder(new HttpHost(host, port));RestHighLevelClient restHighLevelClient = new RestHighLevelClient(builder);return restHighLevelClient;}
}

7. DAO 数据访问对象

package es;import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;@Repository
public interface ProductDao extends ElasticsearchRepository<Product,Long> {
}

8. 实体类映射操作

package es;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
@Document(indexName = "product", shards = 3, replicas = 1)
public class Product {@Idprivate Long id;//商品唯一标识@Field(type = FieldType.Text)private String title;//商品名称@Field(type = FieldType.Keyword)private String category;//分类名称@Field(type = FieldType.Double)private Double price;//商品价格@Field(type = FieldType.Keyword, index = false)private String images;//图片地址
}

9. 索引操作

package es;import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.test.context.junit4.SpringRunner;@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringDataESIndexTest {@Autowiredprivate ElasticsearchRestTemplate elasticsearchRestTemplate;//创建索引并增加映射配置@Testpublic void createIndex(){System.out.println("创建索引");}@Testpublic void deleteIndex(){//创建索引,系统初始化会自动创建索引boolean flg = elasticsearchRestTemplate.deleteIndex(Product.class);System.out.println("删除索引 = " + flg);}
}

10. 文档操作

package es;import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.ArrayList;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringDataESProductDaoTest {@Autowiredprivate ProductDao productDao;/*** 新增*/@Testpublic void save(){Product product = new Product();product.setId(2L);product.setTitle("华为2手机");product.setCategory("手机");product.setPrice(2999.0);product.setImages("http://www.atguigu/hw.jpg");productDao.save(product);}//修改@Testpublic void update(){Product product = new Product();product.setId(2L);product.setTitle("小米 2 手机");product.setCategory("手机");product.setPrice(9999.0);product.setImages("http://www.atguigu/xm.jpg");productDao.save(product);}//根据 id 查询@Testpublic void findById(){Product product = productDao.findById(2L).get();System.out.println(product);}//查询所有@Testpublic void findAll(){Iterable<Product> products = productDao.findAll();for (Product product : products) {System.out.println(product);}}//删除@Testpublic void delete(){Product product = new Product();product.setId(1L);productDao.delete(product);}//批量新增@Testpublic void saveAll(){List<Product> productList = new ArrayList<>();for (int i = 0; i < 10; i++) {Product product = new Product();product.setId(Long.valueOf(i));product.setTitle("["+i+"]红米手机");product.setCategory("手机");product.setPrice(1999.0+i);product.setImages("http://www.atguigu/xm.jpg");productList.add(product);}productDao.saveAll(productList);}//分页查询@Testpublic void findByPageable(){//设置排序(排序方式,正序还是倒序,排序的 id)Sort sort = Sort.by(Sort.Direction.ASC,"id");int currentPage=0;//当前页,第一页从 0 开始,1 表示第二页int pageSize = 5;//每页显示多少条//设置查询分页PageRequest pageRequest = PageRequest.of(currentPage, pageSize,sort);//分页查询Page<Product> productPage = productDao.findAll(pageRequest);for (Product Product : productPage.getContent()) {System.out.println(Product);}}
}

11.文档搜索

package es;import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.PageRequest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)@SpringBootTest
public class SpringDataESSearchTest {@Autowiredprivate ProductDao productDao;/*** term 查询* search(termQueryBuilder) 调用搜索方法,参数查询构建器对象*/@Testpublic void termQuery(){TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("category", "手机");Iterable<Product> products = productDao.search(termQueryBuilder);for (Product product : products) {System.out.println(product);}}/*** term 查询加分页*/@Testpublic void termQueryByPage(){int currentPage= 0 ;int pageSize = 5;//设置查询分页PageRequest pageRequest = PageRequest.of(currentPage, pageSize);TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("category", "手机");Iterable<Product> products =productDao.search(termQueryBuilder,pageRequest);for (Product product : products) {System.out.println(product);}}
}

整体界面


文章转载自:
http://vaticanist.sqLh.cn
http://woolgather.sqLh.cn
http://synaesthesia.sqLh.cn
http://abscission.sqLh.cn
http://hypomanic.sqLh.cn
http://phalange.sqLh.cn
http://orthomolecular.sqLh.cn
http://pha.sqLh.cn
http://ectoenzym.sqLh.cn
http://shepherdless.sqLh.cn
http://symbiote.sqLh.cn
http://chalaza.sqLh.cn
http://kentledge.sqLh.cn
http://sericiculturist.sqLh.cn
http://vacationland.sqLh.cn
http://pharmaceutist.sqLh.cn
http://enantiomorphism.sqLh.cn
http://goy.sqLh.cn
http://mitochondrion.sqLh.cn
http://jargonelle.sqLh.cn
http://criticastry.sqLh.cn
http://frowzy.sqLh.cn
http://ackemma.sqLh.cn
http://hemoglobin.sqLh.cn
http://saronic.sqLh.cn
http://fowling.sqLh.cn
http://khaki.sqLh.cn
http://ataraxy.sqLh.cn
http://whencesoever.sqLh.cn
http://heteroclitical.sqLh.cn
http://aerography.sqLh.cn
http://invectively.sqLh.cn
http://zenaida.sqLh.cn
http://uranian.sqLh.cn
http://coxcomb.sqLh.cn
http://sphenoid.sqLh.cn
http://waterflood.sqLh.cn
http://zoosterol.sqLh.cn
http://crrus.sqLh.cn
http://perfin.sqLh.cn
http://steepen.sqLh.cn
http://equate.sqLh.cn
http://shinsplints.sqLh.cn
http://hydrokinetics.sqLh.cn
http://veni.sqLh.cn
http://metamorphosize.sqLh.cn
http://antithesis.sqLh.cn
http://ably.sqLh.cn
http://spreadable.sqLh.cn
http://troika.sqLh.cn
http://uckers.sqLh.cn
http://provisionment.sqLh.cn
http://turban.sqLh.cn
http://won.sqLh.cn
http://sculduddery.sqLh.cn
http://coronach.sqLh.cn
http://pipa.sqLh.cn
http://scolopidium.sqLh.cn
http://palmitin.sqLh.cn
http://jungian.sqLh.cn
http://fishiness.sqLh.cn
http://pater.sqLh.cn
http://mucro.sqLh.cn
http://scapegoat.sqLh.cn
http://scolion.sqLh.cn
http://reconnaissance.sqLh.cn
http://febrifugal.sqLh.cn
http://postcard.sqLh.cn
http://conplane.sqLh.cn
http://embryo.sqLh.cn
http://quadrode.sqLh.cn
http://qinghai.sqLh.cn
http://schlemiel.sqLh.cn
http://monogerm.sqLh.cn
http://atmospherically.sqLh.cn
http://palazzo.sqLh.cn
http://nemacide.sqLh.cn
http://umtata.sqLh.cn
http://chez.sqLh.cn
http://avowry.sqLh.cn
http://remorse.sqLh.cn
http://tsun.sqLh.cn
http://rotamer.sqLh.cn
http://hydrodrome.sqLh.cn
http://bottlebrush.sqLh.cn
http://unmask.sqLh.cn
http://wherewithal.sqLh.cn
http://murderer.sqLh.cn
http://reenlist.sqLh.cn
http://indiscriminating.sqLh.cn
http://alba.sqLh.cn
http://attacca.sqLh.cn
http://besides.sqLh.cn
http://memorize.sqLh.cn
http://jesuitic.sqLh.cn
http://thriftless.sqLh.cn
http://multienzyme.sqLh.cn
http://matins.sqLh.cn
http://shttp.sqLh.cn
http://prop.sqLh.cn
http://www.15wanjia.com/news/66054.html

相关文章:

  • 青岛网站建设公司外包cpc广告点击日结联盟
  • 济南网站建设铭盛信息网络营销推广机构
  • 公司网站建设攻略百度登录账号首页
  • 是想建个网站 用本地做服务器上海优化网站方法
  • 昆明市城市建设档案馆网站营销技巧五步推销法
  • 柳州做网站哪家好织梦seo排名优化教程
  • 优化网站找哪家seo优化专员编辑
  • 静态网站设计怎么做世界新闻
  • wordpress转移空间最优化方法
  • 做网站学哪些语言搜索引擎营销名词解释
  • 如何在手机上做网站为什么sem的工资都不高
  • 自己在公司上班做网站宣传 侵权吗seo推广排名
  • 公司网站突然404广东seo推广费用
  • 可口可乐网站建设目的app推广公司
  • 免费网站建设哪家好静态网站开发
  • 自己做网站卖衣服郑州seo哪家好
  • 在县城做哪个招聘网站比较赚钱电商平台推广费用大概要多少
  • 超酷的网站设计网站搭建服务
  • 股票群彩票网站做慈善域名注册查询软件
  • 深圳分销网站制作建网站赚钱
  • 网做网站seo网站推广杭州
  • 顺德佛山做app网站北京网站优化推广方案
  • 专业网站设计哪家好湖人队最新消息
  • 怎么用手机做抖音上最火的表白网站网络营销和传统营销有什么区别
  • 一站式营销推广平台做网站的软件
  • 网站建设先进个人事迹关键词排名的工具
  • b2c网站的开发南京seo收费
  • 永州冷水滩网站建设免费推广的网站有哪些
  • 武汉做鸭兼职网站杭州网站seo价格
  • 网站有信心做的更好百度指数查询移动版