idea
file->new->project
选择spring initalizr 然后确认下jdk版本后,直接next
编写group,artifact信息,别的不需要动,注意,最下方的pakage和group一致即可,不需要加上artifact
然后一路next
然后编辑一下setting的mvn和jdk'的版本,然后删除当前项目的一些东西,项目文件夹里
只剩下.idea,.gitignor,demo.iml,pom.xml即可
然后右键项目,new->module,选择maven,确认一下jdk版本,然后next,然后填写artifactId,最好是父工程跟中画线加子工程名
然后next确认下name字段,修改为中画线,然后finish,其他的子项目都这样操作即可,比如service层,dao层,common组件项目等
(这是一个聚合项目的分层构建,下文的web项目指的是将web层抽出来单独部署为一个项目)
创建完父项目和子项目后,父项目中,使用<dependencyManagement>标签来管理公用的依赖,这样所有的子项目都会继承依赖,自己也可以重新定义版本来覆盖,方便管理,同时可以在properties标签中声明所有的依赖版本号,如下
<properties>
<java.version>1.8</java.version>
<ooxx.version>0.0.1-SNAPSHOT</ooxx.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.example.ooxx</groupId>
<artifactId>ooxx-biz</artifactId>
<version>${ooxx.version}</version>
</dependency>
<dependency>
<groupId>com.example.ooxx</groupId>
<artifactId>ooxx-dao</artifactId>
<version>${ooxx.version}</version>
</dependency>
<dependency>
<groupId>com.example.ooxx</groupId>
<artifactId>ooxx-web</artifactId>
<version>${ooxx.version}</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>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--引入mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>
</dependencyManagement>
然后对于子项目,也需要添加互相之间的依赖,比如web项目依赖biz项目 ,那么web项目的pom需要加依赖
<dependency>
<groupId>com.example.ooxx</groupId>
<artifactId>ooxx-biz</artifactId>
<version>${ooxx.version}</version>
</dependency>
同理,biz依赖dao项目
那么biz项目的pom加依赖
<dependency>
<groupId>com.example.ooxx</groupId>
<artifactId>ooxx-dao</artifactId>
<version>${ooxx.version}</version>
</dependency>
这样就完成了相互依赖的东西
然后基本的结构就算完成了,接下来就可以根据各个项目的功能来实现
比如dao项目,需要跟数据库交互,那么就可以在Java路径下建个包,注意包名先要和group一致,然后根据项目自定义一个包,再resource下新建mapper文件夹,generator文件夹等,这样这个项目的entity,mapper,xml等都能先构建好
同理,在biz项目,就一样的可以将service的东西放在这里
然后注意,在web项目中的主启动类上,一定要扫描对应的mapper和对应的package
@MapperScan("com.example.ooxx.dao.mapper")
@SpringBootApplication(scanBasePackages = "com.example.ooxx")
public class OoxxApplication {
public static void main(String[] args) {
SpringApplication.run(OoxxApplication.class, args);
}
}
同时,web项目的配置application.yml文件中,也要配置mybatis的path等
spring:
datasource:
driverClassName: com.mysql.jdbc.Driver
url:
username:
password:
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.example.ooxx.dao.entity
然后启动,就可以完成最基本的crud了
项目打包,在web项目的pom中引入maven打包插件,
<!--打包项目时候,主启动类所在的项目需要maven插件-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
原因是因为这个项目才有主启动类,如果打包插件放在父工程的pom中,那么所有子工程都会继承,会报错,找不到main class,然后还需要对所有依赖的项目设置package标签为jar,打jar包,使得主启动类所在的工程能使用依赖工程的jar
<packaging>jar</packaging>