运用SSM框架制作一个完整的管理系统。网站至少包含普通用户和管理员2个身份,管理员至少可以对普通用户进行管理。 网站至少要包含一些基本功能,如登录、注册、修改信息,信息管理等。写出具体代码
时间: 2024-02-06 22:08:59 浏览: 72
由于题目比较复杂,无法在一篇回答中给出完整的代码,因此我将给出一个简单的示例代码来演示如何使用SSM框架制作一个管理系统。
首先,我们需要创建一个Maven工程,并添加以下依赖:
```xml
<dependencies>
<!--Spring-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.3.9</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.9</version>
</dependency>
<!--MyBatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.5</version>
</dependency>
<!--MySQL-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.26</version>
</dependency>
<!--JSP-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<!--JUnit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>
```
接下来,我们需要创建数据库表和实体类。
假设我们需要创建一个用户信息表,包含以下字段:
- id:用户ID
- username:用户名
- password:密码
- email:邮箱
- phone:手机号码
我们可以使用以下SQL语句创建该表:
```sql
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(50) NOT NULL,
`password` varchar(50) NOT NULL,
`email` varchar(50) DEFAULT NULL,
`phone` varchar(20) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4;
```
接下来,我们需要创建一个User实体类,并使用MyBatis的注解将其与数据库表进行映射:
```java
public class User {
private int id;
private String username;
private String password;
private String email;
private String phone;
// getter/setter方法
}
```
```java
@Mapper
public interface UserMapper {
@Insert("INSERT INTO user(username, password, email, phone) VALUES(#{username}, #{password}, #{email}, #{phone})")
void addUser(User user);
@Select("SELECT * FROM user WHERE id=#{id}")
User getUserById(int id);
@Select("SELECT * FROM user WHERE username=#{username}")
User getUserByUsername(String username);
@Update("UPDATE user SET username=#{username}, password=#{password}, email=#{email}, phone=#{phone} WHERE id=#{id}")
void updateUser(User user);
@Delete("DELETE FROM user WHERE id=#{id}")
void deleteUser(int id);
@Select("SELECT * FROM user")
List<User> getAllUsers();
}
```
接下来,我们需要创建一个控制器来处理用户的请求:
```java
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserMapper userMapper;
@GetMapping("/list")
public String list(Model model) {
List<User> userList = userMapper.getAllUsers();
model.addAttribute("userList", userList);
return "user/list";
}
@GetMapping("/add")
public String add(Model model) {
model.addAttribute("user", new User());
return "user/add";
}
@PostMapping("/add")
public String add(@ModelAttribute User user) {
userMapper.addUser(user);
return "redirect:/user/list";
}
@GetMapping("/edit/{id}")
public String edit(@PathVariable int id, Model model) {
User user = userMapper.getUserById(id);
model.addAttribute("user", user);
return "user/edit";
}
@PostMapping("/edit/{id}")
public String edit(@ModelAttribute User user) {
userMapper.updateUser(user);
return "redirect:/user/list";
}
@GetMapping("/delete/{id}")
public String delete(@PathVariable int id) {
userMapper.deleteUser(id);
return "redirect:/user/list";
}
}
```
最后,我们需要创建一些JSP页面来呈现用户界面:
- user/list.jsp
```jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>User List</title>
</head>
<body>
<h1>User List</h1>
<table>
<tr>
<th>ID</th>
<th>Username</th>
<th>Email</th>
<th>Phone</th>
<th>Operation</th>
</tr>
<c:forEach items="${userList}" var="user">
<tr>
<td>${user.id}</td>
<td>${user.username}</td>
<td>${user.email}</td>
<td>${user.phone}</td>
<td>
<a href="<c:url value='/user/edit/${user.id}'/>">Edit</a>
<a href="<c:url value='/user/delete/${user.id}'/>">Delete</a>
</td>
</tr>
</c:forEach>
</table>
<p><a href="<c:url value='/user/add'/>">Add User</a></p>
</body>
</html>
```
- user/add.jsp
```jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Add User</title>
</head>
<body>
<h1>Add User</h1>
<form action="<c:url value='/user/add'/>" method="post">
<table>
<tr>
<td>Username:</td>
<td><input type="text" name="username"/></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="password"/></td>
</tr>
<tr>
<td>Email:</td>
<td><input type="text" name="email"/></td>
</tr>
<tr>
<td>Phone:</td>
<td><input type="text" name="phone"/></td>
</tr>
</table>
<input type="submit" value="Submit"/>
</form>
</body>
</html>
```
- user/edit.jsp
```jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Edit User</title>
</head>
<body>
<h1>Edit User</h1>
<form action="<c:url value='/user/edit/${user.id}'/>" method="post">
<table>
<tr>
<td>ID:</td>
<td>${user.id}</td>
</tr>
<tr>
<td>Username:</td>
<td><input type="text" name="username" value="${user.username}"/></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="password" value="${user.password}"/></td>
</tr>
<tr>
<td>Email:</td>
<td><input type="text" name="email" value="${user.email}"/></td>
</tr>
<tr>
<td>Phone:</td>
<td><input type="text" name="phone" value="${user.phone}"/></td>
</tr>
</table>
<input type="submit" value="Submit"/>
</form>
</body>
</html>
```
至此,我们就完成了一个基本的SSM框架的管理系统。当然,这只是一个简单示例,实际项目中还需要考虑更多的功能和安全性。
阅读全文