帮我用Java写一个ssm项目实现登录和注册功能
时间: 2024-02-15 18:05:19 浏览: 216
好的,我可以给您提供一个基本的SSM框架的登录和注册功能的示例代码,您可以参考一下:
首先,您需要创建一个Maven项目,然后在pom.xml文件中添加相关依赖:
```xml
<!-- Spring依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<!-- MyBatis依赖 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
<!-- 数据库驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.20</version>
</dependency>
<!-- JSTL依赖 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- Servlet API依赖 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
```
然后,您需要在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/dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
```
接下来,您需要创建一个dispatcher-servlet.xml文件,用于配置Spring MVC的相关内容。
```xml
<!-- 扫描包 -->
<context:component-scan base-package="com.example.controller" />
<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- 数据源配置 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/test" />
<property name="username" value="root" />
<property name="password" value="password" />
</bean>
<!-- MyBatis配置 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis-config.xml" />
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.example.dao" />
</bean>
```
接下来,您需要创建一个mybatis-config.xml文件,用于配置MyBatis的相关内容。
```xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<package name="com.example.entity" />
</typeAliases>
<mappers>
<mapper class="com.example.dao.UserDao" />
</mappers>
</configuration>
```
然后,您需要创建一个User实体类,用于存储用户信息。
```java
public class User {
private Integer id;
private String username;
private String password;
// getter和setter方法省略
}
```
接下来,您需要创建一个UserDao接口,用于定义操作User表的方法。
```java
public interface UserDao {
User selectByUsername(String username);
void insert(User user);
}
```
然后,您需要创建一个UserDaoImpl类,用于实现UserDao接口。
```java
@Repository
public class UserDaoImpl implements UserDao {
@Autowired
private SqlSession sqlSession;
@Override
public User selectByUsername(String username) {
return sqlSession.selectOne("com.example.dao.UserDao.selectByUsername", username);
}
@Override
public void insert(User user) {
sqlSession.insert("com.example.dao.UserDao.insert", user);
}
}
```
接下来,您需要创建一个UserController类,用于处理用户请求。
```java
@Controller
public class UserController {
@Autowired
private UserDao userDao;
@RequestMapping("/login")
public ModelAndView login(String username, String password) {
ModelAndView modelAndView = new ModelAndView();
User user = userDao.selectByUsername(username);
if (user == null || !user.getPassword().equals(password)) {
modelAndView.setViewName("login");
modelAndView.addObject("message", "用户名或密码错误");
} else {
modelAndView.setViewName("index");
}
return modelAndView;
}
@RequestMapping("/register")
public ModelAndView register(String username, String password) {
ModelAndView modelAndView = new ModelAndView();
User user = userDao.selectByUsername(username);
if (user != null) {
modelAndView.setViewName("register");
modelAndView.addObject("message", "用户名已存在");
} else {
user = new User();
user.setUsername(username);
user.setPassword(password);
userDao.insert(user);
modelAndView.setViewName("index");
}
return modelAndView;
}
}
```
最后,您需要创建login.jsp和register.jsp文件,用于显示登录和注册页面。
login.jsp:
```html
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录</title>
</head>
<body>
<h1>登录</h1>
<form action="login" method="post">
<p>
<label>用户名:</label>
<input type="text" name="username" />
</p>
<p>
<label>密码:</label>
<input type="password" name="password" />
</p>
<p>
<input type="submit" value="登录" />
</p>
<p style="color: red">${message }</p>
</form>
</body>
</html>
```
register.jsp:
```html
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>注册</title>
</head>
<body>
<h1>注册</h1>
<form action="register" method="post">
<p>
<label>用户名:</label>
<input type="text" name="username" />
</p>
<p>
<label>密码:</label>
<input type="password" name="password" />
</p>
<p>
<input type="submit" value="注册" />
</p>
<p style="color: red">${message }</p>
</form>
</body>
</html>
```
至此,您就完成了一个基本的SSM框架的登录和注册功能的实现。
阅读全文