mirror of
https://github.com/JeffLi1993/springboot-learning-example.git
synced 2026-03-13 21:43:45 +08:00
Spring Boot 入门、Spring Boot 配置、Web 开发、模板引擎、数据存储、数据缓存 案例更新
This commit is contained in:
65
chapter-3-spring-boot-web/pom.xml
Normal file
65
chapter-3-spring-boot-web/pom.xml
Normal file
@@ -0,0 +1,65 @@
|
||||
<?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>
|
||||
<name>chapter-3-spring-boot-web</name>
|
||||
<description>《Spring Boot 2.x 核心技术实战 - 上 基础篇》第 3 章《Web 开发》Demo</description>
|
||||
|
||||
<groupId>demo.springboot</groupId>
|
||||
<artifactId>chapter-3-spring-boot-web</artifactId>
|
||||
<version>1.0</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.0.0.BUILD-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
<java.version>1.8</java.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<!-- Web 依赖 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 测试依赖 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<!-- Spring Boot Maven 插件
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
-->
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>spring-snapshots</id>
|
||||
<name>Spring Snapshots</name>
|
||||
<url>https://repo.spring.io/libs-snapshot</url>
|
||||
<snapshots>
|
||||
<enabled>true</enabled>
|
||||
</snapshots>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,16 @@
|
||||
package demo.springboot;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
/**
|
||||
* Spring Boot 应用启动类
|
||||
*
|
||||
* Created by bysocket on 26/09/2017.
|
||||
*/
|
||||
@SpringBootApplication
|
||||
public class WebApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(WebApplication.class, args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package demo.springboot.domain;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* Book 实体类
|
||||
*
|
||||
* Created by bysocket on 27/09/2017.
|
||||
*/
|
||||
public class Book implements Serializable {
|
||||
|
||||
/**
|
||||
* 编号
|
||||
*/
|
||||
private Long id;
|
||||
|
||||
/**
|
||||
* 书名
|
||||
*/
|
||||
private String name;
|
||||
|
||||
/**
|
||||
* 作者
|
||||
*/
|
||||
private String writer;
|
||||
|
||||
/**
|
||||
* 简介
|
||||
*/
|
||||
private String introduction;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getWriter() {
|
||||
return writer;
|
||||
}
|
||||
|
||||
public void setWriter(String writer) {
|
||||
this.writer = writer;
|
||||
}
|
||||
|
||||
public String getIntroduction() {
|
||||
return introduction;
|
||||
}
|
||||
|
||||
public void setIntroduction(String introduction) {
|
||||
this.introduction = introduction;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package demo.springboot.service;
|
||||
|
||||
import demo.springboot.domain.Book;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Book 业务接口层
|
||||
*
|
||||
* Created by bysocket on 27/09/2017.
|
||||
*/
|
||||
public interface BookService {
|
||||
/**
|
||||
* 获取所有 Book
|
||||
*/
|
||||
List<Book> findAll();
|
||||
|
||||
/**
|
||||
* 新增 Book
|
||||
*
|
||||
* @param book {@link Book}
|
||||
*/
|
||||
Book insertByBook(Book book);
|
||||
|
||||
/**
|
||||
* 更新 Book
|
||||
*
|
||||
* @param book {@link Book}
|
||||
*/
|
||||
Book update(Book book);
|
||||
|
||||
/**
|
||||
* 删除 Book
|
||||
*
|
||||
* @param id 编号
|
||||
*/
|
||||
Book delete(Long id);
|
||||
|
||||
/**
|
||||
* 获取 Book
|
||||
*
|
||||
* @param id 编号
|
||||
*/
|
||||
Book findById(Long id);
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package demo.springboot.service.impl;
|
||||
|
||||
import demo.springboot.domain.Book;
|
||||
import demo.springboot.service.BookService;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Book 业务层实现
|
||||
*
|
||||
* Created by bysocket on 27/09/2017.
|
||||
*/
|
||||
@Service
|
||||
public class BookServiceImpl implements BookService {
|
||||
|
||||
// 模拟数据库,存储 Book 信息
|
||||
// 第五章《数据存储》会替换成 MySQL 存储
|
||||
private static Map<Long, Book> BOOK_DB = new HashMap<>();
|
||||
|
||||
@Override
|
||||
public List<Book> findAll() {
|
||||
return new ArrayList<>(BOOK_DB.values());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Book insertByBook(Book book) {
|
||||
book.setId(BOOK_DB.size() + 1L);
|
||||
BOOK_DB.put(book.getId(), book);
|
||||
return book;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Book update(Book book) {
|
||||
BOOK_DB.put(book.getId(), book);
|
||||
return book;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Book delete(Long id) {
|
||||
return BOOK_DB.remove(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Book findById(Long id) {
|
||||
return BOOK_DB.get(id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package demo.springboot.web;
|
||||
|
||||
import demo.springboot.domain.Book;
|
||||
import demo.springboot.service.BookService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Book 控制层
|
||||
*
|
||||
* Created by bysocket on 27/09/2017.
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping(value = "/book")
|
||||
public class BookController {
|
||||
|
||||
@Autowired
|
||||
BookService bookService;
|
||||
|
||||
/**
|
||||
* 获取 Book 列表
|
||||
* 处理 "/book" 的 GET 请求,用来获取 Book 列表
|
||||
*/
|
||||
@RequestMapping(method = RequestMethod.GET)
|
||||
public List<Book> getBookList() {
|
||||
return bookService.findAll();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 Book
|
||||
* 处理 "/book/{id}" 的 GET 请求,用来获取 Book 信息
|
||||
*/
|
||||
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
|
||||
public Book getBook(@PathVariable Long id) {
|
||||
return bookService.findById(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建 Book
|
||||
* 处理 "/book/create" 的 POST 请求,用来新建 Book 信息
|
||||
* 通过 @RequestBody 绑定实体参数,也通过 @RequestParam 传递参数
|
||||
*/
|
||||
@RequestMapping(value = "/create", method = RequestMethod.POST)
|
||||
public Book postBook(@RequestBody Book book) {
|
||||
return bookService.insertByBook(book);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新 Book
|
||||
* 处理 "/update" 的 PUT 请求,用来更新 Book 信息
|
||||
*/
|
||||
@RequestMapping(value = "/update", method = RequestMethod.PUT)
|
||||
public Book putBook(@RequestBody Book book) {
|
||||
return bookService.update(book);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除 Book
|
||||
* 处理 "/book/{id}" 的 GET 请求,用来删除 Book 信息
|
||||
*/
|
||||
@RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
|
||||
public Book deleteBook(@PathVariable Long id) {
|
||||
return bookService.delete(id);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package demo.springboot;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class WebApplicationTests {
|
||||
|
||||
@Test
|
||||
public void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user