怎么用jquery和springboot和mybatisplus实现登录注册
时间: 2024-05-08 11:21:51 浏览: 104
springboot整合mybatis实现登录注册功能.html
5星 · 资源好评率100%
首先,你需要创建一个Spring Boot项目,并且添加MyBatis Plus和jQuery依赖。
1. 创建数据库
首先,你需要创建一个数据库,并且创建用户表。
2. 创建实体类
创建一个User实体类,包含用户ID、用户名、密码等属性。
3. 创建Mapper
使用MyBatis Plus的代码生成器来生成UserMapper、UserMapper.xml和User实体类。
4. 创建Service
创建UserService类,并且实现用户注册和登录的方法。在注册时,需要对输入的用户名进行唯一性校验,并且对密码进行加密处理。在登录时,需要对输入的密码进行解密,并且校验用户名和密码是否匹配。
5. 创建Controller
创建UserController类,并且实现用户注册和登录的接口,使用jQuery发送请求并且接收响应。
6. 创建前端页面
创建登录和注册的HTML页面,并且使用jQuery发送请求并且接收响应。
代码示例:
User.java
```java
public class User {
private Integer id;
private String username;
private String password;
// getter and setter
}
```
UserMapper.java
```java
public interface UserMapper extends BaseMapper<User> {
}
```
UserService.java
```java
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public boolean register(User user) {
// check if username already exists
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.eq("username", user.getUsername());
User u = userMapper.selectOne(wrapper);
if (u != null) {
return false;
}
// encrypt password
user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));
int result = userMapper.insert(user);
return result > 0;
}
@Override
public User login(String username, String password) {
// decrypt password
password = DigestUtils.md5DigestAsHex(password.getBytes());
// check username and password
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.eq("username", username);
wrapper.eq("password", password);
User user = userMapper.selectOne(wrapper);
return user;
}
}
```
UserController.java
```java
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@PostMapping("/register")
public boolean register(@RequestBody User user) {
return userService.register(user);
}
@PostMapping("/login")
public User login(@RequestParam String username, @RequestParam String password) {
return userService.login(username, password);
}
}
```
login.html
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Login</title>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$(function () {
$("#submit").click(function () {
$.ajax({
url: "/user/login",
type: "post",
data: {
"username": $("#username").val(),
"password": $("#password").val()
},
success: function (data) {
if (data != null) {
alert("Login success!");
} else {
alert("Login failed!");
}
}
});
});
});
</script>
</head>
<body>
<h1>Login</h1>
<form>
<label>Username:</label>
<input type="text" id="username"><br>
<label>Password:</label>
<input type="password" id="password"><br>
<input type="button" id="submit" value="Submit">
</form>
</body>
</html>
```
register.html
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Register</title>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$(function () {
$("#submit").click(function () {
$.ajax({
url: "/user/register",
type: "post",
data: {
"username": $("#username").val(),
"password": $("#password").val()
},
success: function (data) {
if (data) {
alert("Register success!");
} else {
alert("Register failed!");
}
}
});
});
});
</script>
</head>
<body>
<h1>Register</h1>
<form>
<label>Username:</label>
<input type="text" id="username"><br>
<label>Password:</label>
<input type="password" id="password"><br>
<input type="button" id="submit" value="Submit">
</form>
</body>
</html>
```
以上就是用jQuery和Spring Boot和MyBatis Plus实现登录注册的步骤和代码示例。
阅读全文