SpringBoot+Mybatis+微信小程序实现前后端分离的登录 注册 选课小程序
前端页面:
登录界面:后端判断账号密码是否匹配
选课界面:
我的课表界面:实现退课功能和显示课表功能
个人页面显示:实现setStorageSync缓存 登录人的信息,并且获取登陆人信息显示到个人信息页面
Springboot+Mybatis+Mysql
UserMapper
package com.example.demowx.mapper;
import com.example.demowx.entity.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.RestController;
@Mapper
@Repository
@RestController
public interface UserMapper {
@Insert("INSERT into user(userId,username,password,usersex,school) VALUES(#{userId},#{username},#{password},#{usersex},#{school}) ")
int saveUser(@Param("userId") String userId, @Param("username") String username, @Param("password") String password, @Param("usersex") String usersex, @Param("school") String school);
@Select("select userId,username,password,usersex,school from user where userId=#{userId}")
User selectUser(String userId);
}
User实体类
package com.example.demowx.entity;
import lombok.Data;
@Data
public class User {
private String userId;
private String username;
private String password;
private String usersex;
private String school;
}
UserController控制层
package com.example.demowx.controller;
import com.example.demowx.entity.User;
import org.apache.ibatis.jdbc.Null;
import org.springframework.util.StringUtils;
import com.example.demowx.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.google.gson.Gson;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.io.Writer;
import java.util.List;
@RequestMapping("user")
@RestController
public class UserController {
@Autowired
private UserMapper userMapper;
@ResponseBody
@GetMapping("login")
public Object login(String userId,String password, HttpServletResponse response){
User user=userMapper.selectUser(userId);
if (user==null){ //判断对象为不为空, 判断对象的属性为不为空会报空指针错误
return 2;
}
if(password.equals(user.getPassword())){
return user;
}
return 0;
}
@ResponseBody
@RequestMapping("register")
public int register(String userId,String username,String password,String usersex,String school){
User user=userMapper.selectUser(userId);
if(user!=null){
return 1;//用户已经存在
}
int resultCount=userMapper.saveUser(userId,username,password,usersex,school);
if(resultCount==0){
return 0;//注册失败
}
return 2;//注册成功
}
@ResponseBody
@GetMapping("getOne")
public User login(String userId, HttpServletResponse response) {
User user=userMapper.selectUser(userId);
return user;
}
}