实习总结(第二天)

1.@primary注解

讨论Spring的@Primary注解,该注解是框架在3.0版中引入的。

其作用与功能,当有多个相同类型的bean时,使用@Primary来赋予bean更高的优先级。

 

2.@ConfigurationProperties(prefix="file")注解

该注解回去.property或者.ymal文件中去找前缀为file,后面字段对应类中的属性的字段然后赋值

 

3.WebMvcConfigurer接口下的addCorsMappings(CorsRegistry registry)方法,解决跨域问题

 

4.代码统一的返回结果的封装

@Data
public class Result<T> {
    private int code;
    private String msg;
    private T data;

    public Result(CommonResultEnum result, T data) {
        this.code = result.getCode();
        this.msg = result.getMsg();
        this.data = data;
    }

    public Result(CommonResultEnum result, String msg, T data) {
        this.code = result.getCode();
        this.msg = msg;
        this.data = data;
    }

    public Result(CommonResultEnum result) {
        this.code = result.getCode();
        this.msg = result.getMsg();
        this.data = null;
    }
}
public enum CommonResultEnum {
    /**
     * 成功
     */
    SUCCESS(200, "成功"),
    /**
     * 未登录或用户不存在
     */
    NO_USER(401, "未登录或用户不存在"),
    /**
     * 账号密码错误
     */
    USER_ERROR(402, "账号密码错误"),
    /**
     * 登录已过期
     */
    LOGIN_EXPIRED(403, "登录已过期"),
    /**
     * 参数错误
     */
    PARAMETERS_ERROR(600, "参数错误"),
    /**
     * 服务器异常
     */
    SERVER_ERROR(601, "服务器异常");

    private final Integer code;

    private final String msg;

    CommonResultEnum(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public int getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }
}

 

5、@RequsetParam(required=false)表示接收参数,但是可以不传

 

6、SQL语句 IF(ASCII(#{type}),media_type=#{type},True) ,if(exap1,exap2,exap3),如果exap1为真,则取exap2否则取exap3,类似于java中的三目运算符。

ASCII(exap): 返回字符串str的最左字符的数值。返回0,如果str为空字符串。返回NULL,如果str为NULL

 

7、sql函数 DATE_FORMAT(update_time,'%Y-%m') ,改变字段的日期格式,具体用法 MySQL DATE_FORMAT() 函数 (w3school.com.cn)

 

8、@Nullable 注解可以使用在方法、属性、参数上,分别表示方法返回可以为空、属性值可以为空、参数值可以为空。

 

9、MultipartFile相关参数

    @GetMapping("/test")
    public ApiResult test(@RequestParam MultipartFile file) throws IOException {
        // 原文件名称
        System.out.println("file.getOriginalFilename() = " + file.getOriginalFilename());
        // 文件的接收参数 @RequestParam MultipartFile file 中的 file
        System.out.println("file.getName() = " + file.getName());
        // 文件的类型
        System.out.println("file.getContentType() = " + file.getContentType());
        System.out.println("file.getResource() = " + file.getResource());
        System.out.println("file.getBytes() = " + file.getBytes());
        // 文件大小
        System.out.println("file.getSize() = " + file.getSize());
        return ApiResult.ok();
    }

返回值

file.getOriginalFilename() = lbxx.jpg
file.getName() = file
file.getContentType() = image/jpeg
file.getResource() = MultipartFile resource [file]
file.getBytes() = [B@1fa8cd72
file.getSize() = 22820

10、mybatis中的分页插件PageHelper的使用:导入依赖

<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper-spring-boot-starter</artifactId>
    <version>1.2.3</version>
</dependency>
PageHelper.startPage(currentPage, pageSize);
PageInfo<UpdateInfoVO> updatePageInfo = new PageInfo<>(updateInfoVOList);
        return new PageableResponse<>(updatePageInfo.getPageNum(), updatePageInfo.getPageSize(),
                updatePageInfo.getPages(), updatePageInfo.getTotal(), updateInfoVOList);

详情见 (31条消息) 使用PageHelper实现分页查询(详细)_筱晰的博客-CSDN博客_pagehelper分页使用

 

11、Controller 层的方法 接收参数是加该注解 @DateTimeFormat(pattern = "yyyy-MM-dd")

作用:设置接收参数的时间格式,调用该接口时必须以此格式进行参数传递

@DateTimeFormat的其他用处 (31条消息) spring 时间格式化注解@DATETIMEFORMAT和 @JSONFORMAT_可以简单点的博客-CSDN博客_spring datetimeformat

 

12、private static final Logger logger = LoggerFactory.getLogger(DockerController.class);

代码作用,见 (31条消息) LoggerFactory.getLogger用法_lghello的博客-CSDN博客_loggerfactory.getlogger

问题:

@PostMapping("/update/upload")
public Result<String> uploadUpdate(

@PostMapping("/dataset/upload")
public Result<Map<String, Object>> uploadDataset(

@PostMapping("/upload")
public Result<String> uploadFile(

@GetMapping("/download")
public ResponseEntity<Resource> downloadDocker(@RequestParam Integer dockerId, @NotNull HttpServletRequest request) {

@GetMapping("/download")
public ResponseEntity<Resource> downloadFile(@RequestParam Integer fileId, @NotNull HttpServletRequest request) {

@GetMapping("/list")
public Result<List<TestInfo>> datasetInfoList(@RequestParam Integer dockerId) {

代码部分的文件上传功能暂时看不懂,TestController,userController暂时还没与看


posted @ 2022-12-21 12:02  见怪见外  阅读(33)  评论(0编辑  收藏  举报