Spring Boot中的嵌入式数据库使用

大家好,我是微赚淘客系统3.0的小编,也是冬天不穿秋裤,天冷也要风度的程序猿!今天我们来探讨如何在Spring Boot中使用嵌入式数据库。嵌入式数据库是一种轻量级的数据库,适用于开发、测试和小型应用。常见的嵌入式数据库有H2、HSQLDB和Derby。

一、为什么选择嵌入式数据库

嵌入式数据库的优势在于它们的轻量级和易用性,适合在开发和测试环境中快速搭建和使用。在Spring Boot中,我们可以非常方便地集成和使用这些嵌入式数据库。

二、项目初始化

首先,创建一个Spring Boot项目,并添加必要的依赖。在pom.xml中添加以下依赖:

<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>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.

三、配置H2数据库

application.properties中添加H2数据库的配置:

spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.h2.console.enabled=true
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

这些配置将启用H2数据库,并在内存中创建一个名为testdb的数据库。我们还启用了H2的Web控制台,方便我们在浏览器中查看和操作数据库。

四、创建实体类和Repository

假设我们有一个简单的用户实体类和对应的Repository:

User.java

package cn.juwatech.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
    private String email;

    // Getters and Setters
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.

UserRepository.java

package cn.juwatech.repository;

import cn.juwatech.model.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface UserRepository extends JpaRepository<User, Long> {
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.

五、创建服务层和控制器

为了更好地展示H2数据库的使用,我们创建一个简单的服务层和控制器:

UserService.java

package cn.juwatech.service;

import cn.juwatech.model.User;
import cn.juwatech.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService {

    @Autowired
    private UserRepository userRepository;

    public List<User> getAllUsers() {
        return userRepository.findAll();
    }

    public User createUser(User user) {
        return userRepository.save(user);
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.

UserController.java

package cn.juwatech.controller;

import cn.juwatech.model.User;
import cn.juwatech.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping
    public List<User> getAllUsers() {
        return userService.getAllUsers();
    }

    @PostMapping
    public User createUser(@RequestBody User user) {
        return userService.createUser(user);
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.

六、测试嵌入式数据库

启动Spring Boot应用程序,并使用以下命令测试API:

  1. 获取所有用户:
curl http://localhost:8080/users
  • 1.
  1. 创建用户:
curl -X POST http://localhost:8080/users -H "Content-Type: application/json" -d '{"name": "John Doe", "email": "john.doe@example.com"}'
  • 1.
  1. 再次获取所有用户,查看新创建的用户:
curl http://localhost:8080/users
  • 1.

七、访问H2控制台

在浏览器中打开H2控制台,查看数据库中的数据:

http://localhost:8080/h2-console
  • 1.

在登录页面中,使用配置文件中的数据库URL和用户名进行登录:

  • JDBC URL: jdbc:h2:mem:testdb
  • User Name: sa
  • Password: (leave blank)

八、总结

通过以上步骤,我们成功地在Spring Boot中集成了H2嵌入式数据库,并实现了基本的CRUD操作。嵌入式数据库非常适合在开发和测试环境中使用,能够快速搭建和验证应用程序的功能。