6.1.1 创建web工程
使用IDEA创建书籍管理工程:
并生成pom.xml文件:
<?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
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.3.RELEASE</version>
<relativePath/>
</parent>
<groupId>com.marshal.springcloud</groupId>
<artifactId>book</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>book</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR8</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
6.1.2 编写工程配置文件
编写工程配置文件“application.yml”:
将该配置文件编写内容复制粘贴到云Git服务:
其中主配置文件内容:
删除原配置文件并编写“bootstrap.yml”配置文件:
6.1.3 修改启动入口类
修改工程的启动入口类,添加配置中心及注册中心客户端注解:
6.1.4 创建应用服务源码
创建应用相关开发包:
创建实体类:
package com.marshal.springcloud.book.entity;
import javax.persistence.*;
/**
* @Author 王毅
* @Date 2020/8/31 9:07
*/
@Entity
@Table(name = "book")
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "book_cover")
private String bookCover;
@Column(name = "book_type_id")
private Integer bookTypeId;
@Column(name = "author")
private String author;
@Column(name = "publisher")
private String publisher;
@Column(name = "book_name")
private String bookName;
@Column(name = "publish_date")
private String publishDate;
@Column(name = "version")
private String version;
@Column(name = "read_page_code")
private Integer readPageCode;
@Column(name = "is_borrow_out")
private Integer isBorrowOut;
@Column(name = "borrower")
private String borrower;
@Column(name = "is_finished")
private Integer isFinished;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getBookCover() {
return bookCover;
}
public void setBookCover(String bookCover) {
this.bookCover = bookCover;
}
public Integer getBookTypeId() {
return bookTypeId;
}
public void setBookTypeId(Integer bookTypeId) {
this.bookTypeId = bookTypeId;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public String getPublishDate() {
return publishDate;
}
public void setPublishDate(String publishDate) {
this.publishDate = publishDate;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public Integer getReadPageCode() {
return readPageCode;
}
public void setReadPageCode(Integer readPageCode) {
this.readPageCode = readPageCode;
}
public Integer getIsBorrowOut() {
return isBorrowOut;
}
public void setIsBorrowOut(Integer isBorrowOut) {
this.isBorrowOut = isBorrowOut;
}
public String getBorrower() {
return borrower;
}
public void setBorrower(String borrower) {
this.borrower = borrower;
}
public Integer getIsFinished() {
return isFinished;
}
public void setIsFinished(Integer isFinished) {
this.isFinished = isFinished;
}
@Override
public String toString() {
return "Book{" +
"id=" + id +
", bookCover='" + bookCover + '\'' +
", bookTypeId=" + bookTypeId +
", author='" + author + '\'' +
", publisher='" + publisher + '\'' +
", bookName='" + bookName + '\'' +
", publishDate='" + publishDate + '\'' +
", version='" + version + '\'' +
", readPageCode=" + readPageCode +
", isBorrowOut=" + isBorrowOut +
", borrower='" + borrower + '\'' +
", isFinished=" + isFinished +
'}';
}
}
创建访问数据库服务接口:
package com.marshal.springcloud.book.repository;
import com.marshal.springcloud.book.entity.Book;
import org.springframework.data.jpa.repository.JpaRepository;
/**
* @Author 王毅
* @Date 2020/8/31 9:15
*/
public interface BookRepository extends JpaRepository<Book, Long> {
// 使用findAll()方法;
}
创建数据库服务类:
package com.marshal.springcloud.book.service;
import com.marshal.springcloud.book.entity.Book;
import com.marshal.springcloud.book.repository.BookRepository;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
/**
* @Author 王毅
* @Date 2020/8/31 10:07
*/
@Service
public class BookService {
@Resource
private BookRepository bookRepository;
public List<Book> findAll() {
return bookRepository.findAll();
}
}
创建数据库服务控制器类:
package com.marshal.springcloud.book.controller;
import com.marshal.springcloud.book.entity.Book;
import com.marshal.springcloud.book.service.BookService;
import jdk.nashorn.internal.objects.annotations.Getter;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @Author 王毅
* @Date 2020/8/31 10:09
*/
@RestController
public class BookController {
@Resource
private BookService bookService;
@GetMapping("/booklist")
public Map bookList() {
Map result = new HashMap();
try {
List bookList = bookService.findAll();
result.put("code", 0);
result.put("message", "success");
result.put("data", bookList);
} catch (Exception e) {
e.printStackTrace();
result.put("code", e.getClass().getSimpleName());
result.put("message", e.getMessage());
}
return result;
}
}
6.1.5 启动服务并测试
启动微服务并测试: