什么是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.分页查询