feat-登录功能demo
This commit is contained in:
parent
7fc20deda1
commit
9a9d2e7018
9
pom.xml
9
pom.xml
@ -32,6 +32,15 @@
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mybatis.spring.boot</groupId>
|
||||
<artifactId>mybatis-spring-boot-starter</artifactId>
|
||||
<version>3.0.3</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.mysql</groupId>
|
||||
<artifactId>mysql-connector-j</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
43
src/main/java/cn/dcsy/stsy/controllers/BasicController.java
Normal file
43
src/main/java/cn/dcsy/stsy/controllers/BasicController.java
Normal file
@ -0,0 +1,43 @@
|
||||
package cn.dcsy.stsy.controllers;
|
||||
|
||||
import cn.dcsy.stsy.service.UserService;
|
||||
import cn.dcsy.stsy.utils.BaseResponse;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
/**
|
||||
* 基本控制器
|
||||
*
|
||||
* @author DC_DC
|
||||
* Date: 2024/4/8/08:50
|
||||
*/
|
||||
|
||||
@Slf4j
|
||||
@RestController
|
||||
@CrossOrigin("*")
|
||||
@RequestMapping("/basic")
|
||||
@RequiredArgsConstructor
|
||||
public class BasicController {
|
||||
private final UserService userService;
|
||||
/**
|
||||
* 网站主页
|
||||
*/
|
||||
// @GetMapping("/**")
|
||||
public ResponseEntity<BaseResponse> index() {
|
||||
log.info("访问主页");
|
||||
BaseResponse response = new BaseResponse("欢迎", 200, "Success", "这是故事管理系统主页");
|
||||
return ResponseEntity.ok(response);
|
||||
}
|
||||
|
||||
/*
|
||||
* 用户登录
|
||||
* */
|
||||
@PostMapping("/login")
|
||||
public ResponseEntity<BaseResponse> login(@RequestParam String username, @RequestParam String password) {
|
||||
log.info("尝试登录 用户名: {}", username);
|
||||
return userService.login(username, password);
|
||||
}
|
||||
|
||||
}
|
@ -1,18 +0,0 @@
|
||||
package cn.dcsy.stsy.controllers;
|
||||
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* @author DC_DC
|
||||
* Date: 2024/3/21/23:54
|
||||
*/
|
||||
@RestController
|
||||
public class Demo {
|
||||
|
||||
@GetMapping("/index")
|
||||
public String domain(){
|
||||
System.out.println("demo");
|
||||
return "DC_DC";
|
||||
}
|
||||
}
|
24
src/main/java/cn/dcsy/stsy/dao/UserDAO.java
Normal file
24
src/main/java/cn/dcsy/stsy/dao/UserDAO.java
Normal file
@ -0,0 +1,24 @@
|
||||
package cn.dcsy.stsy.dao;
|
||||
|
||||
import cn.dcsy.stsy.mappers.UserMapper;
|
||||
import cn.dcsy.stsy.models.doData.UserDemoDO;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
/**
|
||||
* @author DC_DC
|
||||
* Date: 2024/4/16/22:02
|
||||
*/
|
||||
@Slf4j
|
||||
@Repository
|
||||
@RequiredArgsConstructor
|
||||
public class UserDAO {
|
||||
private final UserMapper userMapper;
|
||||
|
||||
public UserDemoDO getPasswordByUserName(String userName) {
|
||||
log.info("[DAO] 执行 getPasswordByUserName 方法");
|
||||
log.info("\t> Mysql 读取");
|
||||
return userMapper.getPasswordByUsername(userName);
|
||||
}
|
||||
}
|
15
src/main/java/cn/dcsy/stsy/mappers/UserMapper.java
Normal file
15
src/main/java/cn/dcsy/stsy/mappers/UserMapper.java
Normal file
@ -0,0 +1,15 @@
|
||||
package cn.dcsy.stsy.mappers;
|
||||
|
||||
import cn.dcsy.stsy.models.doData.UserDemoDO;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Select;
|
||||
|
||||
/**
|
||||
* @author DC_DC
|
||||
* Date: 2024/4/16/22:03
|
||||
*/
|
||||
@Mapper
|
||||
public interface UserMapper {
|
||||
@Select("SELECT * FROM stories_system.user_demo WHERE username = #{username}")
|
||||
UserDemoDO getPasswordByUsername(String username);
|
||||
}
|
14
src/main/java/cn/dcsy/stsy/models/doData/UserDemoDO.java
Normal file
14
src/main/java/cn/dcsy/stsy/models/doData/UserDemoDO.java
Normal file
@ -0,0 +1,14 @@
|
||||
package cn.dcsy.stsy.models.doData;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* @author DC_DC
|
||||
* Date: 2024/4/16/22:00
|
||||
*/
|
||||
@Data
|
||||
public class UserDemoDO {
|
||||
private Integer id;
|
||||
private String username;
|
||||
private String password;
|
||||
}
|
12
src/main/java/cn/dcsy/stsy/service/UserService.java
Normal file
12
src/main/java/cn/dcsy/stsy/service/UserService.java
Normal file
@ -0,0 +1,12 @@
|
||||
package cn.dcsy.stsy.service;
|
||||
|
||||
import cn.dcsy.stsy.utils.BaseResponse;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
|
||||
/**
|
||||
* @author DC_DC
|
||||
* Date: 2024/4/16/21:51
|
||||
*/
|
||||
public interface UserService {
|
||||
ResponseEntity<BaseResponse> login(String username, String password);
|
||||
}
|
39
src/main/java/cn/dcsy/stsy/service/impl/UserServiceImpl.java
Normal file
39
src/main/java/cn/dcsy/stsy/service/impl/UserServiceImpl.java
Normal file
@ -0,0 +1,39 @@
|
||||
package cn.dcsy.stsy.service.impl;
|
||||
|
||||
import cn.dcsy.stsy.dao.UserDAO;
|
||||
import cn.dcsy.stsy.models.doData.UserDemoDO;
|
||||
import cn.dcsy.stsy.service.UserService;
|
||||
import cn.dcsy.stsy.utils.BaseResponse;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* @author DC_DC
|
||||
* Date: 2024/4/16/21:53
|
||||
*/
|
||||
@Service
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
public class UserServiceImpl implements UserService {
|
||||
private final UserDAO userDAO;
|
||||
|
||||
@Override
|
||||
public ResponseEntity<BaseResponse> login(String username, String password) {
|
||||
UserDemoDO userDemoDO = userDAO.getPasswordByUserName(username);
|
||||
log.info(String.valueOf(userDemoDO));
|
||||
if (userDemoDO != null){
|
||||
if (userDemoDO.getPassword().equals(password)){
|
||||
BaseResponse response = new BaseResponse("登录成功", 200, "Success", "用户已登录");
|
||||
return ResponseEntity.ok(response);
|
||||
} else {
|
||||
BaseResponse response = new BaseResponse("登录失败", 404, "Error", "密码错误");
|
||||
return ResponseEntity.status(404).body(response);
|
||||
}
|
||||
} else {
|
||||
BaseResponse response = new BaseResponse("登录失败", 404, "Error", "用户名不存在");
|
||||
return ResponseEntity.status(404).body(response);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
package cn.dcsy.stsy.start;
|
||||
|
||||
import org.mybatis.spring.annotation.MapperScan;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
@ -9,6 +10,7 @@ import org.springframework.context.annotation.ComponentScan;
|
||||
*/
|
||||
@SpringBootApplication
|
||||
@ComponentScan(basePackages = {"cn.dcsy.stsy"})
|
||||
@MapperScan("cn.dcsy.stsy.mappers")
|
||||
public class StoriesSystemApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
21
src/main/java/cn/dcsy/stsy/utils/BaseResponse.java
Normal file
21
src/main/java/cn/dcsy/stsy/utils/BaseResponse.java
Normal file
@ -0,0 +1,21 @@
|
||||
package cn.dcsy.stsy.utils;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* @author DC_DC
|
||||
* Date: 2024/4/16/21:22
|
||||
*/
|
||||
@Slf4j
|
||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||
public record BaseResponse(String output, Integer code, String message, Object data) {
|
||||
|
||||
public BaseResponse(String output, Integer code, String message, Object data) {
|
||||
this.output = output;
|
||||
this.code = code;
|
||||
this.message = message;
|
||||
this.data = data;
|
||||
log.info("============================================================");
|
||||
}
|
||||
}
|
@ -1,2 +1,8 @@
|
||||
server:
|
||||
port: 8088
|
||||
port: 8088
|
||||
spring:
|
||||
datasource:
|
||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||
url: jdbc:mysql://localhost:3305
|
||||
username: "root"
|
||||
password: "123456"
|
Loading…
x
Reference in New Issue
Block a user