Commit f8c5fa8c authored by cloudgyb's avatar cloudgyb

feat:增加首页

parent 6dda3746
......@@ -43,3 +43,4 @@ create table t_book
```shell
./mvnw spring-boot:run
```
5. 访问http://localhost:8080 进入首页进行书籍信息的添加和搜索
\ No newline at end of file
......@@ -4,6 +4,7 @@ import com.gyb.elasticsearch.demo.entity.db.Book;
import com.gyb.elasticsearch.demo.entity.es.ESBook;
import com.gyb.elasticsearch.demo.service.BookService;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.util.StopWatch;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
......@@ -26,16 +27,24 @@ public class BookController {
}
@PostMapping("/book")
public Map<String,String> addBook(@RequestBody Book book){
public Map<String, String> addBook(@RequestBody Book book) {
bookService.addBook(book);
Map<String,String> map = new HashMap<>();
map.put("msg","ok");
Map<String, String> map = new HashMap<>();
map.put("msg", "ok");
return map;
}
@GetMapping("/book/search")
public SearchHits<ESBook> search(String key){
return bookService.searchBook1(key);
public Map<String, Object> search(String key) {
final HashMap<String, Object> map = new HashMap<>();
final StopWatch stopWatch = new StopWatch();
stopWatch.start();
final SearchHits<ESBook> searchHits = bookService.searchBook1(key);
stopWatch.stop();
map.put("res",searchHits);
final double totalTimeSeconds = stopWatch.getTotalTimeSeconds();
map.put("elapsedTime",totalTimeSeconds);
return map;
}
}
......@@ -19,9 +19,9 @@ public class ESBook {
@Id
@Field(type = FieldType.Text)
private String id;
@Field(analyzer="ik_max_word")
@Field(type=FieldType.Text,analyzer="ik_max_word")
private String title;
@Field(analyzer="ik_max_word")
@Field(type=FieldType.Text,analyzer="ik_max_word")
private String author;
@Field(type = FieldType.Double)
private Double price;
......
......@@ -10,6 +10,7 @@ import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.stereotype.Service;
import org.springframework.transaction.support.TransactionTemplate;
import java.util.Date;
import java.util.List;
/**
......@@ -32,6 +33,8 @@ public class BookService {
}
public void addBook(Book book) {
book.setCreateTime(new Date());
book.setUpdateTime(new Date());
final Book saveBook = transactionTemplate.execute((status) ->
bookRepository.save(book)
);
......
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Elasticsearch Demo</title>
<style>
input{
height: 30px;
width: 100%;
margin-bottom: 10px;
}
</style>
</head>
<body>
<div style="width: 400px;height: 300px;
margin: 0 auto;padding: 20px;box-shadow:#ccc 0 0 10px">
<h2>添加书籍信息</h2>
<label for="t"></label>
<input id="t" name="title" type="text" placeholder="书名" required/>
<label for="a"></label>
<input id="a" name="author" type="text" placeholder="作者" required/>
<label for="p"></label>
<input id="p" name="price" type="text" placeholder="价格" required/>
<button onclick="addBook()" style="width: 100%;height: 35px">添加</button>
</div>
<div style="width: 600px;
margin: 0 auto;padding: 20px;">
<button onclick="goSearch()" style="text-align: center;
width: 100%;height: 50px;background-color: #2498f1;
border: none;color: aliceblue">搜索书籍</button>
</div>
</body>
<script>
function goSearch(){
window.open("/search.html","_blank")
}
function addBook() {
let data = {
"title": document.getElementById("t").value,
"author":document.getElementById("a").value,
"price":document.getElementById("p").value
}
if(data.title.trim()==='' ||
data.author.trim()==='' || data.price.trim()===''){
alert("请补全信息再提交!")
return;
}
if(isNaN(parseFloat(data.price.trim()))){
alert("价格不合法!")
return;
}
let request = new XMLHttpRequest();
request.open("post", "/book", true);
request.setRequestHeader("Content-Type", "application/json")
request.send(JSON.stringify(data));
request.onload = function () {
if(this.status === 200){
alert("添加成功!")
}else{
alert("添加失败!")
}
}
request.onerror = function (){
alert("请求失败!")
}
}
</script>
</html>
\ No newline at end of file
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>搜索书籍</title>
</head>
<body>
<div style="width: 600px;
margin: 0 auto;padding: 20px;">
<label for="key"></label>
<input id="key" onkeypress="search(event)"
placeholder="输入关键字,回车" style="text-align: center;
width: 100%;height: 50px;outline: none;
border: #2498f1 1px solid;border-radius: 10px;font-size: 30px">
<div style="margin-top: 50px">
<label for="res">搜索结果:</label>
<span id="time" style="float: right"></span>
<textarea disabled id="res" name="" rows="30"
style="width: 100%;border: 1px #ccc solid;outline: none"></textarea>
</div>
</div>
<script>
function search(e) {
if (e.keyCode === 13) {
searchBook(document.getElementById("key").value)
}
console.log(e)
}
function searchBook(key) {
if (key === undefined || key.trim() === '') {
document.getElementById("res").value = ''
document.getElementById("time").innerHTML=''
return;
}
let request = new XMLHttpRequest();
request.open("get", "/book/search?key=" + key, true);
request.send();
request.onload = function () {
if (this.status === 200) {
let parse = JSON.parse(this.responseText);
document.getElementById("res").value = this.response
document.getElementById("time").innerHTML='耗时:' + parse.elapsedTime + 's'
} else {
alert("搜索失败!")
}
}
request.onerror = function () {
alert("请求失败!")
}
}
</script>
</body>
</html>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment