ssm框架实现网页的增删改查源码
时间: 2023-08-30 20:07:01 浏览: 110
Maven+SSM框架实现简单的增删改查
好的,以下是一个简单的SSM框架实现网页的增删改查的示例代码。
1. 实体类
User.java
```java
public class User {
private Integer id;
private String name;
private Integer age;
private String email;
// getter and setter methods
}
```
2. Mapper接口
UserMapper.java
```java
public interface UserMapper {
List<User> getAllUsers();
User getUserById(Integer id);
int addUser(User user);
int updateUser(User user);
int deleteUser(Integer id);
}
```
3. Mapper映射文件
UserMapper.xml
```xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.mapper.UserMapper">
<select id="getAllUsers" resultType="User">
select * from user
</select>
<select id="getUserById" resultType="User">
select * from user where id = #{id}
</select>
<insert id="addUser" parameterType="User">
insert into user(name, age, email) values(#{name}, #{age}, #{email})
</insert>
<update id="updateUser" parameterType="User">
update user set name = #{name}, age = #{age}, email = #{email} where id = #{id}
</update>
<delete id="deleteUser" parameterType="Integer">
delete from user where id = #{id}
</delete>
</mapper>
```
4. Service接口及实现类
UserService.java
```java
public interface UserService {
List<User> getAllUsers();
User getUserById(Integer id);
boolean addUser(User user);
boolean updateUser(User user);
boolean deleteUser(Integer id);
}
```
UserServiceImpl.java
```java
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public List<User> getAllUsers() {
return userMapper.getAllUsers();
}
@Override
public User getUserById(Integer id) {
return userMapper.getUserById(id);
}
@Override
public boolean addUser(User user) {
return userMapper.addUser(user) > 0;
}
@Override
public boolean updateUser(User user) {
return userMapper.updateUser(user) > 0;
}
@Override
public boolean deleteUser(Integer id) {
return userMapper.deleteUser(id) > 0;
}
}
```
5. Controller类
UserController.java
```java
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/list")
public String list(Model model) {
List<User> userList = userService.getAllUsers();
model.addAttribute("userList", userList);
return "user/list";
}
@GetMapping("/add")
public String add(Model model) {
model.addAttribute("user", new User());
return "user/edit";
}
@GetMapping("/edit/{id}")
public String edit(@PathVariable Integer id, Model model) {
User user = userService.getUserById(id);
model.addAttribute("user", user);
return "user/edit";
}
@PostMapping("/save")
public String save(User user) {
if (user.getId() == null) {
userService.addUser(user);
} else {
userService.updateUser(user);
}
return "redirect:/user/list";
}
@GetMapping("/delete/{id}")
public String delete(@PathVariable Integer id) {
userService.deleteUser(id);
return "redirect:/user/list";
}
}
```
6. JSP页面
list.jsp
```jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>用户列表</title>
</head>
<body>
<table>
<thead>
<tr>
<th>ID</th>
<th>姓名</th>
<th>年龄</th>
<th>邮箱</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<c:forEach items="${userList}" var="user">
<tr>
<td>${user.id}</td>
<td>${user.name}</td>
<td>${user.age}</td>
<td>${user.email}</td>
<td>
<a href="/user/edit/${user.id}">编辑</a>
<a href="/user/delete/${user.id}">删除</a>
</td>
</tr>
</c:forEach>
</tbody>
</table>
<a href="/user/add">添加用户</a>
</body>
</html>
```
edit.jsp
```jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>编辑用户</title>
</head>
<body>
<form method="post" action="/user/save">
<input type="hidden" name="id" value="${user.id}"/>
姓名:<input type="text" name="name" value="${user.name}"/><br/>
年龄:<input type="text" name="age" value="${user.age}"/><br/>
邮箱:<input type="text" name="email" value="${user.email}"/><br/>
<input type="submit" value="保存"/>
</form>
</body>
</html>
```
以上是一个简单的SSM框架实现网页的增删改查的示例代码,您可以根据您的需要进行修改和完善。希望对您有所帮助!
阅读全文