javaweb实现增删改查mvc超详细
时间: 2023-08-17 20:24:11 浏览: 101
JavaWeb 实现增删改查(CRUD)的操作通常采用 MVC 的设计模式,即 Model-View-Controller。
Model 是指数据模型,负责数据的存储、读取和处理。View 是指视图,负责数据的展示和交互。Controller 是指控制器,负责处理用户请求和调用 Model 和 View。
下面是一个简单的示例:
1. 创建数据模型
在 Java 中,我们可以使用 POJO(Plain Old Java Object)来表示数据模型。例如,我们可以创建一个 User 类,表示用户信息:
```java
public class User {
private int id;
private String name;
private String email;
// Getter and Setter methods
}
```
2. 创建 DAO(Data Access Object)类
DAO 类负责与数据库进行交互,提供对数据的增删改查操作。例如,我们可以创建一个 UserDao 类,提供以下方法:
```java
public interface UserDao {
List<User> list(); // 查询所有用户信息
User get(int id); // 根据 ID 查询用户信息
void add(User user); // 添加用户信息
void update(User user); // 更新用户信息
void delete(int id); // 根据 ID 删除用户信息
}
```
3. 创建 Service 类
Service 类负责业务逻辑的处理,调用 DAO 类提供的方法来实现对数据的操作。例如,我们可以创建一个 UserService 类,提供以下方法:
```java
public class UserService {
private UserDao userDao = new UserDaoImpl();
public List<User> list() {
return userDao.list();
}
public User get(int id) {
return userDao.get(id);
}
public void add(User user) {
userDao.add(user);
}
public void update(User user) {
userDao.update(user);
}
public void delete(int id) {
userDao.delete(id);
}
}
```
4. 创建控制器 Controller
Controller 类负责处理用户请求,调用 Service 类提供的方法来实现对数据的操作。例如,我们可以创建一个 UserController 类,提供以下方法:
```java
public class UserController {
private UserService userService = new UserService();
public void list(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
List<User> userList = userService.list();
request.setAttribute("userList", userList);
request.getRequestDispatcher("/user/list.jsp").forward(request, response);
}
public void add(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
User user = new User();
user.setName(request.getParameter("name"));
user.setEmail(request.getParameter("email"));
userService.add(user);
response.sendRedirect(request.getContextPath() + "/user/list");
}
public void edit(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
int id = Integer.parseInt(request.getParameter("id"));
User user = userService.get(id);
request.setAttribute("user", user);
request.getRequestDispatcher("/user/edit.jsp").forward(request, response);
}
public void update(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
int id = Integer.parseInt(request.getParameter("id"));
User user = userService.get(id);
user.setName(request.getParameter("name"));
user.setEmail(request.getParameter("email"));
userService.update(user);
response.sendRedirect(request.getContextPath() + "/user/list");
}
public void delete(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
int id = Integer.parseInt(request.getParameter("id"));
userService.delete(id);
response.sendRedirect(request.getContextPath() + "/user/list");
}
}
```
5. 创建视图 JSP
JSP(JavaServer Pages)负责展示数据和交互,例如,我们可以创建 list.jsp、add.jsp 和 edit.jsp,分别用于显示用户列表、添加用户和编辑用户信息。
```jsp
<!-- list.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>User List</title>
</head>
<body>
<h1>User List</h1>
<table border="1">
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
<th>Action</th>
</tr>
<c:forEach var="user" items="${userList}">
<tr>
<td>${user.id}</td>
<td>${user.name}</td>
<td>${user.email}</td>
<td>
<a href="<c:url value='/user/edit?id=${user.id}' />">Edit</a>
<a href="<c:url value='/user/delete?id=${user.id}' />">Delete</a>
</td>
</tr>
</c:forEach>
</table>
<a href="<c:url value='/user/add' />">Add User</a>
</body>
</html>
<!-- add.jsp -->
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Add User</title>
</head>
<body>
<h1>Add User</h1>
<form action="<c:url value='/user/add' />" method="post">
Name: <input type="text" name="name" /><br />
Email: <input type="text" name="email" /><br />
<input type="submit" value="Add" />
</form>
<a href="<c:url value='/user/list' />">Back to User List</a>
</body>
</html>
<!-- edit.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>Edit User</title>
</head>
<body>
<h1>Edit User</h1>
<form action="<c:url value='/user/update' />" method="post">
<input type="hidden" name="id" value="${user.id}" />
Name: <input type="text" name="name" value="${user.name}" /><br />
Email: <input type="text" name="email" value="${user.email}" /><br />
<input type="submit" value="Update" />
</form>
<a href="<c:url value='/user/list' />">Back to User List</a>
</body>
</html>
```
6. 配置 web.xml
在 web.xml 中配置 Servlet 和 JSP,例如:
```xml
<servlet>
<servlet-name>user</servlet-name>
<servlet-class>com.example.UserController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>user</servlet-name>
<url-pattern>/user/*</url-pattern>
</servlet-mapping>
<jsp-config>
<jsp-property-group>
<url-pattern>*.jsp</url-pattern>
<page-encoding>UTF-8</page-encoding>
</jsp-property-group>
</jsp-config>
```
这样,我们就完成了 JavaWeb 中使用 MVC 实现增删改查的操作。
阅读全文