spring mysql 切换 h2_在springboot中使用h2数据库

在springboot中使用h2数据库

一、h2数据库介绍

h2database为我们提供了十分轻量,十分快捷方便的内嵌式数据库

H2是一个用Java开发的嵌入式数据库,它本身只是一个类库,可以直接嵌入到应用项目中。

可以同应用程序打包在一起发布

它的另一个用途是用于单元测试。启动速度快,而且可以关闭持久化功能,每一个用例执行完随即还原到初始状态

提供JDBC访问接口,提供基于浏览器的控制台,可以执行sql

免费,开源,够快

还方便了程序刚开始dao层单元测试测试,不需要搭建oracle,不需要加载mysql,快速测试写的dao

二、导入过程

1. 在pom.xml中导入相关依赖

com.h2database

h2

runtime

org.springframework.boot

spring-boot-starter-test

test

2. 修改application.yml文件,加入H2相关配置

server:

port: 8089

spring:

datasource:

url: jdbc:h2:~/test

driver-class-name: org.h2.Driver

username: sa

password: 123456

# schema: classpath:db/schema.sql

# data: classpath:db/data.sql

jpa:

database: h2

hibernate:

ddl-auto: update

show-sql: true

h2:

console:

path: /h2-console

enabled: true

3. domain层,即Location类(entity):

package com.springboot.demo.entity;

import lombok.AllArgsConstructor;

import lombok.Data;

import lombok.NoArgsConstructor;

import javax.persistence.*;

@Entity

@Data

@AllArgsConstructor

@NoArgsConstructor

public class Location {

@Id

@GeneratedValue(strategy = GenerationType.IDENTITY)

private Long id;

private String type;

private double latitude;

private double longtitude;

}

4. dao层,即LocationRepository接口:

package com.springboot.demo.repository;

import com.springboot.demo.entity.Location;

import org.springframework.context.annotation.Bean;

import org.springframework.data.jpa.repository.JpaRepository;

import org.springframework.stereotype.Repository;

import java.util.List;

@Repository

public interface LocationRepository extends JpaRepository {

List getLocationsByType(String type);

}

5. controller层,即LocationController:

package com.springboot.demo.controller;

import com.springboot.demo.entity.Location;

import com.springboot.demo.repository.LocationRepository;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.stereotype.Repository;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller

public class HelloContraller {

@Autowired

private LocationRepository locationRepository;

@ResponseBody

@RequestMapping("/hello")

public List hello(){

return locationRepository.findAll();

}

}

6. 编写DemoApplication

package com.springboot.demo;

import com.springboot.demo.entity.Location;

import com.springboot.demo.repository.LocationRepository;

import org.springframework.beans.factory.InitializingBean;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.boot.web.servlet.FilterRegistrationBean;

import org.springframework.context.annotation.Bean;

@SpringBootApplication

public class DemoApplication {

@Bean

InitializingBean saveData(LocationRepository repo){

return ()->{

repo.save(new Location((long) 1,"1",38.998064, 117.317267));

repo.save(new Location((long)2,"2",38.997793, 117.317069));

repo.save(new Location((long)3,"3",38.998006, 117.317101));

repo.save(new Location((long)4,"4",38.997814, 117.317332));

};

}

public static void main(String[] args) {

SpringApplication.run(DemoApplication.class, args);

}

}

7. 启动项目,打开浏览器访问http://localhost:8089/hello

162e9e68f6fcccd321922d1026919242.png

8. 下面使用H2控制台查看:

输入用户名sa,密码123456

c7125631e3c87ce0ce5ff48a9685a679.png

9. 在打开的页面中点击左侧的Location。

a96e256908d013ab784788fd5674e327.png

10. 可以看到右侧显示了SQL:

SELECT * FROM USER

点击上面的Run执行。

8d484dff8faaffa7b8b896d429d164c9.png

11. 执行完毕后,可以看到下面显示了我们加入的数据。

c694782dd5a15b0ba535a047e5f45f54.png

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot 内嵌的 H2 数据库是一款基于 Java 的内存数据库,可以很方便地在应用程序使用使用 H2 数据库有很多好处,比如它非常轻量级,可以快速启动和停止,适合于开发和测试环境等。 下面是在 Spring Boot 使用 H2 数据库的步骤: 1. 在 pom.xml 添加 H2 数据库的依赖: ```xml <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> </dependency> ``` 2. 在 application.properties 文件配置 H2 数据库相关信息: ```properties # H2 Database spring.datasource.url=jdbc:h2:mem:testdb spring.datasource.driver-class-name=org.h2.Driver spring.datasource.username=sa spring.datasource.password= ``` 这里的配置将 H2 数据库的 URL 设置为 `jdbc:h2:mem:testdb`,表示使用内存数据库,用户名和密码分别设置为 `sa` 和空字符串。 3. 创建实体类和数据访问层: ```java @Entity @Table(name = "users") public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String username; private String password; // getters and setters } @Repository public interface UserRepository extends JpaRepository<User, Long> { } ``` 这里我们创建了一个名为 `User` 的实体类,并使用 `JpaRepository` 接口来访问数据库。 4. 创建控制器和服务层: ```java @RestController public class UserController { @Autowired private UserService userService; @GetMapping("/users") public List<User> getUsers() { return userService.getUsers(); } } @Service public class UserService { @Autowired private UserRepository userRepository; public List<User> getUsers() { return userRepository.findAll(); } } ``` 这里我们创建了一个名为 `UserController` 的控制器,并使用 `UserService` 来获取用户列表。 5. 运行应用程序,并访问 http://localhost:8080/users 即可查看用户列表。 总之,使用 H2 数据库内嵌到 Spring Boot 应用程序非常简单,可以方便地进行开发和测试。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值