forked from ityouknow/spring-boot-examples
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
218 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?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> |
12 changes: 12 additions & 0 deletions
12
spring-boot-elasticsearch/src/main/java/com/neo/ElasticsearchApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
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); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
spring-boot-elasticsearch/src/main/java/com/neo/model/Customer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
|
||
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); | ||
} | ||
|
||
} |
17 changes: 17 additions & 0 deletions
17
spring-boot-elasticsearch/src/main/java/com/neo/repository/CustomerRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
|
||
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); | ||
|
||
} |
1 change: 1 addition & 0 deletions
1
spring-boot-elasticsearch/src/main/resources/application.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
spring.data.elasticsearch.cluster-nodes=localhost:9300 |
18 changes: 18 additions & 0 deletions
18
spring-boot-elasticsearch/src/test/java/com/neo/ElasticsearchApplicationTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
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"); | ||
} | ||
|
||
} |
64 changes: 64 additions & 0 deletions
64
spring-boot-elasticsearch/src/test/java/com/neo/repository/CustomerRepositoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
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); | ||
} | ||
} | ||
|
||
} |