1,建父工程test_demo:删除父工程下的src文件
然后next -> finish
(第一步也可以用spring initialize来代替maven创建,即直接创建spring initialize,然后不勾选任何模块,一直next,创建成功后,除了pom.xml,其他都删除)
2,分别建子工程entity,service,dao,web
右击父工程,如图
点击Spring Initialize --> next -->(或者选择maven,将会创建子模块,不过需要手动添加springboot依赖)
先建web(group属性必须跟父模块完全一致,如com.test)
然后next
然后next–>finish
(通过Springboot初始化不能直接成为子模块,需要更改pom.xml,跟上面括号里面直接在父工程中new-》module-》maven,得到的将直接是子模块,但不会自动导入Springboot依赖)将demo_web 里 pom.xml 的jar包依赖 剪切到 父工程的pom.xml中,因为后续web模块会继承父模块的pom(也可以只剪切parent,再在web模块pom.xml中新建一个parent,把groupId和artifactId,version改成父工程对应的,并指定父模块的pom.xml位置(一般为…/pom.xml)成为父工程的子模块,这种通过spring initialize方法创建的module自带packaging打包方式,需要删除以及删除打包插件build里面的plugin,因为继承了父工程,打包方式为pom)
demo_web就建完了
之后建dao,service,entity
到这儿之后一直next->next->finish期间任何选项都不选
service,dao都如此,其中都将pom.xml中的 jar包依赖 删掉,jar包依赖 只需在 父工程的pom.xml中有就可以
3,打开父工程的pom.xml 添加
<packaging>pom</packaging>
并添加其子工程:
并添加mapper位置(以防后边找不到mapper.xml)
<!-- mapper 配置位置 -->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
4,分别在dao,service,entity,web中加入父工程依赖:如下图
5,分别加入依赖:dao层依赖entity(如下图); service依赖dao,entity; web依赖service,entity;entity不需要
6,在demo_web中测试搭建是否成功
在demo_web的com.test.demo_web包结构下创建一个包controller(如图)
7,添加application.properties(配置数据源,.xml文件,配置jsp)
spring.datasource.url=jdbc:mysql://localhost:3306/test1
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
mybatis.mapper-locations = classpath:mapper/*.xml
spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp
把子模块中的packaging删除,只保留父模块pom中的packaging,子模块最好只保留父模块和依赖模块
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.imooc</groupId>
<artifactId>imooc-videos-dev</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>imooc-videos-dev-mini-api</artifactId>
<dependencies>
<dependency>
<groupId>com.imooc</groupId>
<artifactId>imooc-videos-dev-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
8,在SpringBoot的入口加上exclude= {DataSourceAutoConfiguration.class}(测试时)
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class DemoWebApplication {
public static void main(String[] args) {
SpringApplication.run(DemoWebApplication.class, args);
}
}
测试结果:
以上就是使用springboot搭建简单的聚合项目