构建core模块
文章目录
1.环境搭建
1.sunrays-common下新建core模块
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.创建模块
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.启动测试
2.common-web-starter
1.目录
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.结果
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.结果
4.common-test-starter
1.目录
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.结果
5.common-mybatis-plus-starter
1.生成CRUD(注意五个必要字段)
2.生成的代码结构
3.测试
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);
}
}
原文地址:https://blog.csdn.net/m0_64637029/article/details/145234998
免责声明:本站文章内容转载自网络资源,如侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!