《苍穹外卖》知识梳理P1
一.多模块项目的创建
个人认为对于一个项目的学习,应当先从它的项目结构入手,明确了各个模块的职责与功能,后边尽心编写以及改写的时候会更加得心应手。
该项目分为了3个模块:sky-common模块,sky-pojo模块,sky-server模块,而这三个模块,又从属于一个父模块sky-take-out(sky-take-out中并没有任何内容,只是为了实现统一管理依赖版本,以及聚合其他子模块);
- sky-common模块:子模块,存放公共类,每个公共类都单独对应一个包,例如:
- 工具类utils
- 常量类constants
- 异常类exception
- sky-pojo模块:子模块,存放实体类,例如:
- 实体类DO,用于对应数据库中的表,通过持久层向上传递数据源对象;
- 数据传输对象DTO,用来接收前端传过来的参数,一般会把前端传过来的参数封装进DTO,然后在Service中复制给新new的entity对象,然后进行持久化。
- 显示层对象VO,通常是 Web 向模板渲染引擎层传输的对象。VO就是返回给前端的视图对象。
- sky-server模块:子模块,后端服务类(主要在此编写代码)
- 配置类Config,启动服务器时加载相关配置类进行配置;
- 拦截器类Interceptor,对请求进行拦截,以便实现某些功能(比如登陆校验);
- 切面类Aspect,实现AOP面向切面编程,在不影响原有代码的基础是进行功能增强;
- 控制器类Controller,处理用户请求,负责接收请求参数,调用业务逻辑处理(通常是Service层),然后返回响应给用户。在Spring MVC等框架中,通常使用@Controller注解来标识控制器
- 服务类Service,执行业务逻辑,包含应用的核心业务规则。Service层负责处理业务逻辑,可能涉及到事务控制、数据处理等。在Spring中,通常使用@Service注解来标识服务。
- 数据访问类Mapper, 负责与数据库交互,进行数据的持久化操作。在MyBatis等持久层框架中,通常使用Mapper来定义数据访问的接口。在Spring Data JPA等框架中,通常使用@Repository注解标识数据访问对象。
1.1 Springboot多模块项目的创建
1.创建父模块
- 1.1新建一个Springboot工程,使用默认即可;
- 1.2项目设置这里,类型选择Maven,Java版本使用Java8(我这里只有17,21),如果不能直接在这里选择java8与springboot2.7.3,后续也是可以直接在配置文件中进行修改的;
- 1.3选择spirngboot的版本2.7.3(我这里只能选择3以上的版本,因为前边选的Java17,这两个是有关联的);然后直接下一步即可;
- 1.4创建
- 1.5创建后的项目目录如下:我们需要删除一些内容,只保留我们需要的;
将选中的.mvn,src,HELP.md,mvnw,mvnw.cmd全部删掉
二.创建子模块
- 2.1右键父模块,选择创建》新模块》这次直接选择Maven,
- 2.2 修改子模块的名称,默认父模块是开始创建的那个;》完成
三.修改pom.xml中springboot与java版本的信息(如果需要修改)
在父模块中:
<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.7.3</version><relativePath/>
</parent>
<properties><java.version>8</java.version>
</properties>
3.1配置父模块中的pom.xml
<!--父模块统一进行依赖版本管理-->
<properties><java.version>8</java.version><mybatis.spring>2.2.0</mybatis.spring><lombok>1.18.20</lombok><fastjson>1.2.76</fastjson><commons.lang>2.6</commons.lang><druid>1.2.1</druid><pagehelper>1.3.0</pagehelper><aliyun.sdk.oss>3.10.2</aliyun.sdk.oss><knife4j>3.0.2</knife4j><aspectj>1.9.4</aspectj><jjwt>0.9.1</jjwt><jaxb-api>2.3.1</jaxb-api><poi>3.16</poi><junit>4.12</junit>
</properties>
<!--全部使用可选依赖,子模块需要时需要按需导入,版本使用统一设定好的即可-->
<dependencyManagement><dependencies><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>${mybatis.spring}</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>${junit}</version><scope>test</scope></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>${lombok}</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>${fastjson}</version></dependency><dependency><groupId>commons-lang</groupId><artifactId>commons-lang</artifactId><version>${commons.lang}</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId><version>${druid}</version></dependency><dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper-spring-boot-starter</artifactId><version>${pagehelper}</version></dependency><dependency><groupId>com.github.xiaoymin</groupId><artifactId>knife4j-spring-boot-starter</artifactId><version>${knife4j}</version></dependency><dependency><groupId>org.aspectj</groupId><artifactId>aspectjrt</artifactId><version>${aspectj}</version></dependency><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>${aspectj}</version></dependency><dependency><groupId>io.jsonwebtoken</groupId><artifactId>jjwt</artifactId><version>${jjwt}</version></dependency><dependency><groupId>com.aliyun.oss</groupId><artifactId>aliyun-sdk-oss</artifactId><version>${aliyun.sdk.oss}</version></dependency><dependency><groupId>javax.xml.bind</groupId><artifactId>jaxb-api</artifactId><version>${jaxb-api}</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>${junit}</version><scope>test</scope></dependency><!-- poi --><dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>${poi}</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>${poi}</version></dependency><!--微信支付--><dependency><groupId>com.github.wechatpay-apiv3</groupId><artifactId>wechatpay-apache-httpclient</artifactId><version>0.4.8</version></dependency><!--文件上传--><!-- https://mvnrepository.com/artifact/cn.hutool/hutool-all --><dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>5.8.14</version></dependency><dependency><groupId>com.aliyun.oss</groupId><artifactId>aliyun-sdk-oss</artifactId><version>3.15.1</version></dependency></dependencies>
</dependencyManagement>
3.2配置子模块的pom.xml
子模块由于继承了父模块中的依赖,所以只要按照需要直接导入即可,而不用添加版本(默认使用父模块中统一制定的版本);
<!--common模块中的依赖,其余相同,请根据需要进行导入-->
<dependencies><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId></dependency><dependency><groupId>commons-lang</groupId><artifactId>commons-lang</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-json</artifactId></dependency><dependency><groupId>io.jsonwebtoken</groupId><artifactId>jjwt</artifactId></dependency><!--支持配置属性类,yml文件中可以提示配置项--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId><optional>true</optional></dependency><dependency><groupId>com.aliyun.oss</groupId><artifactId>aliyun-sdk-oss</artifactId></dependency><dependency><groupId>javax.xml.bind</groupId><artifactId>jaxb-api</artifactId></dependency><!--微信支付--><dependency><groupId>com.github.wechatpay-apiv3</groupId><artifactId>wechatpay-apache-httpclient</artifactId></dependency>
</dependencies>
<!--pojo模块-->
<dependencies><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.9.2</version></dependency><dependency><groupId>com.github.xiaoymin</groupId><artifactId>knife4j-spring-boot-starter</artifactId></dependency>
</dependencies>
<dependencies><!--当前模块需要使用其他模块的内容,也作为依赖导入;--><dependency><groupId>com.sky</groupId><artifactId>zhisou-common</artifactId><version>0.0.1-SNAPSHOT</version></dependency><dependency><groupId>com.sky</groupId><artifactId>zhisou-pojo</artifactId><version>0.0.1-SNAPSHOT</version></dependency><!--其他依赖--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><scope>compile</scope></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope></dependency><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId></dependency><dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId></dependency><dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper-spring-boot-starter</artifactId></dependency><dependency><groupId>org.aspectj</groupId><artifactId>aspectjrt</artifactId></dependency><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId></dependency><dependency><groupId>com.github.xiaoymin</groupId><artifactId>knife4j-spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-cache</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-websocket</artifactId></dependency><dependency><groupId>javax.xml.bind</groupId><artifactId>jaxb-api</artifactId></dependency><!-- poi --><dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId></dependency><!--单元测试--><dependency><groupId>junit</groupId><artifactId>junit</artifactId><scope>test</scope></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><scope>compile</scope></dependency><!--文件上传--><dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId></dependency><dependency><groupId>com.aliyun.oss</groupId><artifactId>aliyun-sdk-oss</artifactId><version>3.15.1</version></dependency>
</dependencies>
<!--还需要额外导入插件-->
<build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins>
</build>
3.3在server模块的com.sky.下设置启动类
(server模块这是我们主要进行开发的模块);
@SpringBootApplication
@EnableTransactionManagement //开启注解方式的事务管理
@Slf4j
public class ZhiSouApplication {public static void main(String[] args) {SpringApplication.run(ZhiSouApplication.class, args);log.info("server started ...");}
}
至此,项目皮配置已经完成;