当前位置: 首页 > wzjs >正文

信阳市网站建设公司百度浏览官网

信阳市网站建设公司,百度浏览官网,网络运营公司的经营范围,minty wordpress使用 Spring Boot 框架实现图书管理系统可以按照以下步骤进行,涵盖了从项目搭建、数据库设计、后端接口开发到前端页面展示的整个流程。 1. 项目搭建 可以使用 Spring Initializr(https://start.spring.io/ )来快速创建一个 Spring Boot 项目…

使用 Spring Boot 框架实现图书管理系统可以按照以下步骤进行,涵盖了从项目搭建、数据库设计、后端接口开发到前端页面展示的整个流程。

1. 项目搭建

可以使用 Spring Initializr(https://start.spring.io/ )来快速创建一个 Spring Boot 项目,选择以下依赖:

  • Spring Web:用于构建 RESTful API 和 Web 应用。
  • Spring Data JPA:用于简化数据库操作。
  • MySQL Driver:如果使用 MySQL 数据库。
  • Thymeleaf:作为模板引擎来构建前端页面。

2. 数据库设计

设计图书管理系统的数据库,主要涉及两个实体:图书(Book)和用户(User),这里以 MySQL 为例创建相应的表。

2.1 创建数据库

收起

sql

CREATE DATABASE book_management;
USE book_management;
2.2 创建图书表

收起

sql

CREATE TABLE books (id INT AUTO_INCREMENT PRIMARY KEY,title VARCHAR(255) NOT NULL,author VARCHAR(255) NOT NULL,isbn VARCHAR(20) UNIQUE
);

3. 配置数据库连接

在 src/main/resources/application.properties 中配置数据库连接信息:

收起

properties

spring.datasource.url=jdbc:mysql://localhost:3306/book_management
spring.datasource.username=root
spring.datasource.password=your_password
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true

4. 创建实体类

在 src/main/java 下创建相应的实体类,例如 Book 类:

收起

java

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;@Entity
public class Book {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)private Long id;private String title;private String author;private String isbn;// 无参构造函数public Book() {}// 有参构造函数public Book(String title, String author, String isbn) {this.title = title;this.author = author;this.isbn = isbn;}// Getter 和 Setter 方法public Long getId() {return id;}public void setId(Long id) {this.id = id;}public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}public String getAuthor() {return author;}public void setAuthor(String author) {this.author = author;}public String getIsbn() {return isbn;}public void setIsbn(String isbn) {this.isbn = isbn;}
}

5. 创建数据访问层(Repository)

创建一个 BookRepository 接口,继承 JpaRepository 来实现对 Book 实体的基本数据库操作:

收起

java

import org.springframework.data.jpa.repository.JpaRepository;public interface BookRepository extends JpaRepository<Book, Long> {
}

6. 创建服务层(Service)

创建一个 BookService 类来处理图书的业务逻辑:

收起

java

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;@Service
public class BookService {@Autowiredprivate BookRepository bookRepository;public List<Book> getAllBooks() {return bookRepository.findAll();}public Book saveBook(Book book) {return bookRepository.save(book);}public Book getBookById(Long id) {return bookRepository.findById(id).orElse(null);}public void deleteBook(Long id) {bookRepository.deleteById(id);}
}

7. 创建控制器层(Controller)

创建一个 BookController 类来处理 HTTP 请求:

收起

java

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;import java.util.List;@Controller
@RequestMapping("/books")
public class BookController {@Autowiredprivate BookService bookService;@GetMappingpublic String getAllBooks(Model model) {List<Book> books = bookService.getAllBooks();model.addAttribute("books", books);return "books";}@GetMapping("/add")public String showAddBookForm(Model model) {model.addAttribute("book", new Book());return "add-book";}@PostMapping("/add")public String addBook(@ModelAttribute Book book) {bookService.saveBook(book);return "redirect:/books";}@GetMapping("/edit/{id}")public String showEditBookForm(@PathVariable Long id, Model model) {Book book = bookService.getBookById(id);model.addAttribute("book", book);return "edit-book";}@PostMapping("/edit/{id}")public String editBook(@PathVariable Long id, @ModelAttribute Book book) {book.setId(id);bookService.saveBook(book);return "redirect:/books";}@GetMapping("/delete/{id}")public String deleteBook(@PathVariable Long id) {bookService.deleteBook(id);return "redirect:/books";}
}

8. 创建前端页面

在 src/main/resources/templates 目录下创建相应的 Thymeleaf 模板页面。

8.1 books.html

收起

html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head><title>图书列表</title>
</head>
<body><h1>图书列表</h1><a href="/books/add">添加图书</a><table><thead><tr><th>ID</th><th>标题</th><th>作者</th><th>ISBN</th><th>操作</th></tr></thead><tbody><tr th:each="book : ${books}"><td th:text="${book.id}"></td><td th:text="${book.title}"></td><td th:text="${book.author}"></td><td th:text="${book.isbn}"></td><td><a th:href="@{/books/edit/{id}(id=${book.id})}">编辑</a><a th:href="@{/books/delete/{id}(id=${book.id})}">删除</a></td></tr></tbody></table>
</body>
</html>
8.2 add-book.html

收起

html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head><title>添加图书</title>
</head>
<body><h1>添加图书</h1><form method="post" th:action="@{/books/add}" th:object="${book}"><label for="title">标题:</label><input type="text" id="title" th:field="*{title}" required><br><label for="author">作者:</label><input type="text" id="author" th:field="*{author}" required><br><label for="isbn">ISBN:</label><input type="text" id="isbn" th:field="*{isbn}" required><br><input type="submit" value="添加"></form><a href="/books">返回图书列表</a>
</body>
</html>
8.3 edit-book.html

收起

html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head><title>编辑图书</title>
</head>
<body><h1>编辑图书</h1><form method="post" th:action="@{/books/edit/{id}(id=${book.id})}" th:object="${book}"><label for="title">标题:</label><input type="text" id="title" th:field="*{title}" required><br><label for="author">作者:</label><input type="text" id="author" th:field="*{author}" required><br><label for="isbn">ISBN:</label><input type="text" id="isbn" th:field="*{isbn}" required><br><input type="submit" value="保存"></form><a href="/books">返回图书列表</a>
</body>
</html>

9. 启动应用程序

创建一个主应用类,通常命名为 BookManagementSystemApplication,并添加 @SpringBootApplication 注解:

收起

java

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class BookManagementSystemApplication {public static void main(String[] args) {SpringApplication.run(BookManagementSystemApplication.class, args);}
}

运行 main 方法启动 Spring Boot 应用程序,访问 http://localhost:8080/books 即可看到图书列表页面,并且可以进行图书的添加、编辑和删除操作。

http://www.dtcms.com/wzjs/315354.html

相关文章:

  • 成都个人做网站促销活动推广方案
  • 做外贸比较好的网站有哪些培训心得体会1000字通用
  • 设计网站printerest怎么做网站排名
  • 腾讯云wed服务器做网站友情链接样式
  • 广西南宁疫情最新消息今天封城了北京网站优化策略
  • 品牌建设归哪个部门管seo链接优化建议
  • 程序员和网站建设沈阳网络优化培训
  • 做盗版小说网站赚钱嘛快速排名程序
  • 保定网站制作套餐百度云
  • 电梯网站建设淘宝补流量平台
  • 日照分析网站今日头条热搜榜
  • 单位的网站建设费会计处理公司优化是什么意思
  • 旅游门户网站系统代运营公司靠谱吗
  • 石家庄专门做网站的公司seo分析是什么意思
  • 思勤传媒网站建设公司郑州seo全网营销
  • 做搜狗网站快速排名建立一个企业网站需要多少钱
  • 如何建立网上商城seo培训
  • 云主机上传网站外贸推广营销公司
  • 互联云主机郑州seo线下培训
  • 怎么自己做淘宝客网站百度一键优化
  • 企业做网站的合同银川网页设计公司
  • 南漳网站设计市场监督管理局是干什么的
  • 网站首页怎么制作合肥网站制作公司
  • 淄博网站建设团队短视频入口seo
  • 大连做网站那个公司最好合肥网站快速优化排名
  • 校园门户网站建设特点加快百度收录的方法
  • 绍兴 网站制作网络营销策划模板
  • 商城网站建设服务网络推广公司北京
  • 用ul做的网站为何浮动不上去代运营电商公司排行榜
  • 专业的河南网站建设公司百度搜索风云榜人物