开始写寒假作业了
目前是只写了登录和注册的简单功能
数据库用的mysql,使用可视化数据库工具mysqlworkbench加以辅助;
后端使用springboot+mybatisPlus
前端使用vue3+axios+router4,日后应该会加上pinia和element-ui
(话说博客园代码怎么弄成文件夹那样的格式..)
后端目录如下:

因为只写了登录和注册,所以只写了User相关
entity和mapper就不介绍了
UserService
1 package com.backtest.service; 2 3 import com.backtest.entity.User; 4 import com.backtest.mapper.UserMapper; 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.stereotype.Service; 7 8 @Service 9 public class UserService { 10 @Autowired 11 private UserMapper userMapper; 12 13 public String getPasswordById(String id) { 14 User user = userMapper.selectById(id); 15 if (user != null) { 16 return user.getPassword(); 17 }else 18 return null; 19 } 20 21 public void addUser(String id, String password) { 22 User user = new User(); 23 user.setId(id); 24 user.setPassword(password); 25 userMapper.insert(user); 26 } 27 }
UserController
1 package com.backtest.controller; 2 3 import com.backtest.service.UserService; 4 import org.springframework.beans.factory.annotation.Autowired; 5 import org.springframework.web.bind.annotation.CrossOrigin; 6 import org.springframework.web.bind.annotation.PostMapping; 7 import org.springframework.web.bind.annotation.RequestParam; 8 import org.springframework.web.bind.annotation.RestController; 9 10 @RestController 11 @CrossOrigin 12 public class UserController { 13 @Autowired 14 UserService userService; 15 16 @PostMapping("/user/login") 17 public String login(@RequestParam String id,@RequestParam String password) { 18 String pw = userService.getPasswordById(id); 19 if(pw!=null&&pw.equals(password)){ 20 return "0"; 21 }else if(pw!=null&&!pw.equals(password)){ 22 return "2"; 23 }else if(pw == null) { 24 return "1"; 25 }else 26 return "-1"; 27 } 28 29 @PostMapping("/user/register") 30 public String register(@RequestParam String id,@RequestParam String password) { 31 String pw = userService.getPasswordById(id); 32 if(pw!=null){ 33 return "1"; 34 }else{ 35 userService.addUser(id,password); 36 return "0"; 37 } 38 } 39 }
接下来是前端
目录如下

App.vue,main.js和index.js就不过多展示了,展示一下关键点的login和register
login.vue
1 <template> 2 <div> 3 <table> 4 <tbody> 5 <tr> 6 <td>账号</td> 7 <td><input type="text" v-model="id"></td> 8 </tr> 9 <tr> 10 <td>密码</td> 11 <td><input type="password" v-model="password"></td> 12 </tr> 13 </tbody> 14 </table> 15 <button @click="login">登录</button><router-link to="/register">注册</router-link> 16 </div> 17 </template> 18 <script> 19 import router from '../router'; 20 import axios from 'axios'; 21 22 export default { 23 data(){ 24 return { 25 id:'', 26 password:'', 27 } 28 }, 29 methods: { 30 login(){ 31 axios.post('http://localhost:8080/user/login',{},{ 32 params:{ 33 id:this.id, 34 password:this.password 35 } 36 }).then(res=>{ 37 switch(String(res.data)){ 38 case '0': 39 alert('登录成功'); 40 break; 41 case '1': 42 alert('用户不存在'); 43 break; 44 case '2': 45 alert('密码错误'); 46 break; 47 default: 48 alert('未知错误:'+res.data); 49 break; 50 } 51 }) 52 }, 53 } 54 } 55 </script> 56 <style scoped> 57 58 </style>
register.vue
1 <template> 2 <div> 3 <table> 4 <tbody> 5 <tr> 6 <td>账号</td> 7 <td><input type="text" v-model="id"></td> 8 </tr> 9 <tr> 10 <td>密码</td> 11 <td><input type="password" v-model="password"></td> 12 </tr> 13 <tr> 14 <td>确认密码</td> 15 <td><input type="password" v-model="configPassword"></td> 16 </tr> 17 </tbody> 18 </table> 19 <button @click="register">注册</button><router-link to="/login">返回登录</router-link> 20 </div> 21 </template> 22 <script> 23 import router from '../router'; 24 import axios from 'axios'; 25 export default { 26 data(){ 27 return { 28 id:'', 29 password:'', 30 configPassword:'', 31 } 32 }, 33 methods: { 34 register(){ 35 if(this.password!==this.configPassword){ 36 alert('两次密码输入不一致'); 37 return; 38 } 39 axios.post('http://localhost:8080/user/register',{},{ 40 params:{ 41 id:this.id, 42 password:this.password 43 } 44 }).then(res=>{ 45 switch(String(res.data)){ 46 case '0': 47 alert('注册成功'); 48 router.push('/login'); 49 break; 50 case '1': 51 alert('用户已存在'); 52 break; 53 } 54 }) 55 }, 56 } 57 } 58 </script>
样式美化等心情好了以后再弄吧...