init_object,增加了不完全的登录API,完善数据库设计

This commit is contained in:
guo
2024-06-28 22:31:21 +08:00
parent b0deeb58af
commit f1dc42fce5
14 changed files with 417 additions and 0 deletions
@@ -0,0 +1,37 @@
package com.guo.learningprogresstracker.controller;
import com.guo.learningprogresstracker.domain.entity.CommonResult;
import com.guo.learningprogresstracker.domain.entity.TestTableEntity;
import com.guo.learningprogresstracker.exception.AppException;
import com.guo.learningprogresstracker.exception.ErrorParameterException;
import com.guo.learningprogresstracker.exception.NotFindEntitiesException;
import com.guo.learningprogresstracker.exception.OperationFailedException;
import com.guo.learningprogresstracker.service.TestTableService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
@Slf4j
public class TestController {
@Autowired
TestTableService testTableService;
@GetMapping("/test/{id}")
public CommonResult<TestTableEntity> getTestEntityById(@PathVariable("id") String id) throws Exception {
log.info("testGet请求访问");
if ("1".equals(id)) {
throw new AppException("测试AppException");
} else if ("2".equals(id)) {
throw new ErrorParameterException("测试ErrorParameterException");
}else if ("3".equals(id)) {
throw new NotFindEntitiesException("测试NotFindEntitiesException");
}else if ("4".equals(id)) {
throw new OperationFailedException("测试OperationFailedException");
}
TestTableEntity testTableEntity = testTableService.getTestEntityById(id);
return CommonResult.success(testTableEntity);
}
}
@@ -0,0 +1,54 @@
package com.guo.learningprogresstracker.domain.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class CommonResult<T> {
private Integer code;
private String message;
private T data;
public CommonResult(Integer code, String message){
this(code,message, null);
}
public static CommonResult<String> success(){
return new CommonResult(200,"请求成功",null);
}
public static CommonResult<String> success(String message){
return new CommonResult(200,message,null);
}
public static CommonResult<Object> success(String message,Object data){
return new CommonResult(200,"请求成功,"+message,data);
}
public static CommonResult success(Object data){
return new CommonResult(200,"请求成功",data);
}
public static CommonResult<String> error(String message){
return new CommonResult<>(400,message,null);
}
public static CommonResult<String> notFind(){
return new CommonResult<>(404,"请求的资源不存在",null);
}
public static CommonResult<String> notFind(String message){
return new CommonResult<>(404,message,null);
}
public static CommonResult<String> serverError(String message){
return new CommonResult<>(500,message,null);
}
}
@@ -0,0 +1,69 @@
package com.guo.learningprogresstracker.domain.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import java.io.Serializable;
import lombok.Data;
/**
* 测试表
* @author Guo
* @TableName test_table
*/
@TableName(value ="test_table")
@Data
public class TestTableEntity implements Serializable {
/**
* 测试主键
*/
@TableId(type = IdType.AUTO)
private Integer id;
/**
* id名字
*/
private String idName;
@TableField(exist = false)
private static final long serialVersionUID = 1L;
@Override
public boolean equals(Object that) {
if (this == that) {
return true;
}
if (that == null) {
return false;
}
if (getClass() != that.getClass()) {
return false;
}
TestTableEntity other = (TestTableEntity) that;
return (this.getId() == null ? other.getId() == null : this.getId().equals(other.getId()))
&& (this.getIdName() == null ? other.getIdName() == null : this.getIdName().equals(other.getIdName()));
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((getId() == null) ? 0 : getId().hashCode());
result = prime * result + ((getIdName() == null) ? 0 : getIdName().hashCode());
return result;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(getClass().getSimpleName());
sb.append(" [");
sb.append("Hash = ").append(hashCode());
sb.append(", id=").append(id);
sb.append(", idName=").append(idName);
sb.append(", serialVersionUID=").append(serialVersionUID);
sb.append("]");
return sb.toString();
}
}
@@ -0,0 +1,12 @@
package com.guo.learningprogresstracker.exception;
/**
* 应用程序异常
*
* 数据库连接失败等潜在异常
* */
public class AppException extends Exception{
// 应用程序异常类
public AppException(String message){
super(message);
}
}
@@ -0,0 +1,13 @@
package com.guo.learningprogresstracker.exception;
/**
* 参数错误
*
* 数据不合法时抛出此错误
* 此错误由GlobalExceptionHandler类统一处理
* */
public class ErrorParameterException extends Exception {
public ErrorParameterException(String message){
super(message);
}
}
@@ -0,0 +1,16 @@
package com.guo.learningprogresstracker.exception;
/**
* 没有发现实体错误
*
* 当查询一个或多个实体时没有返回值时抛出此错误
* (你需要查询一个实体,而非模糊查询, 理应提供已存在的实体id
* 但服务器却没找到,既无法理解请求的操作,进而返回code:400)
*
* 响应code为404
* */
public class NotFindEntitiesException extends Exception {
public NotFindEntitiesException(String message){
super(message);
}
}
@@ -0,0 +1,14 @@
package com.guo.learningprogresstracker.exception;
/**
* 操作失败异常
*
* 当查询失败或者增删改的时候返回false
* 响应code为404
* */
public class OperationFailedException extends Exception{
public OperationFailedException(){}
public OperationFailedException(String message){
super(message);
}
}
@@ -0,0 +1,21 @@
package com.guo.learningprogresstracker.mapper;
import com.guo.learningprogresstracker.domain.entity.TestTableEntity;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
/**
* @author Administrator
* @description 针对表【test_table(测试表)】的数据库操作Mapper
* @createDate 2024-04-28 18:01:21
* @Entity generator.domain.TestTable
*/
@Mapper
public interface TestTableMapper extends BaseMapper<TestTableEntity> {
}
@@ -0,0 +1,15 @@
package com.guo.learningprogresstracker.service;
import com.guo.learningprogresstracker.domain.entity.TestTableEntity;
import com.baomidou.mybatisplus.extension.service.IService;
/**
* @author Administrator
* @description 针对表【test_table(测试表)】的数据库操作Service
* @createDate 2024-04-28 18:01:21
*/
//@Service
public interface TestTableService extends IService<TestTableEntity> {
TestTableEntity getTestEntityById(String id);
}
@@ -0,0 +1,26 @@
package com.guo.learningprogresstracker.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guo.learningprogresstracker.domain.entity.TestTableEntity;
import com.guo.learningprogresstracker.mapper.TestTableMapper;
import com.guo.learningprogresstracker.service.TestTableService;
import org.springframework.stereotype.Service;
/**
* @author Administrator
* @description 针对表【test_table(测试表)】的数据库操作Service实现
* @createDate 2024-04-28 18:01:21
*/
@Service
public class TestTableServiceImpl extends ServiceImpl<TestTableMapper, TestTableEntity>
implements TestTableService {
@Override
public TestTableEntity getTestEntityById(String id) {
return this.getById(id);
}
}
@@ -0,0 +1,48 @@
package com.guo.learningprogresstracker.utils;
import com.guo.learningprogresstracker.domain.entity.CommonResult;
import com.guo.learningprogresstracker.exception.AppException;
import com.guo.learningprogresstracker.exception.ErrorParameterException;
import com.guo.learningprogresstracker.exception.NotFindEntitiesException;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* @author guo
*/
@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler({ErrorParameterException.class})
@ResponseBody
public CommonResult errorParameterException(ErrorParameterException ex){
//code:400
return CommonResult.error(ex.getMessage());
}
@ExceptionHandler({NotFindEntitiesException.class})
@ResponseBody
public CommonResult notFindEntitiesException(NotFindEntitiesException ex){
//code:400
return CommonResult.error(ex.getMessage());
}
@ExceptionHandler({AppException.class})
@ResponseBody
public CommonResult AppException(AppException ex){
//code:500
return CommonResult.serverError(ex.getMessage());
}
@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseBody
public CommonResult MyMethodArgumentNotValidException(MethodArgumentNotValidException ex){
BindingResult bindingResult = ex.getBindingResult();
//code:400
return CommonResult.error(bindingResult.getFieldError().getDefaultMessage());
}
}
@@ -0,0 +1,37 @@
-- Study Expectations table
ALTER TABLE study_expectations RENAME COLUMN ExpectationID TO expectation_id;
ALTER TABLE study_expectations RENAME COLUMN SessionID TO session_id;
ALTER TABLE study_expectations RENAME COLUMN Description TO description;
-- Study Report Fragments table
ALTER TABLE study_report_fragments RENAME COLUMN FragmentID TO fragment_id;
ALTER TABLE study_report_fragments RENAME COLUMN SessionID TO session_id;
ALTER TABLE study_report_fragments RENAME COLUMN Content TO content;
ALTER TABLE study_report_fragments RENAME COLUMN CreatedTime TO created_time;
-- Study Reports table
ALTER TABLE study_reports RENAME COLUMN ReportID TO report_id;
ALTER TABLE study_reports RENAME COLUMN SessionID TO session_id;
ALTER TABLE study_reports RENAME COLUMN Content TO content;
ALTER TABLE study_reports RENAME COLUMN CreatedTime TO created_time;
-- Study Sessions table
ALTER TABLE study_sessions RENAME COLUMN SessionID TO session_id;
ALTER TABLE study_sessions RENAME COLUMN TaskID TO task_id;
ALTER TABLE study_sessions RENAME COLUMN StartTime TO start_time;
ALTER TABLE study_sessions RENAME COLUMN EndTime TO end_time;
ALTER TABLE study_sessions RENAME COLUMN ActualTime TO actual_time;
ALTER TABLE study_sessions RENAME COLUMN EffectiveTime TO effective_time;
ALTER TABLE study_sessions RENAME COLUMN EffectivenessRatio TO effectiveness_ratio;
ALTER TABLE study_sessions RENAME COLUMN SessionState TO session_state;
-- Tasks table
ALTER TABLE tasks RENAME COLUMN TaskID TO task_id;
ALTER TABLE tasks RENAME COLUMN TaskName TO task_name;
ALTER TABLE tasks RENAME COLUMN MaterialURL TO material_url;
ALTER TABLE tasks RENAME COLUMN Urgency TO urgency;
ALTER TABLE tasks RENAME COLUMN Importance TO importance;
ALTER TABLE tasks RENAME COLUMN ContentDifficulty TO content_difficulty;
ALTER TABLE tasks RENAME COLUMN FutureValue TO future_value;
ALTER TABLE tasks RENAME COLUMN SubjectivePriority TO subjective_priority;
ALTER TABLE tasks RENAME COLUMN CalculatedPriority TO calculated_priority;
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.guo.learningprogresstracker.mapper.TestTableMapper">
<resultMap id="BaseResultMap" type="com.guo.learningprogresstracker.domain.entity.TestTableEntity">
<id property="id" column="id" jdbcType="INTEGER"/>
<result property="idName" column="id_name" jdbcType="VARCHAR"/>
</resultMap>
<sql id="Base_Column_List">
id,id_name
</sql>
</mapper>
@@ -0,0 +1,40 @@
package com.guo.learningprogresstracker;
import com.guo.learningprogresstracker.domain.entity.TestTableEntity;
import com.guo.learningprogresstracker.service.impl.TestTableServiceImpl;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.HashMap;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* 用于测试数据库连接是否成功
*/
@SpringBootTest
public class DataSourceTest {
@Autowired
TestTableServiceImpl testTableService;
@Test
public void testAdd(){
TestTableEntity testTableEntity = new TestTableEntity();
testTableEntity.setIdName("guo_test");
boolean save = testTableService.save(testTableEntity);
assertTrue(save,"创建idName为guo_test的数据成功");
}
@Test
public void testDelete(){
HashMap<String, Object> stringStringHashMap = new HashMap<>();
stringStringHashMap.put("id_name", "guo_test");
boolean delete = testTableService.removeByMap(stringStringHashMap);
assertTrue(delete,"删除idName为guo_test的数据失败");
}
}