Fork me on Gitee

Springboot多模块项目搭建

新建项目,作为父模块

image

删除除pom.xml之外的所有文件

效果如下:

image

.gitignore和ReadMe.md是可选文件

创建子模块

工程上右键 -> new -> model -> 选择Spring Initializer -> 下一步

image

依此步骤分别建立 web、service、entity、dao,删除每个模块中无用的文件,只留下pom.xml

删除除web模块外其他模块的Application启动类和resources目录下的application.properties配置文件

image

各个模块建立关系

父POM配置

1. packaging改为pom
2. 声明子模块
3. properties/denpendencies下的配置都为公共配置
4. build模块是打包用的,这里声明了所有模块都跳过单元测试

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.6</version>
        <relativePath/>
    </parent>

    <!--父POM-->
    <groupId>com.xxx</groupId>
    <artifactId>myblog</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>myblog</name>
    <packaging>pom</packaging>

    <!--声明子模块-->
    <modules>
        <module>entity</module>
        <module>dao</module>
        <module>service</module>
        <module>web</module>
    </modules>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>


    <dependencies>
        <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>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <!--maven-surefire-plugin是执行测试用例的插件,这里声明跳过TEST-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <skip>true</skip>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

Entity的POM配置

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <!--声明父模块-->
    <parent>
        <groupId>com.xxx</groupId>
        <artifactId>myblog</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <!--默认就是 ../pom.xml-->
        <relativePath>../pom.xml</relativePath>
    </parent>

    <!--当前模块-->
    <groupId>com.xxx</groupId>
    <artifactId>entity</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>entity</name>
    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>
</project>

Dao的POM配置

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <!--声明父模块-->
    <parent>
        <groupId>com.xxx</groupId>
        <artifactId>myblog</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>

    <!--当前模块-->
    <groupId>com.xxx</groupId>
    <artifactId>dao</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>dao</name>
    <packaging>jar</packaging>

    <!--依赖-->
    <dependencies>
        <!--数据库连接-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

        <!--Mybatis-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.2.0</version>
        </dependency>

        <!--引入entity模块-->
        <dependency>
            <groupId>com.xxx</groupId>
            <artifactId>entity</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

Service的POM配置

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <!--声明父模块-->
    <parent>
        <groupId>com.xxx</groupId>
        <artifactId>myblog</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>

    <!--当前模块-->
    <groupId>com.xxx</groupId>
    <artifactId>service</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>service</name>
    <packaging>jar</packaging>

    <!--依赖-->
    <dependencies>
        <!--引入dao模块-->
        <dependency>
            <groupId>com.xxx</groupId>
            <artifactId>dao</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

        <!--引入entity模块-->
        <dependency>
            <groupId>com.xxx</groupId>
            <artifactId>entity</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

Web的POM配置

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <!--声明父模块-->
    <parent>
        <groupId>com.xxx</groupId>
        <artifactId>myblog</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>

    <!--当前模块-->
    <groupId>com.xxx</groupId>
    <artifactId>web</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>web</name>
    <packaging>jar</packaging>

    <!--依赖-->
    <dependencies>
        <!--Web-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--引入service模块-->
        <dependency>
            <groupId>com.xxx</groupId>
            <artifactId>service</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

        <!--引入entity模块-->
        <dependency>
            <groupId>com.xxx</groupId>
            <artifactId>entity</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

    </dependencies>

    <!--编译打包相关-->
    <build>
        <plugins>
            <!--SpringBoot项目必须加入这个插件,才会将普通JAR打成SpringBoot的JAR-->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <!--指定Main Class为全局的唯一入口-->
                    <mainClass>com.xxx.blog.WebApplication</mainClass>
                </configuration>
            </plugin>

        </plugins>
    </build>
</project>

代码

Entity

package com.xxx.blog;

import lombok.Data;

import java.util.Date;

/**
 * @author computer
 */
@Data
public class UmsMenu {
    private int id;

    private String title;

    private int level;

    private Date createTime;

    private int parentId;
}

Mapper

package com.xxx.blog;

import org.apache.ibatis.annotations.Param;

import java.util.List;

/**
 * @author computer
 */
public interface UmsMenuMapper {
    /**
     * 根据ID查找
     * @param id
     * @return
     */
    /*@Select("select * from ums_menu where id = #{id}")
    @Results({
            @Result(property = "createTime",column = "create_time",javaType = Date.class),
            @Result(property = "parentId",column = "parent_id",javaType = int.class)
    })*/
    UmsMenu findById(@Param("id") int id);

    /**
     * 查找所有
     * @return
     */
    /*@Select("select * from ums_menu")
    @Results({
            @Result(property = "createTime",column = "create_time",javaType = Date.class),
            @Result(property = "parentId",column = "parent_id",javaType = int.class)
    })*/
    List<UmsMenu> findAll();
}

Mapper.xml

<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xxx.blog.UmsMenuMapper">
    <resultMap id="resultMap" type="com.xxx.blog.UmsMenu">
        <result column="id" property="id" javaType="java.lang.Integer"/>
        <result column="title" property="title" javaType="java.lang.String"/>
        <result column="level" property="level" javaType="java.lang.Integer"/>
        <result column="create_time" property="createTime" javaType="java.util.Date"/>
        <result column="parent_id" property="parentId" javaType="java.lang.Integer"/>
    </resultMap>

    <select id="findById" parameterType="java.lang.Integer" resultMap="resultMap">
        select * from ums_menu where id = #{id}
    </select>

    <select id="findAll" resultMap="resultMap">
        select * from ums_menu
    </select>
</mapper>

Service

package com.xxx.blog;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author computer
 */
@Service
public class WebService {

    @Autowired
    UmsMenuMapper mapper;

    /**
     * 获取所有Menu信息
     * @return
     */
    public List<UmsMenu> getAllMenu(){
        return mapper.findAll();
    }
}

Controller

package com.xxx.blog.controller;

import com.xxx.blog.WebService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author computer
 */
@RestController
@RequestMapping("/test")
public class WebTest {

    @Autowired
    WebService webService;

    /**
     * 获取所有Menu信息
     * @return
     */
    @RequestMapping(value = "/getAllMenu",produces = "application/json")
    public String getAllMenu(){
        return webService.getAllMenu().toString();
    }
}

验证

效果
image

注意事项

1.父pom.xml 打包方式,jar要更改为pom,build 需要更改

2.不需要打包的模块pom.xml文件中不要写<build>,全删掉,例如有些工程中的common模块,utils模块,entity模块,service模  块都不需要打包

3.声明父工程时,填写父工程位置<relativePath>../pom.xml</relativePath>

4.关于applicatin.properties配置文件,只需要在启动的模块中配置就可以了,

5.关于打包为什么打包jar包,不打war包,打war包目的是war包可以运行在tomcat下,但是SpringBoot是内置tomcat,如果你打war包,前提是干掉内置的tomcat,然后才能打包,各种麻烦,直接打包可执行jar包,使用java -jar 命令就可以完美的运行起来很方便!
————————————————
版权声明:本文为CSDN博主「凌云冷海」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/baidu_41885330/article/details/81875395

参考资料

SpringBoot+Maven多模块项目(创建、依赖、打包可执行jar包部署测试)完整流程

posted @ 2021-11-21 21:17  江南西道  阅读(1668)  评论(0编辑  收藏  举报
Fork me on GitHub