springboot maven父子项目搭建

15 篇文章 0 订阅
2 篇文章 0 订阅

此文设计内容:

一个project包含多个module
可以访问的url
mybatis
数据库连接池druid的使用
配置生产和测试环境的配置

背景

最近接触了一个新项目,已经接触了好几个月,但是一直忙于写代码,没有仔细分析过项目结构,今天开始分析下,包括搭建类似的项目,介绍应用到的一些新的技术点。

创建项目主要参考的博客是:https://www.cnblogs.com/orzlin/p/10330163.html

https://blog.csdn.net/ssllkkyyaa/article/details/80855133 -- 创建可以包含多个module的maven项目
https://blog.csdn.net/tiantangdizhibuxiang/article/details/81130297 -- 一个maven项目包含多个springboot项目
https://www.cnblogs.com/orzlin/p/9717399.html -- springboot + 多个maven项目

主要记录下创建过程中遇到的一些问题

1 编译错误,提示:Error:java: 错误: 不支持发行版本 5

1)在Intellij中点击“File” -->“Project Structure”,看一下“Project”和“Module”栏目中Java版本是否与本地一致;注意这里点击不同的module分别查看和确认。

2)点击“Settings”-->“Bulid, Execution,Deployment”-->“Java Compiler”,Target bytecode version设为本地Java版本。
参考:https://blog.csdn.net/qq_22076345/article/details/82392236 既可解决,主要是编译 和 执行的jdk版本存在一些问题,


2 执行mvn clean package ,报错如下:
 (repackage) on project beta-dao: Unable to find main clas

但是beta-dao模块原本就是作为工具模块,不需要启动,后来发现:
 parent 项目添加 spring-boot-maven-plugin 构建插件,而应该给终端项目使用,因为这个插件的 repackage 目标会处理 jar 包,导致依赖它的模块无法使用它
解决:移除父project中的spring-boot-maven-plugin依赖即可
参考:https://my.oschina.net/tridays/blog/825245

3 maven项目添加springboot依赖后仍然找不到对像
Error:(3, 32) java: 程序包org.springframework.boot不存在
https://www.cnblogs.com/Sarah-Strawberry/p/11081855.html  -- maven reimport

4 父子项目依赖理解
现在有父项目beta,里面有module:beta-web beta-biz beta-common beta-dao 
依赖关系是beta-web依赖于beta-biz beta-common和beta-dao,biz依赖beta-common、beta-dao

这里的依赖怎么理解?beta-web依赖于beta-biz,那么web的pom中就有如下配置:
        <dependency>
            <groupId>com.mz</groupId>
            <artifactId>beta-biz</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <scope>compile</scope>
        </dependency>
那么此时 
现在有父项目beta,里面有module:beta-web就会依赖beta-biz和其包含的所有依赖,如beta-common    

5 springboot项目启动后自动停止了,并不能访问服务接口

最后提示为:Process finished with exit code 0 springboot

可能是引入的jar包不对,错误引入:
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>5.2.9.RELEASE</version>
        </dependency>

正确引入:
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

6 配置mybatis
需要引入 pom,

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
</dependency>

application.properties中增加如下内容:
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://rm-ds.mysql.zhangbei.rds.aliyuncs.com/xescoding_test
spring.datasource.username = ads
spring.datasource.password = aaa
mybatis.mapper-locations = classpath:mybatis/*.xml
mybatis.type-aliases-package = com.mz.beta.dao.entity

7 配置druid数据库连接池
<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.9</version>
        </dependency>
此时如果 <version>1.1.9</version>红色 找不到包,执行项目 reimport即可
在配置applicaiton.yml


8 配置多个环境 dev test等
https://blog.csdn.net/qq_35139965/article/details/82255479
resource中写入多个文件 
application.yml 内容如下:
spring:
  profiles:
    active: dev # maven profile configuration
#      active: @spring.profiles.active.value@ # maven profile configuration

  application:
    name: web
  http:
    encoding:
      charset: UTF-8
      enabled: true
      force: true
    session:
      redis:
        namespace: codingcloud:session
  datasource:
    druid:
      url: jdbc:mysql://rm-8vb75855c40jn36lx6o.mysql.zhangbei.rds.aliyuncs.com/xescoding_test?${datasource.mysql.parameters}
      username: codingcloud
      password: 9y9egrgwt3R8V8Yx
      async-init: true
      initial-size: 200
      min-idle: 100
      max-active: 1000
      max-wait: 60000
#      stat-view-servlet:
#        login-username: codemonkey
#        login-password: hTSZT8n9XKLg
      time-between-eviction-runs-millis: 60000
      min-evictable-idle-time-millis: 30000
      test-while-idle: true
      pool-prepared-statements: true
      filters: stat,wall,log4j2
      connect-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=3000
      use-global-data-source-stat: true
      aop-patterns: com.tal.coding.platform.controller.*,com.tal.coding.platform.service.*,com.tal.coding.platform.mapper.*
      web-stat-filter:
        enabled: true
        url-pattern: /*
        exclusions: "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"
      stat-view-servlet:
        enabled: true
        reset-enable: false
        url-pattern: /druid/*

  servlet:
    multipart:
      enabled: true
      max-file-size: 1000MB
      max-request-size: 1000MB

server:
  servlet:
    context-path: /api/core
  undertow:
    # 以下的配置会影响buffer,这些buffer会用于服务器连接的IO操作,有点类似netty的池化内存管理
    # 每块buffer的空间大小,越小的空间被利用越充分
    buffer-size: 1024
    # 是否分配的直接内存
    direct-buffers: true

application-dev.yml

application-test.yml 实例如下:
spring:
  datasource:
    druid:
      url: jdbc:mysql://rm-8vb75855c40jn36lx6o.mysql.zhangbei.rds.aliyuncs.com/xescoding_test?${datasource.mysql.parameters}
      username: codingcloud
      password: 9y9egrgwt3R8V8Yx
      async-init: true
      initial-size: 200
      min-idle: 100
      max-active: 1000
      max-wait: 60000
      stat-view-servlet:
        login-username: codemonkey
        login-password: hTSZT8n9XKLg

pom增加修改
    <profiles>
        <profile>
            <id>dev</id>
            <properties>
                <!-- 环境标识,需要与配置文件的名称相对应 -->
                <activatedProperties>dev</activatedProperties>
            </properties>
        </profile>
        <profile>
            <id>test</id>
            <properties>
                <activatedProperties>test</activatedProperties>
            </properties>
            <activation>
                <!-- 默认环境 -->
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>prod</id>
            <properties>
                <activatedProperties>prod</activatedProperties>
            </properties>
        </profile>
    </profiles>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值