用javaweb和idea写一个图书管理系统
时间: 2023-06-26 15:06:23 浏览: 184
好的,这里提供一个简单的图书管理系统的示例,基于JavaWeb和IDEA开发。
首先,需要准备以下环境:
- JDK 1.8+
- Tomcat 7+
- MySQL 5.6+
- IDEA
接下来,按照以下步骤进行开发:
1. 创建Maven项目
在IDEA中创建Maven项目,并添加以下依赖:
```xml
<dependencies>
<!-- Servlet API -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<!-- JSP API -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
<!-- JSTL -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- MySQL JDBC driver -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.49</version>
</dependency>
</dependencies>
```
2. 创建数据库
在MySQL中创建一个名为`library`的数据库,并创建以下表格:
```sql
CREATE TABLE `book` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
`author` varchar(50) NOT NULL,
`price` double NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
```
3. 编写JavaBean类
创建一个名为`Book`的JavaBean类,用于封装图书信息。
```java
public class Book {
private int id;
private String name;
private String author;
private double price;
// getter and setter methods
}
```
4. 编写DAO类
创建一个名为`BookDAO`的DAO类,用于访问数据库,并提供以下方法:
```java
public class BookDAO {
// 获取所有图书信息
public List<Book> getAllBooks() {
// TODO: 查询所有图书信息并返回
}
// 根据ID获取图书信息
public Book getBookById(int id) {
// TODO: 根据ID查询图书信息并返回
}
// 添加图书信息
public void addBook(Book book) {
// TODO: 添加图书信息到数据库
}
// 更新图书信息
public void updateBook(Book book) {
// TODO: 更新图书信息到数据库
}
// 删除图书信息
public void deleteBook(int id) {
// TODO: 根据ID删除图书信息
}
}
```
5. 编写Servlet类
创建一个名为`BookServlet`的Servlet类,用于处理HTTP请求,并调用DAO类中的方法。
```java
@WebServlet("/book")
public class BookServlet extends HttpServlet {
private BookDAO bookDAO = new BookDAO();
// 处理GET请求
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String action = request.getParameter("action");
if ("add".equals(action)) {
// 转发到添加图书页面
request.getRequestDispatcher("/jsp/add.jsp").forward(request, response);
} else if ("edit".equals(action)) {
// 根据ID获取图书信息,并转发到编辑图书页面
int id = Integer.parseInt(request.getParameter("id"));
Book book = bookDAO.getBookById(id);
request.setAttribute("book", book);
request.getRequestDispatcher("/jsp/edit.jsp").forward(request, response);
} else if ("delete".equals(action)) {
// 根据ID删除图书信息,并重定向到图书列表页面
int id = Integer.parseInt(request.getParameter("id"));
bookDAO.deleteBook(id);
response.sendRedirect(request.getContextPath() + "/book");
} else {
// 获取所有图书信息,并转发到图书列表页面
List<Book> books = bookDAO.getAllBooks();
request.setAttribute("books", books);
request.getRequestDispatcher("/jsp/list.jsp").forward(request, response);
}
}
// 处理POST请求
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String action = request.getParameter("action");
if ("add".equals(action)) {
// 获取表单提交的图书信息,并添加到数据库
String name = request.getParameter("name");
String author = request.getParameter("author");
double price = Double.parseDouble(request.getParameter("price"));
Book book = new Book();
book.setName(name);
book.setAuthor(author);
book.setPrice(price);
bookDAO.addBook(book);
response.sendRedirect(request.getContextPath() + "/book");
} else if ("edit".equals(action)) {
// 获取表单提交的图书信息,并更新到数据库
int id = Integer.parseInt(request.getParameter("id"));
String name = request.getParameter("name");
String author = request.getParameter("author");
double price = Double.parseDouble(request.getParameter("price"));
Book book = new Book();
book.setId(id);
book.setName(name);
book.setAuthor(author);
book.setPrice(price);
bookDAO.updateBook(book);
response.sendRedirect(request.getContextPath() + "/book");
}
}
}
```
6. 编写JSP页面
创建以下JSP页面,用于展示图书信息和处理表单提交:
- list.jsp:展示所有图书信息
```jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>图书列表</title>
</head>
<body>
<h1>图书列表</h1>
<p><a href="${pageContext.request.contextPath}/book?action=add">添加图书</a></p>
<table border="1">
<thead>
<tr>
<th>ID</th>
<th>书名</th>
<th>作者</th>
<th>价格</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<c:forEach items="${books}" var="book">
<tr>
<td>${book.id}</td>
<td>${book.name}</td>
<td>${book.author}</td>
<td>${book.price}</td>
<td>
<a href="${pageContext.request.contextPath}/book?action=edit&id=${book.id}">编辑</a>
<a href="${pageContext.request.contextPath}/book?action=delete&id=${book.id}">删除</a>
</td>
</tr>
</c:forEach>
</tbody>
</table>
</body>
</html>
```
- add.jsp:添加图书表单页面
```jsp
<%@ 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>
<form action="${pageContext.request.contextPath}/book" method="post">
<input type="hidden" name="action" value="add">
<p>
书名:<input type="text" name="name">
</p>
<p>
作者:<input type="text" name="author">
</p>
<p>
价格:<input type="text" name="price">
</p>
<p>
<input type="submit" value="添加">
</p>
</form>
</body>
</html>
```
- edit.jsp:编辑图书表单页面
```jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>编辑图书</title>
</head>
<body>
<h1>编辑图书</h1>
<form action="${pageContext.request.contextPath}/book" method="post">
<input type="hidden" name="action" value="edit">
<input type="hidden" name="id" value="${book.id}">
<p>
书名:<input type="text" name="name" value="${book.name}">
</p>
<p>
作者:<input type="text" name="author" value="${book.author}">
</p>
<p>
价格:<input type="text" name="price" value="${book.price}">
</p>
<p>
<input type="submit" value="保存">
</p>
</form>
</body>
</html>
```
7. 部署项目
将项目打包成WAR文件,并部署到Tomcat服务器上。
完成后,启动Tomcat服务器,访问`http://localhost:8080/图书管理系统名称/book`即可查看图书列表。可以点击“添加图书”按钮添加图书信息,也可以点击“编辑”、“删除”按钮编辑或删除图书信息。
阅读全文