1.新建首页index.html
在 resources 目录下 static 中 新建
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>axios异步请求测试</h1>
<div>
<a href="/login.html">登录页面</a>
</div>
2.新建 login.html
在 resources 目录下 static 中 新建
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div>
<h1>登录页面</h1>
<input type="text" v-model="user.username" placeholder="用户名">
<input type="text" v-model="user.password" placeholder="密码">
<input type="button" value="登录" @click="login()">
</div>
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
<script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
<script>
let v = new Vue({
el:"body>div",
data:{
user:{
username:"",
password:""
}
},
methods:{
login(){
axios.post("/login",v.user).then(function (response) {
if(response.data==1){
alert("登陆成功!");
Location.href="/";//显示首页
}else if(response.data==2){
alert("用户名不存在!");
}else{
alert("密码错误!");
}
})
}
}
})
</script>
</body>
</html>
3.新建User实体类
新建entity包下创建User类
也可使用lombok注解生成get,set,toString,这里懒得改了
package cn.detu.boot07.entity;
public class User {
private Integer id;
private String username;
private String password;
private String nick;
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", nick='" + nick + '\'' +
'}';
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getNick() {
return nick;
}
public void setNick(String nick) {
this.nick = nick;
}
}
4.新建UserController
controller 包下新建 控制层
package cn.detu.boot07.controller;
import cn.detu.boot07.entity.User;
import cn.detu.boot07.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@Autowired(required = false)
UserMapper mapper;
@RequestMapping("/login")
public int login(@RequestBody User user){
User u= mapper.selectByUsername(user.getUsername());
if(u != null){
if(user.getPassword().equals(u.getPassword())){
return 1;//登陆成功
}
return 3;//密码错误
}
return 2;//用户名不存在
}
5..新建UserMapper
mapper包下新建
package cn.detu.boot07.mapper;
import cn.detu.boot07.entity.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
@Mapper
public interface UserMapper {
@Select("select password from user where username = #{username}")
User selectByUsername(String username);
}