从零开始整SpringBoot-搭建一个企业级项目
创建Springboot项目
在企业里的项目多数是分Modules的,更多是通过物理方式隔离层之间的职责。
首先按照原有新建SpringBoot项目创建,填好相关信息后,选择Java 8其他不用选。
完成后得到上面的结构,把红色选中的相关模块删除后,得到下面的项目结构
新建Module
什么都不需要选,下一步
填好Name后(注意前缀统一,通过-进行分割)点击完成,得出以下结构。
重复上面步骤,把sikiro-system-infrastructure、sikiro-system-repository新增上去,得到下面结构
添加项目依赖
sikiro-om-api->sikiro-om-application->sikiro-om-repository,sikiro-om-infrastructure被三层直接依赖
一共有两种方式添加:
在File->Project Structure->Modules,点击右侧的+,选择Module。
另外一种在pom.xml添加dependencies
<?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"> <parent> <artifactId>om</artifactId> <groupId>sikiro.system</groupId> <version>1.0.0</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>sikiro-system-api</artifactId> <dependencies> <dependency> <groupId>sikiro.system</groupId> <artifactId>sikiro-system-application</artifactId> <version>1.0.0</version> <scope>compile</scope> </dependency> <dependency> <groupId>sikiro.system</groupId> <artifactId>sikiro-system-infrastructure</artifactId> <version>1.0.0</version> <scope>compile</scope> </dependency> </dependencies> </project>
添加Package
在sikiro-system-api项目添加新package-com.sikiro.system.api,同时添加SikiroSystemApiApplication
@SpringBootApplication() @ComponentScan(basePackages = {"com.sikiro.system"}) public class SikiroSystemApiApplication { public static void main(String[] args) { SpringApplication.run(SikiroSystemApiApplication.class, args); } }
然后继续添加HelloController到新的package-com.sikiro.system.api.controller
@RestController
public class HelloController {
@GetMapping("/api/hi")
public ApiResult Hi(){
return ApiResult.Success();
}
}
在sikiro-system-infrastructure添加下面两个类
package com.sikiro.system.infrastructure.base; import lombok.AccessLevel; import lombok.Setter; /** * API统一响应实体 * @param <T> 业务实体 */ public class ApiResult<T> { @Setter(AccessLevel.PRIVATE) public int resultCode; @Setter(AccessLevel.PRIVATE) public T data; @Setter(AccessLevel.PRIVATE) public String message; @Setter(AccessLevel.PRIVATE) public boolean success; @Setter(AccessLevel.PRIVATE) public boolean failed; @Setter(AccessLevel.PRIVATE) public boolean error; public static <T> ApiResult<T> Success() { return Success(ApiResultCode.Succeed.Message(), null, ApiResultCode.Succeed.Value()); } public static <T> ApiResult<T> Success(T data) { return Success(ApiResultCode.Succeed.Message(), data, ApiResultCode.Succeed.Value()); } public static <T> ApiResult<T> Success(String msg) { return Success(msg, null); } public static <T> ApiResult<T> Success(String msg, T data, int code) { ApiResult<T> result = new ApiResult<>(); result.setMessage(msg); result.setData(data); result.setResultCode(code); result.setSuccess(true); result.setFailed(false); result.setError(false); return result; } public static <T> ApiResult<T> Success(String msg, T data) { return Success(msg, data, ApiResultCode.Succeed.Value()); } public static <T> ApiResult<T> Failed() { return Failed(ApiResultCode.Failed.Message(), null, ApiResultCode.Failed.Value()); } public static <T> ApiResult<T> Failed(String msg) { return Failed(msg, null); } public static <T> ApiResult<T> Failed(String msg, T data) { return Failed(msg, data, ApiResultCode.Failed.Value()); } public static <T> ApiResult<T> Failed(String msg, T data, int code) { ApiResult<T> result = new ApiResult<>(); result.setMessage(msg); result.setData(data); result.setResultCode(code); result.setSuccess(false); result.setFailed(true); result.setError(false); return result; } public static <T> ApiResult<T> Error(String msg) { ApiResult<T> result = new ApiResult<>(); result.setMessage(msg); result.setResultCode(ApiResultCode.Error.Value()); result.setSuccess(false); result.setFailed(true); result.setError(true); return result; } }
package com.sikiro.system.infrastructure.base; public enum ApiResultCode { Succeed(200,"请求成功"), Failed(400,"请求失败"), Error(500,"服务执行异常"); private int _index; private String _msg; ApiResultCode(int index, String msg) { _index = index; _msg = msg; } public int Value() { return _index; } public String Message() { return _msg; } }
最后编辑启动配置
作 者:
陈珙
出 处:http://www.cnblogs.com/skychen1218/
关于作者:专注于微软平台的项目开发。如有问题或建议,请多多赐教!
版权声明:本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接。
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是作者坚持原创和持续写作的最大动力!