springboot 总模块 搭项目_springboot基于maven多模块项目搭建(直接启动webApplication)...

1. 新建maven项目springboot-module

ac38d29c0957785f8aa7d8cb7b8ecaab.png

2.把src删掉,新建module项目

springboot-module-api

springboot-module-model

springboot-module-service

springboot-module-util

springboot-module-web

1739943a949d9a253b2e1b304a397eb2.png

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.  模块依赖关系图

560d971f72c7a004b848b56483889f0a.png

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包下

bd17b358111019359c995af77bcc4829.png

2ccf06d2812efcb551ac9e30f8588104.png

3e24c6a441c7ed0126ccdc4168eb5941.png

7695ed44bf6f724d27c31179d291d1a4.png

6.2. 包名以groupId开头,web层WebApplication.java放在了com.springboot.module.web下。

8fd46423bd22899a7a5a399dec792f78.png

07555df3fd15888ec48d8a44baa920c7.png

2c9da48d62ec703ea32fe103b0cb2d26.png

启动WebApplication.java会报如下错误

d8ede4621eb17152eeafb1b265e4971a.png

6.2.1. 解决方法:

1)     第一步:在WebApplication中加入

1 @ComponentScan(basePackages = {"com.springboot.module"})不是@ComponentScan(basePackages = {"com.springboot.module.*"})

af55eef39e6fe2624813c9aabc2a8c75.png

1)     第二步,在springboot-module-service模块中添加ServiceApplication.java类,

5d83dc57d9da311ce3ecf41a20cc61d8.png

917f1723ba28b0c9c756211404f94a34.png

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值