diff --git a/src/main/java/com/snhanyue/course/controller/api/UsersController.java b/src/main/java/com/snhanyue/course/controller/api/UsersController.java
index 643ba5766ef3aefdcf87c471f7ed610d5568a088..57b9c11ed260482df7030a4c14f595f3ae17caba 100644
--- a/src/main/java/com/snhanyue/course/controller/api/UsersController.java
+++ b/src/main/java/com/snhanyue/course/controller/api/UsersController.java
@@ -4,6 +4,7 @@ import com.snhanyue.course.dto.UsersAddNewUserDTO;
import com.snhanyue.course.service.api.IUsersService;
import com.snhanyue.course.util.json.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@@ -15,12 +16,18 @@ public class UsersController {
@Autowired
private IUsersService usersService;
+ //注册用户
@PostMapping("/users/add-new")
public JsonResult addNew(UsersAddNewUserDTO usersAddNewUserDTO){
usersService.addNewUser(usersAddNewUserDTO);
return JsonResult.ok();
+ //http://localhost:8080/api/users/add-new?username=chenpeng
+ }
- //http://localhost:8080/users/add-new?username=chenpeng
-
+ //登录用户
+ @PostMapping("/users/login")
+ public JsonResult login(UsersAddNewUserDTO usersAddNewUserDTO){
+ usersService.loginUser(usersAddNewUserDTO);
+ return JsonResult.ok();
}
}
diff --git a/src/main/java/com/snhanyue/course/emum/State.java b/src/main/java/com/snhanyue/course/emum/State.java
index 6a84032f49e522fd6c7f6ab281a77ec9d64e98d3..d9d2377e9f8a6b420de4c4ba95925d6a3fa508ef 100644
--- a/src/main/java/com/snhanyue/course/emum/State.java
+++ b/src/main/java/com/snhanyue/course/emum/State.java
@@ -6,7 +6,9 @@ public enum State {
ERR_CATEGORY_NOT_FOUND(40101), // 客户端引起的--类别--数据不存在(查询参数值不正确)
ERR_INSERT(50000), // 服务端引起的--插入数据错误
ERR_UPDATE(50001), // 服务端引起的--更新数据错误
- ERR_REPEAT_OF_USER_NAME(50002);//用户名冲突
+ ERR_REPEAT_OF_USER_NAME(50002),//用户名冲突
+ ERR_USER_NAME_DOES_NOT_EXIST(50003),//用户名不存在
+ ERR_PASSWORD(50004);//密码错误
private Integer value;
diff --git a/src/main/java/com/snhanyue/course/mapper/database/UsersMapper.java b/src/main/java/com/snhanyue/course/mapper/database/UsersMapper.java
index 5997afa0a816c4947ff56b5a189f393ec748526b..7ffbec2b01cc5b576570a4f3d4f575600b5298c6 100644
--- a/src/main/java/com/snhanyue/course/mapper/database/UsersMapper.java
+++ b/src/main/java/com/snhanyue/course/mapper/database/UsersMapper.java
@@ -1,7 +1,7 @@
package com.snhanyue.course.mapper.database;
import com.snhanyue.course.entity.Users;
-import com.snhanyue.course.vo.UsersVo;
+import com.snhanyue.course.vo.UsersVO;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
@@ -24,7 +24,7 @@ public interface UsersMapper {
* @param username用户名
* @return 查询的数据
*/
- UsersVo getByName(String username);
+ UsersVO getByName(String username);
/**
*
diff --git a/src/main/java/com/snhanyue/course/service/api/IUsersService.java b/src/main/java/com/snhanyue/course/service/api/IUsersService.java
index 46942120400eb3d28f8625edd99597b3f5e861a2..29ba0cd5f96567997d927b9d1fa410e18c8a2388 100644
--- a/src/main/java/com/snhanyue/course/service/api/IUsersService.java
+++ b/src/main/java/com/snhanyue/course/service/api/IUsersService.java
@@ -4,4 +4,6 @@ import com.snhanyue.course.dto.UsersAddNewUserDTO;
public interface IUsersService {
void addNewUser(UsersAddNewUserDTO usersAddNewUserDTO);
+
+ void loginUser(UsersAddNewUserDTO usersAddNewUserDTO);
}
diff --git a/src/main/java/com/snhanyue/course/service/api/impl/UsersServiceImpl.java b/src/main/java/com/snhanyue/course/service/api/impl/UsersServiceImpl.java
index 89e3c746436796c3c709ca94743781e8248d1b48..ba44b1a4a38709ac6626372630c2ae9a114aca14 100644
--- a/src/main/java/com/snhanyue/course/service/api/impl/UsersServiceImpl.java
+++ b/src/main/java/com/snhanyue/course/service/api/impl/UsersServiceImpl.java
@@ -6,7 +6,8 @@ import com.snhanyue.course.entity.Users;
import com.snhanyue.course.exception.ServiceException;
import com.snhanyue.course.mapper.database.UsersMapper;
import com.snhanyue.course.service.api.IUsersService;
-import com.snhanyue.course.vo.UsersVo;
+import com.snhanyue.course.util.json.JsonResult;
+import com.snhanyue.course.vo.UsersVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@@ -19,28 +20,62 @@ public class UsersServiceImpl implements IUsersService {
@Override
public void addNewUser(UsersAddNewUserDTO usersAddNewUserDTO) {
+
//从参数中获取用户名
String name = usersAddNewUserDTO.getUsername();
//调用查询方法查询数据库
- UsersVo byName = usersMapper.getByName(name);
+ UsersVO usersVO = usersMapper.getByName(name);
+// String sqlName = usersVO.getUsername();
//判断数据库是否有该用户名
- if (byName != null) {
+ if ( usersVO.equals(null) ) {
System.out.println("用户添加失败用户名" + name + "已存在");
-
+ throw new ServiceException(State.ERR_REPEAT_OF_USER_NAME,"用户名已存在");
}
Users users = new Users();
//将DTO类中的数据全部复制到entity中的users里面
BeanUtils.copyProperties(usersAddNewUserDTO, users);
//补全状态
users.setIs_enable(1);
+
+ //密码需要加密,功能后补
+
+
+
//调用接口插入数据获取返回值
int row = usersMapper.insert(users);
//根据返回值判断是否插入成功
if (row != 1) {
- System.out.println("数据库信息插入失败,请稍后再试");
+// System.out.println("数据库信息插入失败,请稍后再试");
+ throw new ServiceException(State.ERR_INSERT,"数据库信息插入失败");
+ }
+
+ }
+
+ @Override
+ public void loginUser(UsersAddNewUserDTO usersAddNewUserDTO) {
+ //从参数中获取用户名
+ String name = usersAddNewUserDTO.getUsername();
+ //调用查询方法查询数据库
+ UsersVO usersVO = usersMapper.getByName(name);
+ String sqlName=usersVO.getUsername();
+ //判断数据库是否有该用户名
+ if (sqlName == null) {
+ System.out.println("用户登录失败用户名" + name + "不存在");
+ throw new ServiceException(State.ERR_USER_NAME_DOES_NOT_EXIST,"用户名不存在登陆失败");
}
+ String password = usersAddNewUserDTO.getPassword();
+ String sqlPassword=usersVO.getPassword();
+ if (password.equals(sqlPassword) && name.equals(sqlName)) {
+ System.out.println("登陆成功");
+ return;
+ }else {
+ System.out.println(password+"password");
+ System.out.println(sqlPassword+"passwords");
+ throw new ServiceException(State.ERR_PASSWORD,"密码错误");
+ }
}
+
}
diff --git a/src/main/java/com/snhanyue/course/vo/UsersVo.java b/src/main/java/com/snhanyue/course/vo/UsersVO.java
similarity index 93%
rename from src/main/java/com/snhanyue/course/vo/UsersVo.java
rename to src/main/java/com/snhanyue/course/vo/UsersVO.java
index 177b51a9c0d369473e1f0127acf3b46c34d7e825..021c3730a0f61aa88b9c71cf9d0423e17c89ffad 100644
--- a/src/main/java/com/snhanyue/course/vo/UsersVo.java
+++ b/src/main/java/com/snhanyue/course/vo/UsersVO.java
@@ -3,7 +3,7 @@ package com.snhanyue.course.vo;
import lombok.Data;
@Data
-public class UsersVo {
+public class UsersVO {
private String username;
private String password;
private int gender;
diff --git a/src/main/resources/mybatis/mapper/UsersMapper.xml b/src/main/resources/mybatis/mapper/UsersMapper.xml
index 63f4a6d914bb5d04867a82cf9d8e2111d9369523..25ed957fddcb3ed57154b92a44fd28f58bbf0bf6 100644
--- a/src/main/resources/mybatis/mapper/UsersMapper.xml
+++ b/src/main/resources/mybatis/mapper/UsersMapper.xml
@@ -28,7 +28,7 @@
-
+
diff --git a/src/test/java/com/snhanyue/mapper/UserMapperTests.java b/src/test/java/com/snhanyue/mapper/UserMapperTests.java
index 3519eb0c7fd9d4a43db95c5ab32c5a69052d430e..8b1e5201337f050ca963cd7617b9086186018dbd 100644
--- a/src/test/java/com/snhanyue/mapper/UserMapperTests.java
+++ b/src/test/java/com/snhanyue/mapper/UserMapperTests.java
@@ -3,7 +3,7 @@ package com.snhanyue.mapper;
import com.snhanyue.course.entity.Users;
import com.snhanyue.course.mapper.database.UsersMapper;
-import com.snhanyue.course.vo.UsersVo;
+import com.snhanyue.course.vo.UsersVO;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@@ -40,7 +40,7 @@ public class UserMapperTests {
@Test
public void testGetByName(){
String name="陈鹏";
- UsersVo usersVo = mapper.getByName(name);
+ UsersVO usersVo = mapper.getByName(name);
System.out.println(usersVo.toString());
}