帮我用java写一个ssm的增删改查代码,对用户的名字,电话,出生日期进行增删改查
时间: 2023-07-19 22:11:20 浏览: 160
好的,以下是一个简单的SSM增删改查示例代码,对用户的名字,电话和出生日期进行操作:
### 实体类 User.java
```java
public class User {
private int id;
private String name;
private String phone;
private String birthdate;
// getter 和 setter 方法
}
```
### DAO层 UserMapper.java
```java
public interface UserMapper {
List<User> findAll(); // 查询所有用户
User findById(int id); // 根据ID查询用户
void insert(User user); // 添加用户
void update(User user); // 更新用户
void delete(int id); // 根据ID删除用户
}
```
### Service层 UserServiceImpl.java
```java
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public List<User> findAll() {
return userMapper.findAll();
}
@Override
public User findById(int id) {
return userMapper.findById(id);
}
@Override
public void insert(User user) {
userMapper.insert(user);
}
@Override
public void update(User user) {
userMapper.update(user);
}
@Override
public void delete(int id) {
userMapper.delete(id);
}
}
```
### Controller层 UserController.java
```java
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/list")
public String findAll(Model model) {
List<User> userList = userService.findAll();
model.addAttribute("userList", userList);
return "userList";
}
@RequestMapping("/{id}")
public String findById(@PathVariable int id, Model model) {
User user = userService.findById(id);
model.addAttribute("user", user);
return "userDetail";
}
@RequestMapping("/add")
public String add() {
return "userAdd";
}
@RequestMapping(value = "/add", method = RequestMethod.POST)
public String save(User user) {
userService.insert(user);
return "redirect:/user/list";
}
@RequestMapping("/{id}/update")
public String edit(@PathVariable int id, Model model) {
User user = userService.findById(id);
model.addAttribute("user", user);
return "userEdit";
}
@RequestMapping(value = "/update", method = RequestMethod.POST)
public String update(User user) {
userService.update(user);
return "redirect:/user/list";
}
@RequestMapping("/{id}/delete")
public String delete(@PathVariable int id) {
userService.delete(id);
return "redirect:/user/list";
}
}
```
### 页面展示
userList.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.phone}</td>
<td>${user.birthdate}</td>
<td>
<a href="/user/${user.id}">详情</a>
<a href="/user/${user.id}/update">编辑</a>
<a href="/user/${user.id}/delete">删除</a>
</td>
</tr>
</c:forEach>
</tbody>
</table>
<a href="/user/add">添加用户</a>
</body>
</html>
```
userDetail.jsp
```jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>用户详情</title>
</head>
<body>
<table>
<tr>
<td>ID:</td>
<td>${user.id}</td>
</tr>
<tr>
<td>姓名:</td>
<td>${user.name}</td>
</tr>
<tr>
<td>电话:</td>
<td>${user.phone}</td>
</tr>
<tr>
<td>出生日期:</td>
<td>${user.birthdate}</td>
</tr>
</table>
<a href="/user/${user.id}/update">编辑</a>
<a href="/user/${user.id}/delete">删除</a>
<a href="/user/list">返回列表</a>
</body>
</html>
```
userAdd.jsp
```jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>添加用户</title>
</head>
<body>
<form action="/user/add" method="post">
<table>
<tr>
<td>姓名:</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>电话:</td>
<td><input type="text" name="phone"></td>
</tr>
<tr>
<td>出生日期:</td>
<td><input type="text" name="birthdate"></td>
</tr>
</table>
<input type="submit" value="提交">
</form>
<a href="/user/list">返回列表</a>
</body>
</html>
```
userEdit.jsp
```jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>编辑用户</title>
</head>
<body>
<form action="/user/update" method="post">
<table>
<tr>
<td>ID:</td>
<td><input type="text" name="id" value="${user.id}" readonly></td>
</tr>
<tr>
<td>姓名:</td>
<td><input type="text" name="name" value="${user.name}"></td>
</tr>
<tr>
<td>电话:</td>
<td><input type="text" name="phone" value="${user.phone}"></td>
</tr>
<tr>
<td>出生日期:</td>
<td><input type="text" name="birthdate" value="${user.birthdate}"></td>
</tr>
</table>
<input type="submit" value="更新">
</form>
<a href="/user/${user.id}">返回详情</a>
</body>
</html>
```
以上代码仅供参考,实际开发中需要根据具体情况进行修改。
阅读全文