SpringBoot/Spring Data/Hibernate入门 [英] Getting started with SpringBoot/Spring Data/ Hibernate

查看:77
本文介绍了SpringBoot/Spring Data/Hibernate入门的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始实习,对我需要做的所有工作有点不知所措.我从未使用过数据库,也不知道如何开始.我的主管要求我将Spring数据与内存数据库连接,以写入和删除对象(实际上是任何东西).我正在使用eclipse并安装了Spring(我认为),但是卡住了,并且不知道从哪里开始并使它变得简单.我以前从未在数据库中工作过,主管似乎总是很忙.

我遇到了这个示例,但是不知道在哪里编写哪个代码,这似乎是为经验丰富的程序员所解释的:

http://projects.spring.io/spring-data/#quick -开始

以下是指南中的一些代码:

@Entity
public class Employee {

    private @Id @GeneratedValue Long id;
    private String firstName, lastName, description;

    private Employee() {}

    public Employee(String firstName, String lastName, String description) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.description = description;
        }
    }

这是在Spring Data的首页上,它是关于入门的.但是我不确定这意味着什么,也不确定如何运行代码或要构建什么类.如果我尝试在pam文件中编写他们的代码,它只会显示错误,并且该实体不起作用...

解决方案

  1. 首先,在Eclipse中设置 Maven (您可以在Google中找到说明,此处).

  2. 然后转到 start.spring.io 并生成您的项目模板.您只需选择 JPA H2 ( H2 -作为内存数据库)作为依赖项,设置您的 Group (默认为'com.example')和 Name (默认为'demo') ).然后单击生成项目".将文件保存并解压缩到计算机上的某个目录后,请在IDE中打开该项目.

  3. 您将找到一个应用程序类-DemoApplication.在它旁边创建您的实体类,例如-Employee.您将保存到数据库并从数据库中加载其数据.自动生成 toString .

 @Entity
public class Employee {

    @Id 
    @GeneratedValue 
    private Long id;

    private String firstName, lastName, description;

    private Employee() {}

    public Employee(String firstName, String lastName, String description) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.description = description;
    }

    // Autogenerate here getters, setters and toString()
}
 

  1. 然后创建一个存储库"类,该类提供对数据库的访问权限:

 public interface EmployeeRepository extends JpaRepository<Employee, Long> {}
 

  1. 在项目中找到DemoApplicationTests类,对其进行编辑,然后运行:

 @RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests {

    // Injecting your repository
    @Autoware
    private EmployeeRepository repo;

    @Test
    public void dbTest() {

        // Create two employees
        Employee gandalf = new Employee("Gandalf", "Grey", "Wizard");
        Employee frodo = new Employee("Frodo", "Baggins", "Hobbit");

        // Save them to DB
        repo.save(Arrays.asList(gandalf, frodo));

        // Read them from DB
        List<Employee> employees = repo.findAll();

        // Print them
        employees.forEach(System.out::println);
    }
}
 

仅此而已!

更多信息:

Spring Data JPA项目

Spring Data JPA参考

入门指南

I just started my intern position and am a bit overwhelmed by all the work I need to do. I never worked with databases and don't know how to start. My supervisor asked me to connect Spring data with an in-memory database, to write and delete objects (really anything). I am using eclipse and installed Spring (I think), but am stuck and don't know where to start and make it simple. I never worked in databases before, and my supervisor seems very busy all the time.

I came across this example, but don't know where to write which code, as it seems to be explained for seasoned programmers:

http://projects.spring.io/spring-data/#quick-start

Here is some of the code from the guide:

@Entity
public class Employee {

    private @Id @GeneratedValue Long id;
    private String firstName, lastName, description;

    private Employee() {}

    public Employee(String firstName, String lastName, String description) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.description = description;
        }
    }

This is at the Spring Data front page, and it's about getting started. But I am not sure what any of this means, nor how to run the code or what classes to build. If I try to write their code at the pam file it just shows errors and the entity doesn't work...

解决方案

  1. First, setup Maven in Eclipse (you can find instructions in Google, here for example).

  2. Then go to start.spring.io and generate your project template. You need to choose just JPA and H2 (H2 - is in-memory database) as dependencies, set your Group ('com.example' by default) and Name ('demo' by default). Then click to 'Generate Project'. After saving and unpacking the file into the some directory on your computer, open this project in your IDE.

  3. You will find an application class - DemoApplication. Beside it create your entity class, for example - Employee. You will be saving to and loading from the database its data. Autogenerate getters, setters and toString in this class.

@Entity
public class Employee {

    @Id 
    @GeneratedValue 
    private Long id;

    private String firstName, lastName, description;

    private Employee() {}

    public Employee(String firstName, String lastName, String description) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.description = description;
    }

    // Autogenerate here getters, setters and toString()
}

  1. Then create a 'Repository' class which provide access to your database:

public interface EmployeeRepository extends JpaRepository<Employee, Long> {}

  1. Find in your project the DemoApplicationTests class, edit it, then run:

@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests {

    // Injecting your repository
    @Autoware
    private EmployeeRepository repo;

    @Test
    public void dbTest() {

        // Create two employees
        Employee gandalf = new Employee("Gandalf", "Grey", "Wizard");
        Employee frodo = new Employee("Frodo", "Baggins", "Hobbit");

        // Save them to DB
        repo.save(Arrays.asList(gandalf, frodo));

        // Read them from DB
        List<Employee> employees = repo.findAll();

        // Print them
        employees.forEach(System.out::println);
    }
}

That's all!

More info:

Spring Data JPA Project

Spring Data JPA Reference

Getting started guide

这篇关于SpringBoot/Spring Data/Hibernate入门的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