H2数据库的Spring Boot

在本快速教程中,我们将引导一个由内存H2数据库支持的简单Spring Boot应用程序。 我们将使用Spring Data JPA与我们的数据库进行交互。

项目设置:

首先,让我们使用Spring Initializr生成我们的项目模板

点击“生成项目”链接后,我们的项目文件将被下载。

现在,如果我们仔细查看生成的POM文件,将在下面添加依赖项:

 < dependency >

    < groupId >org.springframework.boot</ groupId >

    < artifactId >spring-boot-starter</ artifactId >
 </ dependency >
 < dependency >

    < groupId >org.springframework.boot</ groupId >

    < artifactId >spring-boot-starter-test</ artifactId >
 </ dependency >
 < dependency >

    < groupId >org.springframework.boot</ groupId >

    < artifactId >spring-boot-starter-data-jpa</ artifactId >
 </ dependency >
 < dependency >

    < groupId >com.h2database</ groupId >

    < artifactId >h2</ artifactId >

    < scope >runtime</ scope >
 </ dependency >

H2默认属性:

由于我们已经添加了H2数据库依赖关系,因此Spring Boot将自动配置其相关属性。 默认配置包括:

 spring.datasource.url=jdbc:h2:mem:testdb
 spring.datasource.driverClassName=org.h2.Driver
 spring.datasource.username=sa
 spring.datasource.password=
 spring.h2.console.enabled= false

我们通过在application.properties文件中定义这些属性来覆盖其中一些属性:

 spring.h2.console.enabled= true
 spring.h2.console.path= /h2
 spring.datasource.url=jdbc:h2:mem:university

在这里,我们的数据库名称将是一所大学。 我们还启用了H2控制台并设置了其上下文路径。

定义实体:

现在,我们将定义一个Student实体:

@Entity public class Student {

    @Id

    @GeneratedValue (strategy = GenerationType.AUTO)

    private Integer id;
 
    private String name;
    
    public Student(String name) {

        this .name = name;

    }
 
    //getters, setters
 
    public String toString() {

        return "{id=" + id + ", name=" + name + "}" ;

    }
 }

及其对应的Spring Data JPA存储库:

 @Repository
 public interface StudentRepository

  extends CrudRepository<Student, Integer> {
 }

学生实体将以完全相同的名称映射到数据库表。 如果需要,可以使用@Table批注指定其他表名。

应用类别:

最后,让我们实现我们的UniversityApplication类:

 @SpringBootApplication
 public class UniversityApplication {
 
    public static void main(String[] args) {

        SpringApplication.run(UniversityApplication. class , args);

    }
 
    @Bean

    public CommandLineRunner testApp(StudentRepository repo) {

        return args -> {

            repo.save( new Student( "James" ));

            repo.save( new Student( "Selena" ));
 
            List<Student> allStudents = repo.findAll();

            System.out.println( "All students in DB: " + allStudents);
 
            Student james = repo.findById( 1 );

            System.out.println( "James: " + james);

        };

    }
 }

此类是我们Spring Boot应用程序的起点。 在这里, @SpringBootApplication注释等效于将@ ComponentScan,@ EnableAutoConfiguration@SpringConfiguration组合在一起。

我们还定义了CommandLineRunner的实例。 因此,当我们运行应用程序时,控制台日志将具有:

 UniversityApplication:All students in DB: [{id= 1 , name=James}

 , {id= 2 , name=Selena}]
 James: {id= 1 , name=James}
 ...

请注意,在Spring Boot中,理想情况下所有实体都应在与主应用程序类相同的包级别或更低的级别(在子包中)定义。 如果是这样,Spring Boot将自动扫描所有这些实体。

访问H2控制台:

我们还可以在H2控制台上检查数据库条目。

为此,我们将在任何浏览器上打开URL: http:// localhost:8080 / h2并使用我们的数据库配置登录。 有了它,我们将能够在UI Console仪表板上轻松查看所有创建的表和条目。

结论:

在本教程中,我们使用单个实体引导了一个非常简单的Spring Boot应用程序。 该应用程序与H2数据库集成在一起,并使用Spring Data JPA。我们可以轻松地扩展它,以适应更广泛的应用范围。

翻译自: https://www.javacodegeeks.com/2019/09/spring-boot-with-h2-database.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值