当前位置: 首页 > article >正文

构建core模块

文章目录

    • 1.环境搭建
        • 1.sunrays-common下新建core模块
        • 2.引入依赖,并设置打包常规配置
    • 2.测试使用
        • 1.启动!
          • 1.创建模块
          • 2.引入依赖
          • 3.application.yml 配置MySQL和Minio
          • 4.创建启动类
          • 5.启动测试
        • 2.common-web-starter
          • 1.目录
          • 2.WebController.java
          • 3.结果
        • 3.common-validation-starter
          • 1.目录
          • 2.Req.java 请求参数,使用JSR303注解
          • 3.ValidationController.java 测试校验参数
          • 4.结果
        • 4.common-test-starter
          • 1.目录
          • 2.TestComponent.java
          • 3.TestClass.java 测试类
          • 4.结果
        • 5.common-mybatis-plus-starter
          • 1.生成CRUD(注意五个必要字段)
          • 2.生成的代码结构
          • 3.测试
        • 6.common-minio-starter
          • 1.测试Controller

1.环境搭建

1.sunrays-common下新建core模块

CleanShot 2024-11-16 at 17.15.50@2x

2.引入依赖,并设置打包常规配置
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.sunxiansheng</groupId>
        <artifactId>sunrays-common</artifactId>
        <version>1.0</version>
    </parent>

    <version>1.0</version>

    <artifactId>common-core-starter</artifactId>

    <description>本核心模块提供开发的基本工具包</description>

    <dependencies>
        <!-- common-tool-starter -->
        <dependency>
            <groupId>com.sunxiansheng</groupId>
            <artifactId>common-tool-starter</artifactId>
            <version>1.0</version>
        </dependency>
        <!-- common-exception-starter -->
        <dependency>
            <groupId>com.sunxiansheng</groupId>
            <artifactId>common-exception-starter</artifactId>
            <version>1.0</version>
        </dependency>
        <!-- common-log4j2-starter -->
        <dependency>
            <groupId>com.sunxiansheng</groupId>
            <artifactId>common-log4j2-starter</artifactId>
            <version>1.0</version>
        </dependency>
        <!-- common-minio-starter -->
        <dependency>
            <groupId>com.sunxiansheng</groupId>
            <artifactId>common-minio-starter</artifactId>
            <version>1.0</version>
        </dependency>
        <!-- common-mybatis-plus-starter -->
        <dependency>
            <groupId>com.sunxiansheng</groupId>
            <artifactId>common-mybatis-plus-starter</artifactId>
            <version>1.0</version>
        </dependency>
        <!-- common-test-starter -->
        <dependency>
            <groupId>com.sunxiansheng</groupId>
            <artifactId>common-test-starter</artifactId>
            <version>1.0</version>
        </dependency>
        <!-- common-validation-starter -->
        <dependency>
            <groupId>com.sunxiansheng</groupId>
            <artifactId>common-validation-starter</artifactId>
            <version>1.0</version>
        </dependency>
        <!-- common-web-starter -->
        <dependency>
            <groupId>com.sunxiansheng</groupId>
            <artifactId>common-web-starter</artifactId>
            <version>1.0</version>
        </dependency>
    </dependencies>
</project>

2.测试使用

1.启动!
1.创建模块

CleanShot 2024-11-16 at 17.32.48@2x

2.引入依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.sunxiansheng</groupId>
        <artifactId>sunrays-demo</artifactId>
        <version>1.0</version>
    </parent>

    <version>1.0</version>

    <artifactId>common-core-starter-demo</artifactId>

    <dependencies>
        <!-- sunrays-framework 核心模块 -->
        <dependency>
            <groupId>com.sunxiansheng</groupId>
            <artifactId>common-core-starter</artifactId>
            <version>1.0</version>
        </dependency>
    </dependencies>

</project>
3.application.yml 配置MySQL和Minio
server:
  port: 8080 # 服务端口
