1. 新建maven项目springboot-module
2.把src删掉,新建module项目
- springboot-module-api
- springboot-module-model
- springboot-module-service
- springboot-module-util
- springboot-module-web
3. 添加模块之间的依赖
3.1 springboot-module.pom
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>com.springboot.module</groupId> 8 <artifactId>springboot-module</artifactId> 9 <packaging>pom</packaging> 10 <version>1.0-SNAPSHOT</version> 11 <modules> 12 <module>springboot-module-api</module> 13 <module>springboot-module-model</module> 14 <module>springboot-module-service</module> 15 <module>springboot-module-util</module> 16 <module>springboot-module-web</module> 17 </modules> 18 19 <!-- Spring boot 父引用--> 20 <parent> 21 <groupId>org.springframework.boot</groupId> 22 <artifactId>spring-boot-starter-parent</artifactId> 23 <version>1.4.1.RELEASE</version> 24 </parent> 25 <dependencies> 26 <!-- Spring boot 核心web--> 27 <dependency> 28 <groupId>org.springframework.boot</groupId> 29 <artifactId>spring-boot-starter-web</artifactId> 30 </dependency> 31 <dependency> 32 <groupId>org.projectlombok</groupId> 33 <artifactId>lombok</artifactId> 34 <version>1.16.18</version> 35 </dependency> 36 <dependency> 37 <groupId>org.springframework.boot</groupId> 38 <artifactId>spring-boot-starter-logging</artifactId> 39 </dependency> 40 <dependency> 41 <groupId>org.springframework.boot</groupId> 42 <artifactId>spring-boot-starter-test</artifactId> 43 </dependency> 44 </dependencies> 45 46 <build> 47 <plugins> 48 <plugin> 49 <groupId>org.springframework.boot</groupId> 50 <artifactId>spring-boot-maven-plugin</artifactId> 51 <executions> 52 <execution> 53 <goals> 54 <goal>repackage</goal> 55 </goals> 56 </execution> 57 </executions> 58 <configuration> 59 <executable>true</executable> 60 </configuration> 61 </plugin> 62 </plugins> 63 </build> 64 </project>
3.2 springboot-module-api.pom
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <parent> 6 <artifactId>springboot-module</artifactId> 7 <groupId>com.springboot.module</groupId> 8 <version>1.0-SNAPSHOT</version> 9 </parent> 10 <modelVersion>4.0.0</modelVersion> 11 <artifactId>springboot-module-api</artifactId> 12 13 <dependencies> 14 <dependency> 15 <groupId>com.springboot.module</groupId> 16 <artifactId>springboot-module-util</artifactId> 17 <version>1.0-SNAPSHOT</version> 18 <scope>compile</scope> 19 </dependency> 20 </dependencies> 21 </project>
3.3 springboot-module-model.pom
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <parent> 6 <artifactId>springboot-module</artifactId> 7 <groupId>com.springboot.module</groupId> 8 <version>1.0-SNAPSHOT</version> 9 </parent> 10 <modelVersion>4.0.0</modelVersion> 11 <artifactId>springboot-module-model</artifactId> 12 13 <dependencies> 14 <dependency> 15 <groupId>org.springframework.boot</groupId> 16 <artifactId>spring-boot-starter-data-jpa</artifactId> 17 </dependency> 18 <dependency> 19 <groupId>mysql</groupId> 20 <artifactId>mysql-connector-java</artifactId> 21 </dependency> 22 <dependency> 23 <groupId>com.springboot.module</groupId> 24 <artifactId>springboot-module-util</artifactId> 25 <version>1.0-SNAPSHOT</version> 26 <scope>compile</scope> 27 </dependency> 28 </dependencies> 29 30 </project>
3.4 springboot-module-service.pom
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <parent> 6 <artifactId>springboot-module</artifactId> 7 <groupId>com.springboot.module</groupId> 8 <version>1.0-SNAPSHOT</version> 9 </parent> 10 <modelVersion>4.0.0</modelVersion> 11 <artifactId>springboot-module-service</artifactId> 12 13 <dependencies> 14 <dependency> 15 <groupId>com.springboot.module</groupId> 16 <artifactId>springboot-module-model</artifactId> 17 <version>1.0-SNAPSHOT</version> 18 </dependency> 19 <dependency> 20 <groupId>com.springboot.module</groupId> 21 <artifactId>springboot-module-api</artifactId> 22 <version>1.0-SNAPSHOT</version> 23 </dependency> 24 <dependency> 25 <groupId>ma.glasnost.orika</groupId> 26 <artifactId>orika-core</artifactId> 27 <version>1.5.1</version> 28 </dependency> 29 <dependency> 30 <groupId>org.apache.commons</groupId> 31 <artifactId>commons-lang3</artifactId> 32 <version>3.5</version> 33 </dependency> 34 </dependencies> 35 </project>
3.5 springboot-module-util.pom
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <parent> 6 <artifactId>springboot-module</artifactId> 7 <groupId>com.springboot.module</groupId> 8 <version>1.0-SNAPSHOT</version> 9 </parent> 10 <modelVersion>4.0.0</modelVersion> 11 <artifactId>springboot-module-util</artifactId> 12 13 <dependencies> 14 <dependency> 15 <groupId>org.springframework.boot</groupId> 16 <artifactId>spring-boot-starter-data-jpa</artifactId> 17 </dependency> 18 </dependencies> 19 </project>
3.6 springboot-module-web.pom
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <parent> 6 <artifactId>springboot-module</artifactId> 7 <groupId>com.springboot.module</groupId> 8 <version>1.0-SNAPSHOT</version> 9 </parent> 10 <modelVersion>4.0.0</modelVersion> 11 <artifactId>springboot-module-web</artifactId> 12 13 <dependencies> 14 <dependency> 15 <groupId>com.springboot.module</groupId> 16 <artifactId>springboot-module-service</artifactId> 17 <version>1.0-SNAPSHOT</version> 18 </dependency> 19 </dependencies> 20 </project>
4. 模块依赖关系图
5. 层说明
springboot-module-api ——业务逻辑接口
springboot-module-model ——实体类
springboot-module-service ——数据访问层,业务逻辑层的实现
springboot-module-util ——工具模块
springboot-module-web ——表现层
6. 包名设计
包名推荐以groupId为开头定义,所有模块的包名结构统一规范,比如groupId是com.springboot.module,而所有模块包名都以com.springboot.module开头,其中web层的WebApplication需要放在com.springboot.module下,不能放在com.springboot.module.web或者com.springboot.module.controller下。推荐使用下面的第一种情况。
6.1. 包名以groupId开头,注意WebApplication.java的位置,必须放在com.springboot.module包下,不能放在com.springboot.module.controller或者com.springboot.module.web包下
6.2. 包名以groupId开头,web层WebApplication.java放在了com.springboot.module.web下。
启动WebApplication.java会报如下错误
6.2.1. 解决方法:
1) 第一步:在WebApplication中加入
1 @ComponentScan(basePackages = {"com.springboot.module"})不是@ComponentScan(basePackages = {"com.springboot.module.*"})
1) 第二步,在springboot-module-service模块中添加ServiceApplication.java类,
6.2.2. 包名不以groupId开头,其他和第一种情况一样也是可以的。但最好推荐是以groupId为开头的
7.只有 web层有application.properties
1 server.port=8086 2 server.servlet-path=/ 3 spring.resources.static-locations=classpath:/static/,classpath:/templates/ 4 spring.mvc.view.suffix=.html 5 6 #配置数据源 7 spring.datasource.driver-class-name=com.mysql.jdbc.Driver 8 spring.datasource.url=jdbc:mysql://localhost:3306/springboot-module 9 spring.datasource.username=root 10 spring.datasource.password=root 11 spring.jpa.hibernate.ddl-auto=update 12 spring.jpa.show-sql=true 13 14 #在控制台输出彩色日志 15 spring.output.ansi.enabled=always
原文地址:https://www.cnblogs.com/jcjssl/p/9380309.html
时间: 2024-10-01 09:59:49