springboot+mybatis+html实现登录注册前后端交互
时间: 2023-06-27 12:07:53 浏览: 315
要实现登录注册的前后端交互,可以按照以下步骤:
1. 创建一个Spring Boot项目。
2. 添加依赖:spring-boot-starter-web、spring-boot-starter-thymeleaf、spring-boot-starter-jdbc、mysql-connector-java、mybatis-spring-boot-starter。
3. 配置数据库连接,可以在application.properties文件中添加以下内容:
```
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
```
4. 创建数据库表,例如创建user表,包含id、username、password三个字段。
5. 创建User实体类,对应user表中的字段。
6. 创建Mapper接口和Mapper.xml文件,用于数据操作。
7. 创建Controller类,处理前端请求。
8. 创建html页面,用于用户输入信息和显示结果。
下面是一个简单的示例:
1. 创建Spring Boot项目。
2. 添加依赖:spring-boot-starter-web、spring-boot-starter-thymeleaf、spring-boot-starter-jdbc、mysql-connector-java、mybatis-spring-boot-starter。
```
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>
```
3. 配置数据库连接,可以在application.properties文件中添加以下内容:
```
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
```
4. 创建数据库表,例如创建user表,包含id、username、password三个字段。
```
CREATE TABLE IF NOT EXISTS `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(50) DEFAULT NULL,
`password` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
```
5. 创建User实体类,对应user表中的字段。
```
public class User {
private Integer id;
private String username;
private String password;
// getter和setter方法省略
}
```
6. 创建Mapper接口和Mapper.xml文件,用于数据操作。
```
@Mapper
public interface UserMapper {
@Select("SELECT * FROM user WHERE username=#{username} AND password=#{password}")
User getUser(@Param("username") String username, @Param("password") String password);
@Insert("INSERT INTO user(username, password) VALUES(#{username}, #{password})")
int addUser(User user);
}
```
7. 创建Controller类,处理前端请求。
```
@Controller
public class UserController {
@Autowired
private UserMapper userMapper;
@GetMapping("/")
public String index() {
return "index";
}
@PostMapping("/login")
public String login(@RequestParam("username") String username, @RequestParam("password") String password, Model model) {
User user = userMapper.getUser(username, password);
if (user == null) {
model.addAttribute("error", "用户名或密码错误");
return "index";
} else {
model.addAttribute("username", user.getUsername());
return "success";
}
}
@PostMapping("/register")
public String register(@RequestParam("username") String username, @RequestParam("password") String password, Model model) {
User user = new User();
user.setUsername(username);
user.setPassword(password);
int result = userMapper.addUser(user);
if (result == 1) {
model.addAttribute("username", username);
return "success";
} else {
model.addAttribute("error", "注册失败");
return "index";
}
}
}
```
8. 创建html页面,用于用户输入信息和显示结果。
index.html:
```
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Login/Register</title>
</head>
<body>
<h1>Login/Register</h1>
<form action="/login" method="post">
<label>Username: </label><input type="text" name="username"/><br/>
<label>Password: </label><input type="password" name="password"/><br/>
<input type="submit" value="Login"/>
<span th:text="${error}"></span>
</form>
<hr/>
<form action="/register" method="post">
<label>Username: </label><input type="text" name="username"/><br/>
<label>Password: </label><input type="password" name="password"/><br/>
<input type="submit" value="Register"/>
<span th:text="${error}"></span>
</form>
</body>
</html>
```
success.html:
```
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Success</title>
</head>
<body>
<h1>Welcome, <span th:text="${username}"></span>!</h1>
</body>
</html>
```
以上就是一个简单的登录注册前后端交互的示例。
阅读全文