1.建立spring boot程序,导入mybatisplus依赖
<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.2</version> </dependency>
2.创建实体类,类中简单的包含以下几个字段
@Data public class User { private Integer id; private String username; private String password; private Integer age; }
3.创建返回结果类R,用于在之后的功能中返回相同的数据类型
@Data public class R { private Object data; private boolean flag; public R(boolean flag){ this.flag = flag; } public R(boolean flag, Object data){ this.flag = flag; this.data = data; } }
4.创建mapper层,service层和controller层,controller层中我们要创建登录的controller类UserController
@RestController @RequestMapping("/users") public class UserController { @PostMapping("/login") public R login(@RequestBody User user){ } }
5.我们这里使用mybatisplus中的LambdaQueryWrapper来查询我们需要的字段,登录时,我们需要从前端获取用户名(username)的json数据,从数据库中查询出用户名
//查询数据库
LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(User::getUsername,user.getUsername());
User user1 = userService.getOne(queryWrapper);
6.获取密码,进行逻辑判断,查询user1的用户名不存在或者获取的密码与该用户名对应的密码不等,返回登录失败,否则返回登录成功
String password = user.getPassword(); //没有查询到 if (user1 == null) { return new R(false); } //密码不一致 if (!user1.getPassword().equals(password)){ return new R(false); } //登录成功 return new R(true,user1);
标签:登录,user1,用户,private,flag,简单,new,public From: https://www.cnblogs.com/yxtuxy/p/17093837.html