使用idea搭建springboot+gradle+mysql项目

目录结构如下:
使用idea搭建springboot+gradle+mysql项目_第1张图片

1.新建一个新的gradle项目

使用idea搭建springboot+gradle+mysql项目_第2张图片

使用idea搭建springboot+gradle+mysql项目_第3张图片使用idea搭建springboot+gradle+mysql项目_第4张图片使用idea搭建springboot+gradle+mysql项目_第5张图片

2.build.gradle配置文件

group 'com.great'
version '1.0-SNAPSHOT'

buildscript {
    ext {
        springBootVersion = '1.2.2.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'

jar {
    baseName = 'ng-spring-boot'
    version = '0.0.1-SNAPSHOT'
}
sourceCompatibility = 1.7
targetCompatibility = 1.7

repositories {
    mavenCentral()
    maven { url "https://repo.spring.io/libs-release" }
}

dependencies {
    compile("org.springframework.boot:spring-boot-starter-data-jpa")
    compile("org.springframework.boot:spring-boot-starter-web")
    compile("org.springframework.boot:spring-boot-starter-actuator")
    runtime("mysql:mysql-connector-java")
    testCompile("org.springframework.boot:spring-boot-starter-test")
}

task wrapper(type: Wrapper) {
    gradleVersion = '2.3'
}

3.创建实体类

package com.great.dept.domain;


import org.hibernate.annotations.GenericGenerator;

import javax.persistence.*;


/**
 * 部门表实体类
 */
@Entity
@Table(name = "dept")
public class Dept {

    //部门编号 主键
    @Id
    @Column(name = "id")
    @GeneratedValue(generator = "uuid2")
    @GenericGenerator(name = "uuid2", strategy = "uuid2")
    private String id;

    //部门编码
    @Column(name = "code")
    private String code;

    //部门名称
    @Column(name = "name")
    private String name;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

4.创建JPA仓库类

package com.great.dept.repository;


import com.great.dept.domain.Dept;
import org.springframework.data.jpa.repository.JpaRepository;

/**
 * 实现Jap仓库接口类 
 */
public interface DeptRepository extends JpaRepository {
}

5。创建controller类

package com.great.dept.web;


import com.great.dept.domain.Dept;
import com.great.dept.repository.DeptRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * 部门控制器
 */

@RestController
@RequestMapping("/test")
public class DeptController {

    //部门Jap仓库接口
    @Autowired
    private DeptRepository deptRepository;

    @RequestMapping(method = RequestMethod.GET)
    public List deptfind() {
        List deptList = deptRepository.findAll();
        for (int i = 0; i < deptList.size(); i++) {
            System.out.println(deptList.get(i));
        }
        return deptList;
    }
}

6.新建属性文件application.properties,配置连接数据库的信息

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&useSSL=false
spring.datasource.username=root
spring.datasource.password=root

spring.jpa.database=mysql
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
#spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
使用idea搭建springboot+gradle+mysql项目_第6张图片

7.最后配置启动主函数*(spring Boot的被@SpringBootApplication注解的Application.Java必须放在所有的RestController的根路径的package下)

package com.great;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

访问路径: http://127.0.0.1:8080/test


你可能感兴趣的:(spring,boot)