提交 4a3ce5a9 编写于 作者: 微笑很纯洁's avatar 微笑很纯洁

add spring-boot-elasticsearch

上级 d0c552b4
<?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.neo</groupId>
<artifactId>spring-boot-elasticsearch</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<name>spring-boot-elasticsearch</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</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-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
package com.neo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ElasticsearchApplication {
public static void main(String[] args) {
SpringApplication.run(ElasticsearchApplication.class, args);
}
}
package com.neo.model;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
@Document(indexName = "customer", type = "customer", shards = 1, replicas = 0, refreshInterval = "-1")
public class Customer {
@Id
private String id;
private String userName;
private String address;
public Customer() {
}
public Customer(String userName, String address) {
this.userName = userName;
this.address = address;
}
public String getId() {
return this.id;
}
public void setId(String id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return String.format("Customer[id=%s, userName='%s', address='%s']", this.id,
this.userName, this.address);
}
}
package com.neo.repository;
import com.neo.model.Customer;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import java.util.List;
public interface CustomerRepository extends ElasticsearchRepository<Customer, String> {
public Customer findByUserName(String userName);
public int deleteByUserName(String userName);
public List<Customer> findByAddress(String address);
}
spring.data.elasticsearch.cluster-nodes=localhost:9300
\ No newline at end of file
package com.neo;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class ElasticsearchApplicationTests {
@Test
public void contextLoads() {
System.out.println("Spring Boot Test");
}
}
package com.neo.repository;
import com.neo.model.Customer;
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.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class CustomerRepositoryTest {
@Autowired
private CustomerRepository repository;
@Test
public void saveCustomers() {
repository.save(new Customer("Alice", "北京"));
repository.save(new Customer("Bob", "北京"));
repository.save(new Customer("neo", "西安"));
repository.save(new Customer("summer", "烟台"));
}
@Test
public void deleteCustomers() {
// repository.deleteAll();
repository.deleteByUserName("neo");
}
@Test
public void updateCustomers() {
Customer customer= repository.findByUserName("summer");
System.out.println(customer);
customer.setAddress("北京市海淀区西直门");
repository.save(customer);
Customer xcustomer=repository.findByUserName("summer");
System.out.println(xcustomer);
}
@Test
public void fetchAllCustomers() {
System.out.println("Customers found with findAll():");
System.out.println("-------------------------------");
for (Customer customer : repository.findAll()) {
System.out.println(customer);
}
System.out.println();
}
@Test
public void fetchIndividualCustomers() {
System.out.println("Customer found with findByUserName('summer'):");
System.out.println("--------------------------------");
System.out.println(repository.findByUserName("summer"));
System.out.println("Customers found with findByAddress(\"北京\"):");
System.out.println("--------------------------------");
for (Customer customer : repository.findByAddress("北京")) {
System.out.println(customer);
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册