springMVC增删改查
时间: 2023-10-28 19:03:16 浏览: 93
springmvc增删改查
Spring MVC 是一个基于 Spring 框架实现的 MVC 框架,它主要用于创建 Web 应用程序。下面是 Spring MVC 实现增删改查的基本步骤:
1. 配置 Spring MVC 的 DispatcherServlet
在 web.xml 中配置 DispatcherServlet,指定其处理请求的 URL 匹配模式和 Spring 配置文件的位置。
2. 编写控制器类
编写控制器类,使用 @Controller 注解标注该类,使用 @RequestMapping 注解标注处理请求的方法。
3. 编写数据访问代码
编写数据访问代码,使用 Spring 的数据访问框架(如 Spring JDBC、Spring ORM)进行数据的增删改查操作。
4. 编写视图模板
编写视图模板,使用 JSP、Thymeleaf 等模板引擎将数据渲染成 HTML 页面。
具体实现步骤可以参考以下示例代码:
1. 配置 DispatcherServlet
在 web.xml 中配置 DispatcherServlet:
```xml
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/springmvc-servlet.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
```
2. 编写控制器类
```java
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping(value = "/list", method = RequestMethod.GET)
public ModelAndView list() {
ModelAndView modelAndView = new ModelAndView("user/list");
List<User> userList = userService.findAll();
modelAndView.addObject("userList", userList);
return modelAndView;
}
@RequestMapping(value = "/add", method = RequestMethod.GET)
public ModelAndView addForm() {
ModelAndView modelAndView = new ModelAndView("user/add");
modelAndView.addObject("user", new User());
return modelAndView;
}
@RequestMapping(value = "/add", method = RequestMethod.POST)
public String add(User user) {
userService.save(user);
return "redirect:/user/list";
}
@RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
public ModelAndView editForm(@PathVariable("id") Long id) {
ModelAndView modelAndView = new ModelAndView("user/edit");
User user = userService.findById(id);
modelAndView.addObject("user", user);
return modelAndView;
}
@RequestMapping(value = "/edit/{id}", method = RequestMethod.POST)
public String edit(User user) {
userService.update(user);
return "redirect:/user/list";
}
@RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
public String delete(@PathVariable("id") Long id) {
userService.delete(id);
return "redirect:/user/list";
}
}
```
3. 编写数据访问代码
```java
@Repository
public class UserDaoImpl implements UserDao {
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public List<User> findAll() {
String sql = "SELECT * FROM user";
return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(User.class));
}
@Override
public User findById(Long id) {
String sql = "SELECT * FROM user WHERE id = ?";
return jdbcTemplate.queryForObject(sql, new Object[]{id}, new BeanPropertyRowMapper<>(User.class));
}
@Override
public void save(User user) {
String sql = "INSERT INTO user(name, age) VALUES(?, ?)";
jdbcTemplate.update(sql, user.getName(), user.getAge());
}
@Override
public void update(User user) {
String sql = "UPDATE user SET name = ?, age = ? WHERE id = ?";
jdbcTemplate.update(sql, user.getName(), user.getAge(), user.getId());
}
@Override
public void delete(Long id) {
String sql = "DELETE FROM user WHERE id = ?";
jdbcTemplate.update(sql, id);
}
}
```
4. 编写视图模板
list.jsp:
```html
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>User List</title>
</head>
<body>
<h1>User List</h1>
<table>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Age</th>
<th>Operation</th>
</tr>
</thead>
<tbody>
<c:forEach var="user" items="${userList}">
<tr>
<td>${user.id}</td>
<td>${user.name}</td>
<td>${user.age}</td>
<td>
<a href="/user/edit/${user.id}">Edit</a>
<a href="/user/delete/${user.id}">Delete</a>
</td>
</tr>
</c:forEach>
</tbody>
</table>
<p><a href="/user/add">Add User</a></p>
</body>
</html>
```
add.jsp:
```html
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Add User</title>
</head>
<body>
<h1>Add User</h1>
<form action="/user/add" method="post">
<label>Name:</label>
<input type="text" name="name" /><br/>
<label>Age:</label>
<input type="text" name="age" /><br/>
<input type="submit" value="Save" />
</form>
</body>
</html>
```
edit.jsp:
```html
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Edit User</title>
</head>
<body>
<h1>Edit User</h1>
<form action="/user/edit/${user.id}" method="post">
<input type="hidden" name="id" value="${user.id}" />
<label>Name:</label>
<input type="text" name="name" value="${user.name}" /><br/>
<label>Age:</label>
<input type="text" name="age" value="${user.age}" /><br/>
<input type="submit" value="Save" />
</form>
</body>
</html>
```
阅读全文