什么是Spring Data
Spring Data是一個用於簡化數據庫訪問,並支持雲服務的開源框架。其主要目標是使得對數據的訪問變得方便快捷,並支持map-reduce框架和雲計算數據服務。Spring Data可以極大的簡化JPA的寫法,可以在幾乎不用寫實現的情況下,實現對數據的訪問和操作。除了CRUD外,還包括分頁,排序等一些常用的功能;
什么是Spring Data ElasticSearch
Spring Data ElasticSearch基於spring data API簡化ES操作,將原始操作ES的客戶端API進行封裝。Spring Data為ES項目提供集成搜索引擎。Spring Data ElasticSearch POJO的關鍵字功能區域為中心的模型與ES交互文檔和輕松地編寫一個存儲庫數據訪問層;
增刪改查分頁案列:
1.導入依賴

<?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> <groupId>com.wn</groupId> <artifactId>ES_Data</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <name>ES_Data Maven Webapp</name> <!-- FIXME change it to the project's website --> <url>http://www.example.com</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> <version>5.6.8</version> </dependency> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>transport</artifactId> <version>5.6.8</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-to-slf4j</artifactId> <version>2.9.1</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.24</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-simple</artifactId> <version>1.7.21</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.12</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.8.1</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.8.1</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.8.1</version> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-elasticsearch</artifactId> <version>3.0.5.RELEASE</version> <exclusions> <exclusion> <groupId>org.elasticsearch.plugin</groupId> <artifactId>transport‐netty4‐client</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.0.4.RELEASE</version> </dependency> </dependencies> <build> <finalName>ES_Data</finalName> <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> <plugins> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>3.1.0</version> </plugin> <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging --> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>3.0.2</version> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.1</version> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>3.2.2</version> </plugin> <plugin> <artifactId>maven-install-plugin</artifactId> <version>2.5.2</version> </plugin> <plugin> <artifactId>maven-deploy-plugin</artifactId> <version>2.8.2</version> </plugin> </plugins> </pluginManagement> </build> </project>
2.創建實體類

1 package com.wn.title; 2 3 import org.springframework.data.annotation.Id; 4 import org.springframework.data.elasticsearch.annotations.Document; 5 import org.springframework.data.elasticsearch.annotations.Field; 6 import org.springframework.data.elasticsearch.annotations.FieldType; 7 8 //文檔對象 9 @Document(indexName = "table01",type = "table033") 10 public class Table01 { 11 @Id 12 @Field(store = true,index = false,type = FieldType.Integer) 13 private Integer id; 14 15 @Field(index = true,analyzer = "ik_smart",store = true,searchAnalyzer = "ik_smart",type = FieldType.text) 16 private String title; 17 18 //index:是否設置分詞 19 //analyzer:存儲時使用的分詞器 20 //searchAnalyze:搜索時使用的分詞器 21 //store:是否存儲 22 //type:數據類型 23 @Field(index = true,analyzer = "ik_smart",store = true,searchAnalyzer = "ik_smart",type = FieldType.text) 24 private String content; 25 26 public Integer getId() { 27 return id; 28 } 29 30 public void setId(Integer id) { 31 this.id = id; 32 } 33 34 public String getTitle() { 35 return title; 36 } 37 38 public void setTitle(String title) { 39 this.title = title; 40 } 41 42 public String getContent() { 43 return content; 44 } 45 46 public void setContent(String content) { 47 this.content = content; 48 } 49 50 @Override 51 public String toString() { 52 return "Table01{" + 53 "id=" + id + 54 ", title='" + title + '\'' + 55 ", content='" + content + '\'' + 56 '}'; 57 } 58 }
3.Dao層

package com.wn.dao; import com.wn.title.Table01; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; import org.springframework.stereotype.Repository; import java.util.List; @Repository public interface TableDao extends ElasticsearchRepository<Table01,Integer> { //根據標題查詢 List<Table01> findByTitle(String condition); //根據標題查詢(含分頁) Page<Table01> findByTitle(String condition, Pageable pageable); }
4.Service層

package com.wn.service; import com.wn.title.Table01; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; public interface TableServiceTest { //添加 public void save(Table01 table01); //刪除 public void delete(Table01 table01); //查詢全部 public Iterable<Table01> findAll(); //分頁查詢 public Page<Table01> findAll(Pageable pageable); }
5.ServiceImpl實現

package com.wn.service.impl; import com.wn.dao.TableDao; import com.wn.service.TableServiceTest; import com.wn.title.Table01; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; import javax.annotation.Resource; @Service public class TableServiceImplTest implements TableServiceTest { @Resource private TableDao dao; @Override public void save(Table01 table01) { dao.save(table01); } @Override public void delete(Table01 table01) { dao.delete(table01); } @Override public Iterable<Table01> findAll() { Iterable<Table01> iter = dao.findAll(); return iter; } @Override public Page<Table01> findAll(Pageable pageable) { return dao.findAll(pageable); } }
6.applicationContext.xml文件

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:elasticsearch="http://www.springframework.org/schema/data/elasticsearch" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/data/elasticsearch http://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch-1.0.xsd "> <!--掃描DAO包,自動創建實例--> <elasticsearch:repositories base-package="com.wn.dao"/> <!--掃描service包,創建service的實體--> <context:component-scan base-package="com.wn.service"/> <!--配置ES的連接--> <elasticsearch:transport-client id="client" cluster-nodes="127.0.0.1:9300" cluster-name="my-elasticsearch"/> <!--ES模板對象--> <bean id="elasticsearchTemplate" class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate"> <constructor-arg name="client" ref="client"></constructor-arg> </bean> </beans>
7.測試類

package com.wn.controller; import com.wn.service.TableServiceTest; import com.wn.title.Table01; import org.elasticsearch.client.transport.TransportClient; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; import org.springframework.data.elasticsearch.core.ElasticsearchTemplate; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import javax.annotation.Resource; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:applicationContext.xml") public class TableControllerTest { @Resource private TableServiceTest service; @Autowired private TransportClient client; @Autowired private ElasticsearchTemplate template; /*創建索引和映射*/ @Test public void createIndex(){ template.createIndex(Table01.class); template.createIndex(Table01.class); } /*創建文檔*/ @Test public void save(){ Table01 table01=new Table01(); table01.setId(12); table01.setTitle("ES 3.0版本發布"); table01.setContent("它是一個基於Lucene的搜索服務器"); service.save(table01); } /*測試更新*/ @Test public void update(){ Table01 table01=new Table01(); table01.setId(12); table01.setTitle("【修改】ES 3.0版本發布"); table01.setContent("【修改】它是一個基於Lucene的搜索服務器"); service.save(table01); } /*測試刪除*/ @Test public void delete(){ Table01 table01=new Table01(); table01.setId(12); service.delete(table01); } /*批量插入*/ @Test public void save20(){ for (int i=1;i<20;i++){ Table01 table01=new Table01(); table01.setId(i); table01.setTitle(i+"ES版本"); table01.setContent(i+"基於Lucene搜索服務器"); service.save(table01); } } /*查詢全部*/ @Test public void getAll() { Iterable<Table01> iterable = service.findAll(); for (Table01 table01 : iterable) { System.out.println(table01); } } /*分頁查詢*/ @Test public void findAllPage(){ Pageable pageable= PageRequest.of(0,5); Page<Table01> page=service.findAll(pageable); for (Table01 table01:page.getContent()){ System.out.println(table01); } } }
實現效果:
1.創建索引
2.添加數據
3.查詢
5.分頁查詢