前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >搭建Springboot框架,并添加JPA和Gradle组件

搭建Springboot框架,并添加JPA和Gradle组件

作者头像
week
发布2018-08-24 15:00:40
6510
发布2018-08-24 15:00:40
举报
文章被收录于专栏:用户画像

开发工具: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

代码语言:javascript
复制
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包

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
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表新建成功

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017年10月15日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档