基于springboot写一个图书管理系统,附上源代码
时间: 2023-12-07 21:02:34 浏览: 83
springboot+vue酒店预约管理系统
5星 · 资源好评率100%
很高兴能为您提供帮助,以下是一个基于Spring Boot框架开发的简单图书管理系统的源代码:
```
//Book.java
package com.example.demo.entity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String title;
private String author;
private String publisher;
private int publishYear;
public Book() {
}
public Book(String title, String author, String publisher, int publishYear) {
this.title = title;
this.author = author;
this.publisher = publisher;
this.publishYear = publishYear;
}
public int getId() {
return id;
}
public void setId(int 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 getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public int getPublishYear() {
return publishYear;
}
public void setPublishYear(int publishYear) {
this.publishYear = publishYear;
}
}
```
```
//BookRepository.java
package com.example.demo.repository;
import com.example.demo.entity.Book;
import org.springframework.data.jpa.repository.JpaRepository;
public interface BookRepository extends JpaRepository<Book, Integer> {
}
```
```
//BookService.java
package com.example.demo.service;
import com.example.demo.entity.Book;
import com.example.demo.repository.BookRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class BookService {
@Autowired
private BookRepository bookRepository;
public List<Book> getAllBooks() {
return bookRepository.findAll();
}
public Book getBookById(int id) {
return bookRepository.findById(id).orElse(null);
}
public void addBook(Book book) {
bookRepository.save(book);
}
public void updateBook(Book book) {
bookRepository.save(book);
}
public void deleteBook(int id) {
bookRepository.deleteById(id);
}
}
```
```
//BookController.java
package com.example.demo.controller;
import com.example.demo.entity.Book;
import com.example.demo.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/api/books")
public class BookController {
@Autowired
private BookService bookService;
@GetMapping("")
public List<Book> getAllBooks() {
return bookService.getAllBooks();
}
@GetMapping("/{id}")
public Book getBookById(@PathVariable int id) {
return bookService.getBookById(id);
}
@PostMapping("")
public void addBook(@RequestBody Book book) {
bookService.addBook(book);
}
@PutMapping("/{id}")
public void updateBook(@PathVariable int id, @RequestBody Book book) {
book.setId(id);
bookService.updateBook(book);
}
@DeleteMapping("/{id}")
public void deleteBook(@PathVariable int id) {
bookService.deleteBook(id);
}
}
```
```
//DemoApplication.java
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
```
此图书管理系统使用了Spring Boot框架和JPA,提供了基本的增删改查功能。您可以根据自己的需求进行修改和扩展。
阅读全文