Spring Boot与Spring Data JPA+gradle框架整理

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

[plain]  view plain  copy
  1. spring.jpa.hibernate.ddl-auto=create-drop  
  2. spring.datasource.url=jdbc:mysql://localhost:3306/test  
  3. spring.datasource.username=root  
  4. spring.datasource.password=cueb  

2. 修改build.gradle文件

将34行的providedRuntime修改为compile,否者项目无法正常启动

providedRuntime :在运行时提供Tomcat Jar包

compile :在编译时提供Tomcat jar包

[plain]  view plain  copy
  1. buildscript {  
  2.     ext {  
  3.         springBootVersion = '1.5.7.RELEASE'  
  4.     }  
  5.     repositories {  
  6.         mavenCentral()  
  7.     }  
  8.     dependencies {  
  9.         classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")  
  10.     }  
  11. }  
  12.   
  13. apply plugin: 'java'  
  14. apply plugin: 'eclipse-wtp'  
  15. apply plugin: 'org.springframework.boot'  
  16. apply plugin: 'war'  
  17.   
  18. group = 'com.example'  
  19. version = '0.0.1-SNAPSHOT'  
  20. sourceCompatibility = 1.8  
  21.   
  22. repositories {  
  23.     mavenCentral()  
  24. }  
  25.   
  26. configurations {  
  27.     providedRuntime  
  28. }  
  29.   
  30. dependencies {  
  31.     compile('org.springframework.boot:spring-boot-starter-data-jpa')  
  32.     compile('org.springframework.boot:spring-boot-starter-web')  
  33.     runtime('mysql:mysql-connector-java')  
  34.     compile('org.springframework.boot:spring-boot-starter-tomcat')  
  35.     testCompile('org.springframework.boot:spring-boot-starter-test')  
  36. }  


3. 新建controller

[java]  view plain  copy
  1. package com.example.demo.control;  
  2.   
  3.   
  4. import org.springframework.web.bind.annotation.RequestMapping;  
  5. import org.springframework.web.bind.annotation.RestController;  
  6.   
  7. @RestController  
  8. public class TestController {  
  9.   
  10.     @RequestMapping(value = "")  
  11.     public String test(){  
  12.         return "hello cueb";  
  13.     }  
  14. }  


4. 新建model

  1. package com.example.demo.model;  
  2.   
  3. import javax.persistence.Entity;  
  4. import javax.persistence.GeneratedValue;  
  5. import javax.persistence.GenerationType;  
  6. import javax.persistence.Id;  
  7.   
  8. @Entity  
  9. public class User {  
  10.     @Id  
  11.     @GeneratedValue(strategy= GenerationType.AUTO)  
  12.     int id;  
  13.   
  14.     public int getId() {  
  15.         return id;  
  16.     }  
  17.   
  18.     public void setId(int id) {  
  19.         this.id = id;  
  20.     }  
  21.   
  22.     private String name;  
  23.   
  24.     public String getName() {  
  25.         return name;  
  26.     }  
  27.   
  28.     public void setName(String name) {  
  29.         this.name = name;  
  30.     }  
  31. }  


三、部署运行

1. debug 启动

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Alex_81D

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值