diff --git a/src/main/java/com/guo/learningprogresstracker/controller/TestController.java b/src/main/java/com/guo/learningprogresstracker/controller/TestController.java new file mode 100644 index 0000000..a31a8f7 --- /dev/null +++ b/src/main/java/com/guo/learningprogresstracker/controller/TestController.java @@ -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 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); + } +} diff --git a/src/main/java/com/guo/learningprogresstracker/domain/entity/CommonResult.java b/src/main/java/com/guo/learningprogresstracker/domain/entity/CommonResult.java new file mode 100644 index 0000000..60d3f2e --- /dev/null +++ b/src/main/java/com/guo/learningprogresstracker/domain/entity/CommonResult.java @@ -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 { + private Integer code; + + private String message; + + private T data; + + public CommonResult(Integer code, String message){ + this(code,message, null); + } + + public static CommonResult success(){ + return new CommonResult(200,"请求成功",null); + } + + public static CommonResult success(String message){ + return new CommonResult(200,message,null); + } + + public static CommonResult 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 error(String message){ + return new CommonResult<>(400,message,null); + } + + public static CommonResult notFind(){ + return new CommonResult<>(404,"请求的资源不存在",null); + } + public static CommonResult notFind(String message){ + return new CommonResult<>(404,message,null); + } + + public static CommonResult serverError(String message){ + return new CommonResult<>(500,message,null); + } + +} diff --git a/src/main/java/com/guo/learningprogresstracker/domain/entity/TestTableEntity.java b/src/main/java/com/guo/learningprogresstracker/domain/entity/TestTableEntity.java new file mode 100644 index 0000000..58d1077 --- /dev/null +++ b/src/main/java/com/guo/learningprogresstracker/domain/entity/TestTableEntity.java @@ -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(); + } +} \ No newline at end of file diff --git a/src/main/java/com/guo/learningprogresstracker/exception/AppException.java b/src/main/java/com/guo/learningprogresstracker/exception/AppException.java new file mode 100644 index 0000000..5b3e95d --- /dev/null +++ b/src/main/java/com/guo/learningprogresstracker/exception/AppException.java @@ -0,0 +1,12 @@ +package com.guo.learningprogresstracker.exception; +/** + * 应用程序异常 + * + * 数据库连接失败等潜在异常 + * */ +public class AppException extends Exception{ + // 应用程序异常类 + public AppException(String message){ + super(message); + } +} diff --git a/src/main/java/com/guo/learningprogresstracker/exception/ErrorParameterException.java b/src/main/java/com/guo/learningprogresstracker/exception/ErrorParameterException.java new file mode 100644 index 0000000..aaeba50 --- /dev/null +++ b/src/main/java/com/guo/learningprogresstracker/exception/ErrorParameterException.java @@ -0,0 +1,13 @@ +package com.guo.learningprogresstracker.exception; + +/** + * 参数错误 + * + * 数据不合法时抛出此错误 + * 此错误由GlobalExceptionHandler类统一处理 + * */ +public class ErrorParameterException extends Exception { + public ErrorParameterException(String message){ + super(message); + } +} diff --git a/src/main/java/com/guo/learningprogresstracker/exception/NotFindEntitiesException.java b/src/main/java/com/guo/learningprogresstracker/exception/NotFindEntitiesException.java new file mode 100644 index 0000000..12f6c87 --- /dev/null +++ b/src/main/java/com/guo/learningprogresstracker/exception/NotFindEntitiesException.java @@ -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); + } +} diff --git a/src/main/java/com/guo/learningprogresstracker/exception/OperationFailedException.java b/src/main/java/com/guo/learningprogresstracker/exception/OperationFailedException.java new file mode 100644 index 0000000..8235d2f --- /dev/null +++ b/src/main/java/com/guo/learningprogresstracker/exception/OperationFailedException.java @@ -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); + } +} diff --git a/src/main/java/com/guo/learningprogresstracker/mapper/TestTableMapper.java b/src/main/java/com/guo/learningprogresstracker/mapper/TestTableMapper.java new file mode 100644 index 0000000..179beb8 --- /dev/null +++ b/src/main/java/com/guo/learningprogresstracker/mapper/TestTableMapper.java @@ -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 { + +} + + + + diff --git a/src/main/java/com/guo/learningprogresstracker/service/TestTableService.java b/src/main/java/com/guo/learningprogresstracker/service/TestTableService.java new file mode 100644 index 0000000..8a4c6c5 --- /dev/null +++ b/src/main/java/com/guo/learningprogresstracker/service/TestTableService.java @@ -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 getTestEntityById(String id); +} diff --git a/src/main/java/com/guo/learningprogresstracker/service/impl/TestTableServiceImpl.java b/src/main/java/com/guo/learningprogresstracker/service/impl/TestTableServiceImpl.java new file mode 100644 index 0000000..013cc2c --- /dev/null +++ b/src/main/java/com/guo/learningprogresstracker/service/impl/TestTableServiceImpl.java @@ -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 + implements TestTableService { + + @Override + public TestTableEntity getTestEntityById(String id) { + return this.getById(id); + } +} + + + + diff --git a/src/main/java/com/guo/learningprogresstracker/utils/GlobalExceptionHandler.java b/src/main/java/com/guo/learningprogresstracker/utils/GlobalExceptionHandler.java new file mode 100644 index 0000000..d56ed01 --- /dev/null +++ b/src/main/java/com/guo/learningprogresstracker/utils/GlobalExceptionHandler.java @@ -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()); + } +} \ No newline at end of file diff --git a/src/main/resources/db/migration/V20240609_3__fixTableFieldName.sql b/src/main/resources/db/migration/V20240609_3__fixTableFieldName.sql new file mode 100644 index 0000000..309eabb --- /dev/null +++ b/src/main/resources/db/migration/V20240609_3__fixTableFieldName.sql @@ -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; \ No newline at end of file diff --git a/src/main/resources/mapper/TestTableMapper.xml b/src/main/resources/mapper/TestTableMapper.xml new file mode 100644 index 0000000..6040217 --- /dev/null +++ b/src/main/resources/mapper/TestTableMapper.xml @@ -0,0 +1,15 @@ + + + + + + + + + + + id,id_name + + diff --git a/src/test/java/com/guo/learningprogresstracker/DataSourceTest.java b/src/test/java/com/guo/learningprogresstracker/DataSourceTest.java new file mode 100644 index 0000000..275e9fd --- /dev/null +++ b/src/test/java/com/guo/learningprogresstracker/DataSourceTest.java @@ -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 stringStringHashMap = new HashMap<>(); + stringStringHashMap.put("id_name", "guo_test"); + boolean delete = testTableService.removeByMap(stringStringHashMap); + + assertTrue(delete,"删除idName为guo_test的数据失败"); + + } +}