好家伙,
代码已开源(Gitee)
后端这边update方法改了一下
controller类:
package com.wulaoda.loginhouduan.controller;
import com.wulaoda.loginhouduan.entity.SysUserEntity;
import com.wulaoda.loginhouduan.req.SysUserLoginReq;
import com.wulaoda.loginhouduan.req.SysUserSaveReq;
import com.wulaoda.loginhouduan.req.SysUserUpdateReq;
import com.wulaoda.loginhouduan.resp.CommonResp;
import com.wulaoda.loginhouduan.resp.SysUserLoginResp;
import com.wulaoda.loginhouduan.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
@RestController
//绑定数据库表名
@RequestMapping("/sys-user")
public class SysUserController {
@Autowired
private SysUserService sysUserService;
@PostMapping("register")
// zxcv1234
//@RequestBody主要用来接收前端传递给后端的json字符串中的数据的(请求体中的数据的);
public CommonResp register(@RequestBody SysUserSaveReq req){
//对密码进行md5加密
req.setPassword(DigestUtils.md5DigestAsHex(req.getPassword().getBytes()));
//resp为返回数据
CommonResp resp = new CommonResp<>();
//真正的注册
SysUserEntity saveResp = sysUserService.register(req);
if(saveResp ==null){
resp.setSuccess(false);
}
return resp;
}
@PostMapping("login")
public CommonResp login(@RequestBody SysUserLoginReq req){
// zxcv1234
req.setPassword(DigestUtils.md5DigestAsHex(req.getPassword().getBytes()));
CommonResp resp = new CommonResp<>();
SysUserLoginResp loginResp = sysUserService.login(req);
if (loginResp == null) {
resp.setSuccess(false);
}
resp.setContent(loginResp);
return resp;
}
@PostMapping("update")
public CommonResp update(@RequestBody SysUserUpdateReq req){
CommonResp resp = new CommonResp<>();
sysUserService.update(req);
return resp;
}
@GetMapping("getallscore")
public CommonResp getallscore(){
CommonResp resp = new CommonResp<>();
sysUserService.getallscore();
return resp;
}
}
service类
package com.wulaoda.loginhouduan.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wulaoda.loginhouduan.entity.SysUserEntity;
import com.wulaoda.loginhouduan.mapper.SysUserMapper;
import com.wulaoda.loginhouduan.req.SysUserLoginReq;
import com.wulaoda.loginhouduan.req.SysUserSaveReq;
import com.wulaoda.loginhouduan.req.SysUserUpdateReq;
import com.wulaoda.loginhouduan.resp.SysUserLoginResp;
import com.wulaoda.loginhouduan.resp.SysUserUpdateResp;
import com.wulaoda.loginhouduan.service.SysUserService;
import com.wulaoda.loginhouduan.utils.CopyUtil;
import com.wulaoda.loginhouduan.utils.SnowFlake;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import javax.annotation.Resource;
import java.util.List;
@Service
//implements表示接口的实现,必须实现接口中的所有方法
//此处继承SysUserService的方法并改写
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUserEntity> implements SysUserService {
@Resource
@Autowired
private SysUserMapper sysUserMapper;
@Autowired
private SnowFlake snowFlake;
//注册
@Override
public SysUserEntity register(SysUserSaveReq req) {
//
SysUserEntity user = CopyUtil.copy(req, SysUserEntity.class);
//ObjectUtils.isEmpty判断对象是否为空
if(ObjectUtils.isEmpty(req.getId())){
//查看用户名是否已注册
SysUserEntity userDb = selectByLoginName(req.getLoginName());
//用户名未被注册
if(ObjectUtils.isEmpty(userDb)){
//用算法给帮它设置一个新id
user.setId(snowFlake.nextId());
//这步是真正的往数据库里插数据了
sysUserMapper.insert(user);
//哈哈,没东西返回了
}else{
//用户名已被注册,返回null
return null;
}
}
//无用返回
return user;
}
//登陆
@Override
public SysUserLoginResp login(SysUserLoginReq req) {
SysUserEntity userDb = selectByLoginName(req.getLoginName());
QueryWrapper<SysUserEntity> wrapper = new QueryWrapper<>();
if(ObjectUtils.isEmpty(userDb)){
//用户不存在
return null;
}else {
String s1 = userDb.getPassword();
String s2 = req.getPassword();
//进行字符串校验
if(s1.equals(s2)){
//密码验证成功
int a = userDb.getLife();
SysUserLoginResp userLoginResp = CopyUtil.copy(userDb, SysUserLoginResp.class);
return userLoginResp;
}else {
//密码校验失败
return null;
}
}
}
//数据更新
@Override
public SysUserUpdateResp update(SysUserUpdateReq req){
// SysUserEntity userDb = selectByLoginName(req.getLoginName());
// QueryWrapper<SysUserEntity> wrapper = new QueryWrapper<>();
// LambdaUpdateWrapper<SysUserEntity> wrapper1 = new LambdaUpdateWrapper<>();
// wrapper1.set(SysUserEntity::getLife, req.getLife());
// sysUserMapper.update(userDb,wrapper1);
//重写
//网上的例子
// LambdaUpdateWrapper<User> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
// lambdaUpdateWrapper.eq(User::getName, "rhb").set(User::getAge, 18);
// Integer rows = userMapper.update(null, lambdaUpdateWrapper);
//以下版本只能修改单项数据
// LambdaUpdateWrapper<SysUserEntity> wrapper1 = new LambdaUpdateWrapper<>();
// wrapper1.eq(SysUserEntity::getLoginName, req.getLoginName()).set(SysUserEntity::getLife, req.getLife());
// sysUserMapper.update(null,wrapper1);
LambdaUpdateWrapper<SysUserEntity> wrapper1 = new LambdaUpdateWrapper<>();
wrapper1.eq(SysUserEntity::getLoginName, req.getLoginName()).set(SysUserEntity::getLife, req.getLife());
wrapper1.eq(SysUserEntity::getLoginName, req.getLoginName()).set(SysUserEntity::getScore, req.getScore());
sysUserMapper.update(null,wrapper1);
return null;
}
//获取所有分数getallscore
// @Override
// public void getallscore(){
// // 查询多个字段,其它不需要查询的字段则为null
//// List<Object> objects = sysUserMapper.selectObjs(new QueryWrapper<SysUserEntity>()
//// .lambda().select(SysUserEntity::getLoginName, SysUserEntity::getScore));
// // 查询单个字段
//// List<String> strings = adminService.listObjs(new QueryWrapper<Admin>()
//// .lambda().select(Admin::getAdminMobile), Object::toString);
//// 查询多个字段,其它不需要查询的字段则为null
//// QueryWrapper<SysUserLoginResp> userQueryWrapper = Wrappers.query();
////
//// userQueryWrapper.select(SysUserLoginResp.class , e -> !e.getColumn().equals("loginName")).lt("score" , 50);
//// List<SysUserLoginResp> userList = sysUserMapper.selectList(userQueryWrapper);
//// userList.forEach(System.out::println);
////// return (SysUserLoginResp) userList;
//// return null;
//
// QueryWrapper<SysUserLoginResp> userQueryWrapper = new QueryWrapper<>();
// userQueryWrapper.select(SysUserLoginResp.class , e->!e.getColumn().equals("password"))
// .like("username" , "k")
// .between("age" , 25 , 35)
// .isNotNull("password");
// List<SysUserLoginResp> userList = sysUserMapper.selectList(userQueryWrapper);
// userList.forEach(System.out::println);
// }
//查询loginName是否被注册
public SysUserEntity selectByLoginName(String loginName){
//QueryWrapper<>()
QueryWrapper<SysUserEntity> wrapper = new QueryWrapper<>();
//wrapper.lambda().eq()单表查询,在"SysUserEntity::getLoginName"找一个指定的"loginName"
wrapper.lambda().eq(SysUserEntity::getLoginName,loginName);
//BaseMapper的selectList()方法,根据id批量查询
List<SysUserEntity> userEntityList = sysUserMapper.selectList(wrapper);
//CollectionUtils.isEmpty()判断集合是否为空
if(CollectionUtils.isEmpty(userEntityList)){
//若判断为空,则返回null,意思是没被注册过
return null;
}else {
//查到了,将这条数据返回
return userEntityList.get(0);
}
}
}
对象实体类:
package com.wulaoda.loginhouduan.resp;
public class SysUserUpdateResp {
private String LoginName;
private int life;
private int score;
public String getLoginName() {
return LoginName;
}
public void setLoginName(String loginName) {
LoginName = loginName;
}
public int getLife() {
return life;
}
public void setLife(int life) {
this.life = life;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
@Override
public String toString() {
return "SysUserUpdateReq{" +
"LoginName='" + LoginName + '\'' +
", life=" + life +
", score=" + score +
'}';
}
}