美文网首页
构建Springboot聚合工程

构建Springboot聚合工程

作者: 听城 | 来源:发表于2020-01-19 21:51 被阅读0次

    构建父工程

    • 打开Pycharm,新建工程,选择Maven
      创建工程
    • 填写GroupID和ArtifactID
      groupId :the unique identifier of the organization or group that created the project
      artifactId :unique base name of the primary artifact being generated by this project
      GroupID 是项目组织唯一的标识符,实际对应JAVA的包的结构,是main目录里java的目录结构。
      ArtifactID是项目的唯一的标识符,实际对应项目的名称,就是项目根目录的名称。
    • 创建项目,完善pom文件
      工程目录
      在pom文件中增加<packaging>pom</packaging>

    构建其它模块

    • 分别构建api、common、mapper、pojo、service模块


      构建子模块

      同样选择Maven,其余步骤与创建父模块相同


      image.png
    • 各个模块依赖关系
      pojo->common
      mapper->pojo
      service->mapper
      api->service
      注意在各个pom中添加相关依赖

    添加项目依赖

    在父模块的pom文件中添加项目所需依赖

        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.1.5.RELEASE</version>
            <relativePath />
        </parent>
    
        <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>
                <exclusions>
                    <exclusion>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-starter-logging</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-aop</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-configuration-processor</artifactId>
                <optional>true</optional>
            </dependency>
            <!-- mysql驱动 -->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.41</version>
            </dependency>
            <!-- mybatis -->
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>2.1.0</version>
            </dependency>
            <!-- 通用mapper逆向工具 -->
            <dependency>
                <groupId>tk.mybatis</groupId>
                <artifactId>mapper-spring-boot-starter</artifactId>
                <version>2.1.5</version>
            </dependency>
            <!--pagehelper -->
            <dependency>
                <groupId>com.github.pagehelper</groupId>
                <artifactId>pagehelper-spring-boot-starter</artifactId>
                <version>1.2.12</version>
            </dependency>
    
            <!-- apache 工具类 -->
            <dependency>
                <groupId>commons-codec</groupId>
                <artifactId>commons-codec</artifactId>
                <version>1.11</version>
            </dependency>
            <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-lang3</artifactId>
                <version>3.4</version>
            </dependency>
            <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-io</artifactId>
                <version>1.3.2</version>
            </dependency>
    
            <!-- swagger2 配置 -->
            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger2</artifactId>
                <version>2.4.0</version>
            </dependency>
            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger-ui</artifactId>
                <version>2.4.0</version>
            </dependency>
            <dependency>
                <groupId>com.github.xiaoymin</groupId>
                <artifactId>swagger-bootstrap-ui</artifactId>
                <version>1.6</version>
            </dependency>
    
            <!--引入日志依赖 抽象层 与 实现层-->
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
                <version>1.7.21</version>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
                <version>1.7.21</version>
            </dependency>
    
            <!-- 打包war [3] 添加依赖 -->
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>javax.servlet-api</artifactId>
                <scope>provided</scope>
            </dependency>
        </dependencies>
    

    Install模块

    image.png

    Install成功在控制台会报build success

    试运行

    • 在api模块Java目录下com.jc(你自己的groupId)下创建Application文件
    package com.imooc;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.context.annotation.ComponentScan;
    import tk.mybatis.spring.annotation.MapperScan;
    
    @SpringBootApplication
    // 扫描 mybatis 通用 mapper 所在的包
    //@MapperScan(basePackages = "com.jc.mapper")
    // 扫描所有包以及相关组件包
    //@ComponentScan(basePackages = {"com.jc", "org.n3r.idworker"})
    public class Application {
        public static void main(String [] args){
            SpringApplication.run(Application.class, args);
        }
    }
    
    
    • 在api模块创建controller包,创建IndexController文件
    package com.jc.controller;
    
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class IndexController {
        @GetMapping("/hello")
        public Object hello(){
            return "hello world";
        }
    }
    
    
    • 在api模块resource文件夹下创建application.yml
    ############################################################
    #
    # web访问端口号  约定:8088
    #
    ############################################################
    server:
      port: 8088
      tomcat:
        uri-encoding: UTF-8
      max-http-header-size: 80KB
    ############################################################
    #
    # 配置数据源信息
    #
    ############################################################
    spring:
      datasource: # 数据源的相关配置
        type: com.zaxxer.hikari.HikariDataSource # 数据源类型:HikariCP
        driver-class-name: com.mysql.jdbc.Driver # mysql驱动
        url: jdbc:mysql://localhost:3306/foodie-shop-dev?useUnicode=true&characterEncoding=UTF-8&autoReconnect
        username: root
        password: root
        hikari:
          connection-timeout: 30000 # 等待连接池分配连接的最大时长(毫秒),超过这个时长还没可用的连接则发生SQ
          minimum-idle: 5 # 最小连接数
          maximum-pool-size: 20 # 最大连接数
          auto-commit: true # 自动提交
          idle-timeout: 600000 # 连接超时的最大时长(毫秒),超时则被释放(retired),默认:10分钟
          pool-name: DateSourceHikariCP # 连接池名字
          max-lifetime: 1800000 # 连接的生命时长(毫秒),超时而且没被使用则被释放(retired),默认:30分钟
          connection-test-query: SELECT 1
    
    

    运行application.java文件,访问http://localhost:8088/hello,即可看到结果

    本文GitHub地址https://github.com/zhangpu1211/JavaCommon

    相关文章

      网友评论

          本文标题:构建Springboot聚合工程

          本文链接:https://www.haomeiwen.com/subject/mnhdzctx.html