帮我用java+vue实现一个简单的图书管理系统
时间: 2023-05-23 21:05:56 浏览: 96
基于vue开发的图书管理系统
A:
首先,你可以使用Java编写后端,使用Vue编写前端。下面是一个示例:
1. 后端代码:
```
package com.example.library;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class LibraryApplication {
public static void main(String[] args) {
SpringApplication.run(LibraryApplication.class, args);
}
}
```
```
package com.example.library.controller;
import com.example.library.entity.Book;
import com.example.library.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/api/v1/books")
public class BookController {
@Autowired
private BookService bookService;
@GetMapping
public ResponseEntity<List<Book>> getBooks() {
List<Book> books = bookService.getBooks();
return new ResponseEntity<>(books, HttpStatus.OK);
}
@PostMapping
public ResponseEntity<Book> addBook(@RequestBody Book book) {
Book addedBook = bookService.addBook(book);
return new ResponseEntity<>(addedBook, HttpStatus.CREATED);
}
@PutMapping("/{id}")
public ResponseEntity<Book> updateBook(@PathVariable Long id, @RequestBody Book book) {
Book updatedBook = bookService.updateBook(id, book);
return new ResponseEntity<>(updatedBook, HttpStatus.OK);
}
@DeleteMapping("/{id}")
public ResponseEntity<Void> deleteBook(@PathVariable Long id) {
bookService.deleteBook(id);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
}
```
```
package com.example.library.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.*;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "books")
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String title;
private String author;
private String publisher;
}
```
```
package com.example.library.repository;
import com.example.library.entity.Book;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.Optional;
public interface BookRepository extends JpaRepository<Book, Long> {
Optional<Book> findById(Long id);
}
```
```
package com.example.library.service;
import com.example.library.entity.Book;
import com.example.library.exception.ResourceNotFoundException;
import com.example.library.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> getBooks() {
return bookRepository.findAll();
}
public Book addBook(Book book) {
return bookRepository.save(book);
}
public Book updateBook(Long id, Book newBook) {
Book oldBook = bookRepository.findById(id).orElseThrow(() -> new ResourceNotFoundException("Book not found"));
oldBook.setTitle(newBook.getTitle());
oldBook.setAuthor(newBook.getAuthor());
oldBook.setPublisher(newBook.getPublisher());
return bookRepository.save(oldBook);
}
public void deleteBook(Long id) {
bookRepository.deleteById(id);
}
}
```
2. 前端代码:
```
<template>
<div>
<table>
<thead>
<tr>
<th>ID</th>
<th>Title</th>
<th>Author</th>
<th>Publisher</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<tr v-for="book in books" :key="book.id">
<td>{{ book.id }}</td>
<td>{{ book.title }}</td>
<td>{{ book.author }}</td>
<td>{{ book.publisher }}</td>
<td>
<button @click="editBook(book)">Edit</button>
<button @click="deleteBook(book.id)">Delete</button>
</td>
</tr>
</tbody>
</table>
<div v-if="showForm">
<h2 v-if="!editMode">Add book</h2>
<h2 v-if="editMode">Edit book</h2>
<form @submit.prevent="submitForm">
<label>Title:</label>
<input type="text" v-model="book.title" required>
<label>Author:</label>
<input type="text" v-model="book.author" required>
<label>Publisher:</label>
<input type="text" v-model="book.publisher" required>
<button type="submit">{{ editMode ? 'Save changes' : 'Add' }}</button>
</form>
</div>
<button @click="showForm = true">Add book</button>
</div>
</template>
<script>
import axios from 'axios';
export default {
data() {
return {
books: [],
showForm: false,
editMode: false,
book: {
title: '',
author: '',
publisher: '',
},
};
},
created() {
this.getBooks();
},
methods: {
async getBooks() {
const { data } = await axios.get('/api/v1/books');
this.books = data;
},
async addBook(book) {
await axios.post('/api/v1/books', book);
this.getBooks();
},
async updateBook(id, book) {
await axios.put(`/api/v1/books/${id}`, book);
this.getBooks();
},
async deleteBook(id) {
await axios.delete(`/api/v1/books/${id}`);
this.getBooks();
},
editBook(book) {
this.editMode = true;
this.showForm = true;
this.book = { ...book };
},
async submitForm() {
if (this.editMode) {
await this.updateBook(this.book.id, this.book);
} else {
await this.addBook(this.book);
}
this.showForm = false;
this.editMode = false;
this.book = {
title: '',
author: '',
publisher: '',
};
},
},
};
</script>
```
请注意,这只是一个简单的示例,可以根据您的需要进行更改和修改。您还需要相应配置您的数据库和后端框架以进行完整的实现。
阅读全文