开发工具: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
查看更多关于搭建Springboot框架并添加JPA和Gradle组件的方法的详细内容...