美文网首页
IDEA+Gradle+SpringBoot+Mybatis-p

IDEA+Gradle+SpringBoot+Mybatis-p

作者: crossyf | 来源:发表于2019-11-19 10:57 被阅读0次

最新学习了Gradle构建工具,于是尝试使用Gradle搭建一个最近很流行的框架,SpringBoot+Mybatis-plus框架,话不多说,直接看过程。

首先创建一个Gradle父工程。

image.png
image.png image.png

点击next,然后选择项目路径,点击finish,创建完成,等待gradle下载相关东西,第一次可能需要一点时间。

等待完成后,删除src文件,因为这里要把他作为父工程,所以不需要。
然后新建module

image.png
image.png

然后一直next 直到最后gradle项目配置,和之前一样,选择本地gradle安装路径

image.png

然后再创建gradle-commongradle-web模块,最后项目的结构如下

image.png

接着配置gradle, 首先在父工程的setting.gradle下引用子模块

image.png

父工程下的build.gradle配置如下

plugins {
    id 'java'
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'

}

allprojects {
    apply plugin: 'java'
    apply plugin: 'idea'
    group 'com.crossyf.gradle'
    version '1.0-SNAPSHOT'
    sourceCompatibility = 1.8

    repositories {
        mavenCentral()
    }
    [compileJava,compileTestJava,javadoc]*.options*.encoding = 'UTF-8'

}

subprojects {
    ext {
        springBootVersion = "2.2.1"
    }
}

project(':gradle-common') {
    dependencies {
        compile(
                'cn.hutool:hutool-all:5.0.5'
        )
    }

}


project(':gradle-service') {
    dependencies {
        compile project(":gradle-common")
        compile(
                'org.springframework.boot:spring-boot-starter-web',
                'com.baomidou:mybatis-plus-boot-starter:3.2.0',
                'org.projectlombok:lombok:1.18.0',
                'com.alibaba:druid:1.1.10',
                'mysql:mysql-connector-java:8.0.11'
        )
    }

}

project(':gradle-web') {
    apply plugin: "war"
    dependencies {
        compile project(":gradle-service")
        compile(
                'org.springframework.boot:spring-boot-starter-web',
                'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.0.1'
        )
        providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
    }
    processResources {
        /* 从$projectDir/src/main/java 目录下复制文件到web-inf/classes目录下覆盖原有同名文件*/
        from("$projectDir/src/main/java")
    }
}

还需要在子项目的 build.gradle分别配置一下
common

plugins {
    id 'org.springframework.boot' version '2.2.1.RELEASE'
    id 'io.spring.dependency-management' version '1.0.8.RELEASE'
    id 'java'
}

group = 'com.crossyf.gradle'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
}

test {
    useJUnitPlatform()
}

service

plugins {
    id 'org.springframework.boot' version '2.2.1.RELEASE'
    id 'io.spring.dependency-management' version '1.0.8.RELEASE'
    id 'java'
}
jar.enabled = true
bootJar.enabled = false

group = 'com.crossyf.gradle'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
}

test {
    useJUnitPlatform()
}

web

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:2.2.1.RELEASE")
    }
}
plugins {
    id 'org.springframework.boot' version '2.2.1.RELEASE'
    id 'io.spring.dependency-management' version '1.0.8.RELEASE'
    id 'java'
}


apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'war'

war {
    enabled = true
}

group = 'com.crossyf.gradle'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
sourceSets.main.resources.srcDirs = ["src/main/resources"]

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
}

test {
    useJUnitPlatform()
}

到这里,要引用的包和配置已经差不多了,要想跑通项目,还需要创建application.yml文件,配置数据库,生成daoservice,创建controller并且启动项目,这里就不详细介绍了,具体的项目我放在我的github上了。

相关文章

网友评论

      本文标题:IDEA+Gradle+SpringBoot+Mybatis-p

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