Skip to content

Commit b3a09af

Browse files
committed
Lession 9
1 parent 2c14e45 commit b3a09af

File tree

13 files changed

+351
-0
lines changed

13 files changed

+351
-0
lines changed
Loading
Binary file not shown.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
target/
2+
!.mvn/wrapper/maven-wrapper.jar
3+
4+
### STS ###
5+
.apt_generated
6+
.classpath
7+
.factorypath
8+
.project
9+
.settings
10+
.springBeans
11+
12+
### IntelliJ IDEA ###
13+
.idea
14+
*.iws
15+
*.iml
16+
*.ipr
17+
18+
### NetBeans ###
19+
nbproject/private/
20+
build/
21+
nbbuild/
22+
dist/
23+
nbdist/
24+
.nb-gradle/
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.segmentfault</groupId>
7+
<artifactId>spring-boot-lesson-9</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<name>spring-boot-lesson-9</name>
12+
<description>Demo project for Spring Boot</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>1.5.6.RELEASE</version>
18+
<relativePath/> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24+
<java.version>1.8</java.version>
25+
</properties>
26+
27+
<dependencies>
28+
<dependency>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-starter-actuator</artifactId>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.springframework.boot</groupId>
34+
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
35+
</dependency>
36+
<dependency>
37+
<groupId>org.springframework.boot</groupId>
38+
<artifactId>spring-boot-starter-web</artifactId>
39+
</dependency>
40+
41+
<!-- JNA dependency -->
42+
<dependency>
43+
<groupId>net.java.dev.jna</groupId>
44+
<artifactId>jna</artifactId>
45+
<version>4.4.0</version>
46+
</dependency>
47+
48+
<!--<dependency>-->
49+
<!--<groupId>org.springframework</groupId>-->
50+
<!--<artifactId>spring-jdbc</artifactId>-->
51+
<!--</dependency>-->
52+
53+
<dependency>
54+
<groupId>org.springframework.boot</groupId>
55+
<artifactId>spring-boot-starter-test</artifactId>
56+
<scope>test</scope>
57+
</dependency>
58+
</dependencies>
59+
60+
<build>
61+
<plugins>
62+
<plugin>
63+
<groupId>org.springframework.boot</groupId>
64+
<artifactId>spring-boot-maven-plugin</artifactId>
65+
</plugin>
66+
</plugins>
67+
</build>
68+
69+
70+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.segmentfault.springbootlesson9;
2+
3+
import org.elasticsearch.node.NodeBuilder;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
7+
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
8+
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;
9+
10+
import static org.elasticsearch.node.NodeBuilder.nodeBuilder;
11+
12+
/**
13+
* Elasticsearch {@link Configuration}
14+
*
15+
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
16+
* @see
17+
* @since 2017.07.30
18+
*/
19+
@Configuration
20+
@EnableElasticsearchRepositories(basePackages = "org/springframework/data/elasticsearch/repositories")
21+
public class ElasticsearchConfiguration {
22+
23+
@Bean
24+
public ElasticsearchOperations elasticsearchTemplate() {
25+
NodeBuilder nodeBuilder = nodeBuilder();
26+
nodeBuilder.settings().put("path.home",System.getenv("ES_HOME"));
27+
return new ElasticsearchTemplate(nodeBuilder().local(true).node().client());
28+
}
29+
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.segmentfault.springbootlesson9;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;
6+
7+
@SpringBootApplication
8+
@EnableElasticsearchRepositories
9+
public class SpringBootLesson9Application {
10+
11+
public static void main(String[] args) {
12+
SpringApplication.run(SpringBootLesson9Application.class, args);
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.segmentfault.springbootlesson9.controller;
2+
3+
import com.segmentfault.springbootlesson9.entity.Book;
4+
import com.segmentfault.springbootlesson9.repository.BookRepository;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.beans.factory.annotation.Qualifier;
7+
import org.springframework.data.repository.CrudRepository;
8+
import org.springframework.data.repository.PagingAndSortingRepository;
9+
import org.springframework.web.bind.annotation.*;
10+
11+
import java.util.Date;
12+
import java.util.List;
13+
14+
/**
15+
* 图书的 REST 控制器
16+
*
17+
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
18+
* @see
19+
* @since 2017.07.30
20+
*/
21+
@RestController
22+
public class BookController {
23+
24+
@Autowired
25+
@Qualifier("bookRepository")
26+
private PagingAndSortingRepository<Book, String> bookRepository;
27+
28+
@Autowired
29+
@Qualifier("bookRepository2")
30+
private BookRepository bookRepository2;
31+
32+
@GetMapping(value = "/books/{name}")
33+
public List<Book> getBooks(@PathVariable String name) {
34+
return bookRepository2.findByName(name);
35+
}
36+
37+
@GetMapping(value = "/book/{id}")
38+
public Book getBook(@PathVariable String id) {
39+
40+
Book book = bookRepository2.findOne(id);
41+
42+
return book;
43+
}
44+
45+
@PostMapping(value = "/book/{id}")
46+
public Book publishBook(@PathVariable String id, @RequestBody Book book) {
47+
48+
book.setId(id);
49+
book.setPublishedDate(new Date(System.currentTimeMillis()));
50+
51+
bookRepository.save(book);
52+
53+
return book;
54+
55+
}
56+
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.segmentfault.springbootlesson9.entity;
2+
3+
import org.springframework.data.elasticsearch.annotations.Document;
4+
5+
import java.io.Serializable;
6+
import java.util.Date;
7+
8+
/**
9+
* 图书实体对象
10+
*
11+
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
12+
* @see
13+
* @since 2017.07.30
14+
*/
15+
@Document(indexName = "book", type = "it")
16+
public class Book implements Serializable {
17+
18+
private String id;
19+
20+
private String name;
21+
22+
private Float price;
23+
24+
private Date publishedDate;
25+
26+
public String getId() {
27+
return id;
28+
}
29+
30+
public void setId(String id) {
31+
this.id = id;
32+
}
33+
34+
public String getName() {
35+
return name;
36+
}
37+
38+
public void setName(String name) {
39+
this.name = name;
40+
}
41+
42+
public Float getPrice() {
43+
return price;
44+
}
45+
46+
public void setPrice(Float price) {
47+
this.price = price;
48+
}
49+
50+
public Date getPublishedDate() {
51+
return publishedDate;
52+
}
53+
54+
public void setPublishedDate(Date publishedDate) {
55+
this.publishedDate = publishedDate;
56+
}
57+
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.segmentfault.springbootlesson9.repository;
2+
3+
import com.segmentfault.springbootlesson9.entity.Book;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
6+
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity;
7+
import org.springframework.data.elasticsearch.core.mapping.SimpleElasticsearchPersistentEntity;
8+
import org.springframework.data.elasticsearch.repository.support.AbstractElasticsearchRepository;
9+
import org.springframework.data.elasticsearch.repository.support.ElasticsearchEntityInformation;
10+
import org.springframework.data.elasticsearch.repository.support.MappingElasticsearchEntityInformation;
11+
import org.springframework.data.util.ClassTypeInformation;
12+
import org.springframework.data.util.TypeInformation;
13+
import org.springframework.stereotype.Repository;
14+
15+
/**
16+
* 图书的仓储
17+
*
18+
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
19+
* @see
20+
* @since 2017.07.30
21+
*/
22+
@Repository("bookRepository")
23+
public class BookElasticsearchRepository extends AbstractElasticsearchRepository<Book, String> {
24+
25+
private BookRepository bookRepository;
26+
27+
public Book save(Book book){
28+
29+
return bookRepository.save(book);
30+
31+
}
32+
33+
34+
@Autowired
35+
public BookElasticsearchRepository(ElasticsearchOperations elasticsearchOperations) {
36+
super(createElasticsearchEntityInformation(), elasticsearchOperations);
37+
super.setEntityClass(Book.class);
38+
}
39+
40+
private static ElasticsearchEntityInformation<Book, String> createElasticsearchEntityInformation() {
41+
42+
TypeInformation<Book> typeInformation = ClassTypeInformation.from(Book.class);
43+
44+
ElasticsearchPersistentEntity<Book> entity = new SimpleElasticsearchPersistentEntity(typeInformation);
45+
46+
ElasticsearchEntityInformation<Book, String> information = new MappingElasticsearchEntityInformation(entity);
47+
48+
return information;
49+
50+
}
51+
52+
53+
@Override
54+
protected String stringIdRepresentation(String s) {
55+
return s;
56+
}
57+
58+
}

0 commit comments

Comments
 (0)