搭建Springboot框架并添加JPA和Gradle組件的方法
開發(fā)工具:Intellij IDEA
所需開發(fā)環(huán)境:JDK Gradle
一、新建springboot項目
1.New Project

2. spring initializr

3. 填寫項目組織
group : 項目屬于哪個組,這個組往往和項目所在的組織或公司存在關(guān)聯(lián)
artifact : 當(dāng)前項目在組中唯一的ID
Type : jar包管理所使用的工具
Lauguage : 開發(fā)語言
packageing : 打包方式
Java Version : JDK 的版本號
version :項目當(dāng)前的版本號

4.選擇所需要添加的組件


5. 選擇項目的保存位置

二、目標代碼組織

1. 配置數(shù)據(jù)庫
resource目錄下的application.properties
spring.jpa.hibernate.ddl-auto=create-drop spring.datasource.url=jdbc:mysql://localhost:3306/test spring.datasource.username=root spring.datasource.password=cueb
2. 修改build.gradle文件
將34行的providedRuntime修改為compile,否者項目無法正常啟動
providedRuntime :在運行時提供Tomcat Jar包
compile :在編譯時提供Tomcat jar包
buildscript {
ext {
springBootVersion = '1.5.7.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse-wtp'
apply plugin: 'org.springframework.boot'
apply plugin: 'war'
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
configurations {
providedRuntime
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile('org.springframework.boot:spring-boot-starter-web')
runtime('mysql:mysql-connector-java')
compile('org.springframework.boot:spring-boot-starter-tomcat')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
3. 新建controller
package com.example.demo.control;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@RequestMapping(value = "")
public String test(){
return "hello cueb";
}
}
4. 新建model
package com.example.demo.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class User {
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
三、部署運行
1. debug 啟動

2. 數(shù)據(jù)庫user表新建成功

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
java javax.annotation.Resource注解的詳解
這篇文章主要介紹了javax.annotation.Resource注解的詳解的相關(guān)資料,需要的朋友可以參考下2016-10-10
MyBatisPlus利用Service實現(xiàn)獲取數(shù)據(jù)列表
這篇文章主要為大家詳細介紹了怎樣使用 IServer 提供的 list 方法查詢多條數(shù)據(jù),這些方法將根據(jù)查詢條件獲取多條數(shù)據(jù),感興趣的可以了解一下2022-06-06
詳解Java Project項目打包成jar,并生成exe文件
本篇文章主要介紹了Java Project項目打包成jar,并生成exe文件,非常具有實用價值,有興趣的可以了解一下。2017-01-01
Java?Ribbon與openfeign區(qū)別和用法講解
Ribbon是基于Netflix?Ribbon實現(xiàn)的一套客戶端負載均衡的工具,主要功能是提供客戶端的軟件負載均衡算法和服務(wù)調(diào)用。openfeign對Feign進行了增強,使其支持Spring MVC注解,另外還整合了Ribbon和Nacos,從而使得Feign的使用更加方便2022-08-08
結(jié)合線程池實現(xiàn)apache?kafka消費者組的誤區(qū)及解決方法
這篇文章主要介紹了結(jié)合線程池實現(xiàn)apache?kafka消費者組的誤區(qū)及解決方法,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-07-07

