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
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 4.0.0
6
7 com.springboot.module
8 springboot-module
9 pom
10 1.0-SNAPSHOT
11
12 springboot-module-api
13 springboot-module-model
14 springboot-module-service
15 springboot-module-util
16 springboot-module-web
17
18
19
20
21 org.springframework.boot
22 spring-boot-starter-parent
23 1.4.1.RELEASE
24
25
26
27
28 org.springframework.boot
29 spring-boot-starter-web
30
31
32 org.projectlombok
33 lombok
34 1.16.18
35
36
37 org.springframework.boot
38 spring-boot-starter-logging
39
40
41 org.springframework.boot
42 spring-boot-starter-test
43
44
45
46
47
48
49 org.springframework.boot
50 spring-boot-maven-plugin
51
52
53
54 repackage
55
56
57
58
59 true
60
61
62
63
64
3.2 springboot-module-api.pom
1 <?xml version="1.0" encoding="UTF-8"?>
2
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
6 springboot-module
7 com.springboot.module
8 1.0-SNAPSHOT
9
10 4.0.0
11 springboot-module-api
12
13
14
15 com.springboot.module
16 springboot-module-util
17 1.0-SNAPSHOT
18 compile
19
20
21
3.3 springboot-module-model.pom
1 <?xml version="1.0" encoding="UTF-8"?>
2
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
6 springboot-module
7 com.springboot.module
8 1.0-SNAPSHOT
9
10 4.0.0
11 springboot-module-model
12
13
14
15 org.springframework.boot
16 spring-boot-starter-data-jpa
17
18
19 mysql
20 mysql-connector-java
21
22
23 com.springboot.module
24 springboot-module-util
25 1.0-SNAPSHOT
26 compile
27
28
29
30
3.4 springboot-module-service.pom
1 <?xml version="1.0" encoding="UTF-8"?>
2
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
6 springboot-module
7 com.springboot.module
8 1.0-SNAPSHOT
9
10 4.0.0
11 springboot-module-service
12
13
14
15 com.springboot.module
16 springboot-module-model
17 1.0-SNAPSHOT
18
19
20 com.springboot.module
21 springboot-module-api
22 1.0-SNAPSHOT
23
24
25 ma.glasnost.orika
26 orika-core
27 1.5.1
28
29
30 org.apache.commons
31 commons-lang3
32 3.5
33
34
35
3.5 springboot-module-util.pom
1 <?xml version="1.0" encoding="UTF-8"?>
2
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
6 springboot-module
7 com.springboot.module
8 1.0-SNAPSHOT
9
10 4.0.0
11 springboot-module-util
12
13
14
15 org.springframework.boot
16 spring-boot-starter-data-jpa
17
18
19
3.6 springboot-module-web.pom
1 <?xml version="1.0" encoding="UTF-8"?>
2
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
6 springboot-module
7 com.springboot.module
8 1.0-SNAPSHOT
9
10 4.0.0
11 springboot-module-web
12
13
14
15 com.springboot.module
16 springboot-module-service
17 1.0-SNAPSHOT
18
19
20
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=80862 server.servlet-path=/3 spring.resources.static-locations=classpath:/static/,classpath:/templates/4 spring.mvc.view.suffix=.html5
6 #配置数据源7 spring.datasource.driver-class-name=com.mysql.jdbc.Driver8 spring.datasource.url=jdbc:mysql://localhost:3306/springboot-module9 spring.datasource.username=root10 spring.datasource.password=root11 spring.jpa.hibernate.ddl-auto=update12 spring.jpa.show-sql=true13
14 #在控制台输出彩色日志15 spring.output.ansi.enabled=always