文章目录
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.环境搭建
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>
< dependency>
< groupId> com.sunxiansheng</ groupId>
< artifactId> common-tool-starter</ artifactId>
< version> 1.0</ version>
</ dependency>
< dependency>
< groupId> com.sunxiansheng</ groupId>
< artifactId> common-exception-starter</ artifactId>
< version> 1.0</ version>
</ dependency>
< dependency>
< groupId> com.sunxiansheng</ groupId>
< artifactId> common-log4j2-starter</ artifactId>
< version> 1.0</ version>
</ dependency>
< dependency>
< groupId> com.sunxiansheng</ groupId>
< artifactId> common-minio-starter</ artifactId>
< version> 1.0</ version>
</ dependency>
< dependency>
< groupId> com.sunxiansheng</ groupId>
< artifactId> common-mybatis-plus-starter</ artifactId>
< version> 1.0</ version>
</ dependency>
< dependency>
< groupId> com.sunxiansheng</ groupId>
< artifactId> common-test-starter</ artifactId>
< version> 1.0</ version>
</ dependency>
< dependency>
< groupId> com.sunxiansheng</ groupId>
< artifactId> common-validation-starter</ artifactId>
< version> 1.0</ version>
</ dependency>
< 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>
< 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 :
initial-size : 10
min-idle : 10
max-active : 50
max-wait : 30000
timeBetweenEvictionRunsMillis : 60000
minEvictableIdleTimeMillis : 300000
testWhileIdle : true
testOnBorrow : true
removeAbandoned : true
removeAbandonedTimeout : 180
logAbandoned : true
minio :
endpoint : http: //guest: 9000
accessKey : guest
secretKey : guest
4.创建启动类
package com. sunxiansheng. core ;
import org. springframework. boot. SpringApplication ;
import org. springframework. boot. autoconfigure. SpringBootApplication ;
@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 ;
@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 ;
@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 ;
@RestController
public class ValidationController {
@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 ;
@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 ;
@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 ;
@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;
}
@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) ;
}
@GetMapping ( "/generateDownloadLink" )
public String generateDownloadLink ( @RequestParam String bucketName,
@RequestParam String objectName) {
return minioUtil. generateDownloadLink ( bucketName, objectName) ;
}
@GetMapping ( "/generatePreviewLink" )
public String generatePreviewLink ( @RequestParam String bucketName,
@RequestParam String objectName) {
return minioUtil. generatePreviewLink ( bucketName, objectName) ;
}
}