spring:
  # 数据源配置
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: guest
    password: guest
    url: guest
    type: com.alibaba.druid.pool.DruidDataSource # druid连接池
    druid:
      initial-size: 10          # 初始化连接数(适当减少以节省资源)
      min-idle: 10              # 最小空闲连接数
      max-active: 50            # 最大连接数(根据业务需求调整)
      max-wait: 30000           # 获取连接的最大等待时间(30秒)
      timeBetweenEvictionRunsMillis: 60000  # 每60秒检查一次空闲连接
      minEvictableIdleTimeMillis: 300000    # 最小空闲时间5分钟后回收
      testWhileIdle: true       # 检查空闲连接是否可用
      testOnBorrow: true        # 检查从连接池获取的连接是否可用
      removeAbandoned: true     # 启用清理超时连接
      removeAbandonedTimeout: 180  # 连接超时时间180秒
      logAbandoned: true        # 记录被清理的连接日志
# minio配置
minio:
  endpoint: http://guest:9000 # minio服务地址
  accessKey: guest # minio服务的accessKey
  secretKey: guest # minio服务的secretKey
4.创建启动类
package com.sunxiansheng.core;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * Description: 核心启动类
 *
 * @Author sun
 * @Create 2024/11/16 17:34
 * @Version 1.0
 */
@SpringBootApplication
public class CoreApplication {

    public static void main(String[] args) {
        SpringApplication.run(CoreApplication.class, args);
    }
}
5.启动测试

CleanShot 2024-11-16 at 18.58.41@2x

CleanShot 2024-11-16 at 18.59.02@2x

2.common-web-starter
1.目录

CleanShot 2024-11-16 at 19.00.08@2x

2.WebController.java
package com.sunxiansheng.core.web.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Description: Web控制器
 *
 * @Author sun
 * @Create 2024/11/16 18:58
 * @Version 1.0
 */
@RestController
public class WebController {

    @RequestMapping("/hello")
    public String hello() {
        return "Hello, World!";
    }
}
3.结果

CleanShot 2024-11-16 at 19.00.34@2x

3.common-validation-starter
1.目录

2.Req.java 请求参数,使用JSR303注解
package com.sunxiansheng.core.validation.entity.req;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Past;
import java.io.Serializable;
import java.util.Date;

/**
 * Description: 请求参数
 *
 * @Author sun
 * @Create 2024/11/16 19:03
 * @Version 1.0
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Req implements Serializable {

    private static final long serialVersionUID = 1L;

    @NotNull(message = "ID不能为空")
    private Long id;

    @NotBlank(message = "用户名不能为空")
    private String name;

    @NotNull(message = "生日不能为空")
    @Past(message = "生日必须是过去的日期")
    private Date birthday;
}
3.ValidationController.java 测试校验参数
package com.sunxiansheng.core.validation.controller;

import com.sunxiansheng.core.validation.entity.req.Req;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * Description: 控制器
 *
 * @Author sun
 * @Create 2024/11/16 19:04
 * @Version 1.0
 */
@RestController
public class ValidationController {

    /**
     * 参数校验
     *
     * @param req
     * @return
     */
    @RequestMapping("/validation")
    public Boolean validation(@RequestBody @Valid Req req) {
        return true;
    }
}
4.结果

CleanShot 2024-11-16 at 19.22.22@2x

4.common-test-starter
1.目录

CleanShot 2024-11-16 at 19.32.59@2x

2.TestComponent.java
package com.sunxiansheng.core.validation.test;

import org.springframework.stereotype.Component;

/**
 * Description: 测试
 *
 * @Author sun
 * @Create 2024/11/16 19:28
 * @Version 1.0
 */
@Component
public class TestComponent {

    public String test() {
        return "test";
    }
}
3.TestClass.java 测试类
package com.sunxiansheng.test;

import com.sunxiansheng.core.CoreApplication;
import com.sunxiansheng.core.validation.test.TestComponent;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;

/**
 * Description: 测试类
 *
 * @Author sun
 * @Create 2024/11/16 19:24
 * @Version 1.0
 */
@SpringBootTest(classes = CoreApplication.class) // 指定启动类
public class TestClass {

    @Resource
    private TestComponent testComponent;

    @Test
    void test() {
        System.out.println("testComponent.test() = " + testComponent.test());
    }
}
4.结果

CleanShot 2024-11-16 at 19.32.32@2x

5.common-mybatis-plus-starter
1.生成CRUD(注意五个必要字段)

CleanShot 2024-11-16 at 19.37.18@2x

2.生成的代码结构

CleanShot 2024-11-16 at 20.12.34@2x

3.测试

CleanShot 2024-11-16 at 20.14.50@2x

6.common-minio-starter
1.测试Controller
package com.sunxiansheng.core.minio.controller;

