欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 健康 > 养生 > 构建core模块

构建core模块

2025/1/20 2:13:55 来源:https://blog.csdn.net/m0_64637029/article/details/145234998  浏览:    关键词:构建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.Driverusername: guestpassword: guesturl: guesttype: 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服务的accessKeysecretKey: 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 {@Resourceprivate TestComponent testComponent;@Testvoid 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 {@Autowiredprivate 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);}
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com