基于MVC框架来实现一个图书管理系统写出代码
时间: 2024-03-02 12:50:41 浏览: 74
基于MVC模式的图书管理系统
好的,下面我来给你展示一个基于MVC框架实现的简单图书管理系统的代码。这里使用的是Java语言和Spring MVC框架。
1. 模型层(Book.java)
```java
public class Book {
private int id;
private String title;
private String author;
private String publisher;
private Date publishDate;
private double price;
// 省略getter和setter方法
}
```
2. 视图层(bookList.jsp)
```html
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>图书列表</title>
</head>
<body>
<h1>图书列表</h1>
<table>
<tr>
<th>ID</th>
<th>书名</th>
<th>作者</th>
<th>出版社</th>
<th>出版日期</th>
<th>价格</th>
</tr>
<c:forEach var="book" items="${bookList}">
<tr>
<td>${book.id}</td>
<td>${book.title}</td>
<td>${book.author}</td>
<td>${book.publisher}</td>
<td><fmt:formatDate value="${book.publishDate}" pattern="yyyy-MM-dd"/></td>
<td>${book.price}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
```
3. 控制器层(BookController.java)
```java
@Controller
@RequestMapping("/book")
public class BookController {
@Autowired
private BookService bookService;
@RequestMapping(value="/list", method=RequestMethod.GET)
public String list(Model model) {
List<Book> bookList = bookService.getAllBooks();
model.addAttribute("bookList", bookList);
return "bookList";
}
@RequestMapping(value="/add", method=RequestMethod.GET)
public String add(Model model) {
return "addBook";
}
@RequestMapping(value="/add", method=RequestMethod.POST)
public String add(Book book) {
bookService.addBook(book);
return "redirect:/book/list";
}
@RequestMapping(value="/edit/{id}", method=RequestMethod.GET)
public String edit(@PathVariable int id, Model model) {
Book book = bookService.getBookById(id);
model.addAttribute("book", book);
return "editBook";
}
@RequestMapping(value="/edit/{id}", method=RequestMethod.POST)
public String edit(@PathVariable int id, Book book) {
book.setId(id);
bookService.updateBook(book);
return "redirect:/book/list";
}
@RequestMapping(value="/delete/{id}", method=RequestMethod.GET)
public String delete(@PathVariable int id) {
bookService.deleteBook(id);
return "redirect:/book/list";
}
}
```
4. 服务层(BookService.java)
```java
public interface BookService {
public List<Book> getAllBooks();
public Book getBookById(int id);
public void addBook(Book book);
public void updateBook(Book book);
public void deleteBook(int id);
}
```
```java
@Service
public class BookServiceImpl implements BookService {
@Autowired
private BookDAO bookDAO;
@Override
public List<Book> getAllBooks() {
return bookDAO.getAllBooks();
}
@Override
public Book getBookById(int id) {
return bookDAO.getBookById(id);
}
@Override
public void addBook(Book book) {
bookDAO.addBook(book);
}
@Override
public void updateBook(Book book) {
bookDAO.updateBook(book);
}
@Override
public void deleteBook(int id) {
bookDAO.deleteBook(id);
}
}
```
5. 数据访问层(BookDAO.java)
```java
public interface BookDAO {
public List<Book> getAllBooks();
public Book getBookById(int id);
public void addBook(Book book);
public void updateBook(Book book);
public void deleteBook(int id);
}
```
```java
@Repository
public class BookDAOImpl implements BookDAO {
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public List<Book> getAllBooks() {
String sql = "SELECT * FROM book";
return jdbcTemplate.query(sql, new BookRowMapper());
}
@Override
public Book getBookById(int id) {
String sql = "SELECT * FROM book WHERE id=?";
return jdbcTemplate.queryForObject(sql, new Object[] { id }, new BookRowMapper());
}
@Override
public void addBook(Book book) {
String sql = "INSERT INTO book VALUES(null,?,?,?,?,?)";
jdbcTemplate.update(sql, book.getTitle(), book.getAuthor(), book.getPublisher(),
book.getPublishDate(), book.getPrice());
}
@Override
public void updateBook(Book book) {
String sql = "UPDATE book SET title=?, author=?, publisher=?, publish_date=?, price=? WHERE id=?";
jdbcTemplate.update(sql, book.getTitle(), book.getAuthor(), book.getPublisher(),
book.getPublishDate(), book.getPrice(), book.getId());
}
@Override
public void deleteBook(int id) {
String sql = "DELETE FROM book WHERE id=?";
jdbcTemplate.update(sql, id);
}
private class BookRowMapper implements RowMapper<Book> {
@Override
public Book mapRow(ResultSet rs, int rowNum) throws SQLException {
Book book = new Book();
book.setId(rs.getInt("id"));
book.setTitle(rs.getString("title"));
book.setAuthor(rs.getString("author"));
book.setPublisher(rs.getString("publisher"));
book.setPublishDate(rs.getDate("publish_date"));
book.setPrice(rs.getDouble("price"));
return book;
}
}
}
```
以上就是一个基于MVC框架实现的简单图书管理系统的代码。需要注意的是,这里只是提供了一个简单的参考实现,实际开发中还需要根据具体需求进行适当的修改和优化。
阅读全文