import com.sunxiansheng.minio.utils.MinioUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * Minio 测试 Controller
 *
 * @Author sun
 * @Create 2024/11/16
 * @Version 1.0
 */
@RestController
@RequestMapping("/minio")
public class MinioController {

    @Autowired
    private MinioUtil minioUtil;

    /**
     * 检查存储桶是否存在
     */
    @GetMapping("/bucketExists/{bucketName}")
    public boolean bucketExists(@PathVariable String bucketName) {
        return minioUtil.bucketExists(bucketName);
    }

    /**
     * 列出所有存储桶的名字
     */
    @GetMapping("/listBucketNames")
    public List<String> listBucketNames() {
        return minioUtil.listBucketNames();
    }

    /**
     * 根据名字创建存储桶
     */
    @PostMapping("/makeBucket/{bucketName}")
    public String makeBucket(@PathVariable String bucketName) {
        minioUtil.makeBucket(bucketName);
        return "Bucket '" + bucketName + "' created successfully!";
    }

    /**
     * 根据名字移除一个空桶
     */
    @DeleteMapping("/removeBucket/{bucketName}")
    public String removeBucket(@PathVariable String bucketName) {
        minioUtil.removeBucket(bucketName);
        return "Bucket '" + bucketName + "' removed successfully!";
    }

    /**
     * 下载对象到指定位置
     */
    @GetMapping("/downloadObject")
    public String downloadObject(@RequestParam String bucketName,
                                 @RequestParam String objectName,
                                 @RequestParam String fileName) {
        minioUtil.downloadObject(bucketName, objectName, fileName);
        return "Object '" + objectName + "' downloaded successfully to " + fileName;
    }

    /**
     * 文件上传并返回预览url和下载url
     */
    @PostMapping("/upload")
    public List<String> uploadFile(@RequestParam("file") MultipartFile file,
                                   @RequestParam String bucketName) {
        return minioUtil.putObject(file, bucketName);
    }

    /**
     * 删除指定前缀的所有对象(文件或文件夹)
     */
    @DeleteMapping("/removeObjectOrFolder")
    public String removeObjectOrFolder(@RequestParam String bucketName,
                                       @RequestParam String prefix) {
        return minioUtil.removeObjectOrFolder(bucketName, prefix);
    }

    /**
     * 获取下载url
     */
    @GetMapping("/generateDownloadLink")
    public String generateDownloadLink(@RequestParam String bucketName,
                                       @RequestParam String objectName) {
        return minioUtil.generateDownloadLink(bucketName, objectName);
    }

    /**
     * 获取预览url
     */
    @GetMapping("/generatePreviewLink")
    public String generatePreviewLink(@RequestParam String bucketName,
                                      @RequestParam String objectName) {
        return minioUtil.generatePreviewLink(bucketName, objectName);
    }
}

http://www.kler.cn/a/509445.html

相关文章:

  • 从零开始:Gitee 仓库创建与 Git 配置指南
  • 云消息队列 Kafka 版 V3 系列荣获信通院“云原生技术创新标杆案例”
  • 【C++基础】enum,union,uint8_t,static
  • 计算机网络常见协议
  • python-44-嵌入式数据库SQLite和DuckDB
  • 春秋杯-WEB
  • 接口测试Day10-接口对象封装封装TpShop登录接口
  • mono3d汇总
  • Go语言之路————数组、切片、map
  • PL/SQL语言的文件操作
  • macOS 安装JDK17
  • 【HarmonyOS-开发指南】
  • 使用opencv.js 的时候报错 Uncaught 1022911432
  • JAVA:在IDEA引入本地jar包的方法(不读取maven目录jar包)
  • 【AcWing】蓝桥杯辅导课-递归与递推
  • 了解EJB两种主要类型:BMP与CMP
  • 【Unity】使用Canvas Group改变UI的透明度
  • QrCode.js 技术文档:二维码生成与导出工具类
  • LocalSend - 免费、开源、跨平台的局域网文件传输工具
  • leetcode206-反转链表
  • 数字化转型的新引擎:开源AI智能名片2+1链动模式S2B2C商城小程序源码的应用与实践
  • 年后找工作需要注意的事项
  • java每日精进1.17(用户登录,登出,Token相关)
  • Scala语言的编程范式
  • HTML格式
  • PCL 快速均匀采样【2025最新版】