SpringBoot实践之---JPA连接数据库+idea全新创建该工程

  1. 录结构如下:

这里写图片描述

1.新建一个新的gradle项目
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述

2.build.gradle配置文件

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

buildscript {  
    ext {  
        springBootVersion = '1.5.8.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.8
targetCompatibility = 1.8  

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")  
    compile("mysql:mysql-connector-java")  
    compile("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<Dept, String> {  
}  

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<Dept> deptfind() {  
        List<Dept> 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  

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值