本文共 10136 字,大约阅读时间需要 33 分钟。
1.父pom.xml 打包方式,jar要更改为pom,build 需要更改
2.不需要打包的模块pom.xml文件中不要写<build>,全删掉,例如有些工程中的common模块,domain模块,dao模块,service模 块都不需要打包
3.在子模块中需要声明父工程,填写父工程位置<relativePath>../pom.xml</relativePath>
4.关于applicatin.properties配置文件,只需要在启动的模块中配置就可以了,
5.关于打包为什么打包jar包,不打war包,打war包目的是war包可以运行在tomcat下,但是SpringBoot是内置tomcat,如果你打war包,前提是干掉内置的tomcat,然后才能打包,各种麻烦,直接打包可执行jar包,使用java -jar 命令就可以完美的运行起来很方便!
6.对象之间的调用是使用@Autowired 注解 来实现注入。多模块搭建,首先肯定得有一个父工程,还有若干个子模块,子模块可以理解为,可以继承父模块
整个工程你就当作一个公司,父工程(退休了什么也不干)只需要声明有几个儿子(子模块)就完事了,
假设让子模块api作为程序启动模块,子模块api声明父工程是谁,就当他是大儿子,公司他管事,pom.xml文件需要打包,需要build配置,需要其它子模块协助。
其它子模块声明父工程是谁,之间关系都是兄弟,不需要打包,哪里需要去哪里!
1.首先创建一个父模块:huadian-multil-model
2.将工程中的src进行删除
1.创建api,domain,common,dao,service等模块
选中父工程,右键 new module,如下图:
2.最后创建的工程如下:
父模块: huadian-multil-model
子模块: huadian-api (页面交互接收、传递数据,唯一有启动类的模块)
huadian-common 一些常用工具类
huadian-domain (实体类)
huadian-services (处理业务逻辑)
huadian-dao (用于持久化数据跟数据库交互)
调用关系: huadian-api 依赖 huadian-common、huadian-domain、 huadian-services、huadian-dao
huadian-services 依赖 huadian-common、huadian-domain、 huadian-dao
huadian-dao 依赖 huadian-common、huadian-domain
huadian-common 依赖 huadian-domain
4.0.0 huadian-api huadian-common huadain-service huadian-domain huadian-dao org.springframework.boot spring-boot-starter-parent 2.1.3.RELEASE com.huadian huadian-multil-model pom 1.0-SNAPSHOT huadian-multil-model http://www.example.com UTF-8 1.8 1.8 junit junit 4.11 test org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-test test public aliyun nexus http://maven.aliyun.com/nexus/content/groups/public/ true org.apache.maven.plugins maven-compiler-plugin 3.1 org.apache.maven.plugins maven-surefire-plugin 2.19.1 true
4.0.0 com.huadian huadian-multil-model 1.0-SNAPSHOT ../pom.xml com.huadian huadian-api 1.0-SNAPSHOT huadian-api http://www.example.com UTF-8 1.8 1.8 junit junit 4.11 test org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-test test com.huadian huadian-domain 1.0-SNAPSHOT com.huadian huadian-common 1.0-SNAPSHOT com.huadian huadain-service 1.0-SNAPSHOT com.huadian huadian-dao 1.0-SNAPSHOT org.springframework.boot spring-boot-maven-plugin com.huadian.App ZIP repackage
4.0.0 com.huadian huadian-multil-model 1.0-SNAPSHOT ../pom.xml com.huadian huadain-service 1.0-SNAPSHOT huadain-service http://www.example.com UTF-8 1.8 1.8 junit junit 4.11 test org.springframework.boot spring-boot-starter-data-redis org.springframework.boot spring-boot-starter-web org.mybatis.spring.boot mybatis-spring-boot-starter 1.3.2 mysql mysql-connector-java runtime com.huadian huadian-domain 1.0-SNAPSHOT com.huadian huadian-common 1.0-SNAPSHOT com.huadian huadian-dao 1.0-SNAPSHOT
4.0.0 com.huadian huadian-multil-model 1.0-SNAPSHOT ../pom.xml com.huadian huadian-dao 1.0-SNAPSHOT huadian-dao http://www.example.com UTF-8 1.8 1.8 junit junit 4.11 test com.huadian huadian-domain 1.0-SNAPSHOT
4.0.0 com.huadian huadian-multil-model 1.0-SNAPSHOT ../pom.xml com.huadian huadian-common jar 1.0-SNAPSHOT huadian-common http://www.example.com UTF-8 1.8 1.8 junit junit 4.11 test org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-test test com.huadian huadian-domain 1.0-SNAPSHOT
4.0.0 huadian-api huadian-common huadain-service huadian-domain huadian-dao org.springframework.boot spring-boot-starter-parent 2.1.3.RELEASE com.huadian huadian-multil-model pom 1.0-SNAPSHOT huadian-multil-model http://www.example.com UTF-8 1.8 1.8 junit junit 4.11 test org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-test test public aliyun nexus http://maven.aliyun.com/nexus/content/groups/public/ true org.apache.maven.plugins maven-compiler-plugin 3.1 org.apache.maven.plugins maven-surefire-plugin 2.19.1 true
1.不能在api模块package,需要在父模块下,一起打包,如下图
2. 这个时候api模块已经打好包,进入target下执行java -jar
参考:
源代码见github
转载地址:http://satgi.baihongyu.com/