開發工具:intellij idea
所需開發環境:jdk gradle
一、新建springboot項目
1.new project
2. spring initializr
3. 填寫項目組織
group : 項目屬于哪個組,這個組往往和項目所在的組織或公司存在關聯
artifact : 當前項目在組中唯一的id
type : jar包管理所使用的工具
lauguage : 開發語言
packageing : 打包方式
java version : jdk 的版本號
version :項目當前的版本號
4.選擇所需要添加的組件
5. 選擇項目的保存位置
二、目標代碼組織
1. 配置數據庫
resource目錄下的application.properties
1
2
3
4
|
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包
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
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. 數據庫user表新建成功
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/jxq0816/article/details/78240555