在springboot中使用h2數據庫


在springboot中使用h2數據庫

一、h2數據庫介紹

h2database為我們提供了十分輕量,十分快捷方便的內嵌式數據庫

  • H2是一個用Java開發的嵌入式數據庫,它本身只是一個類庫,可以直接嵌入到應用項目中。
  • 可以同應用程序打包在一起發布
  • 它的另一個用途是用於單元測試。啟動速度快,而且可以關閉持久化功能,每一個用例執行完隨即還原到初始狀態
  • 提供JDBC訪問接口,提供基於瀏覽器的控制台,可以執行sql
  • 免費,開源,夠快
  • 還方便了程序剛開始dao層單元測試測試,不需要搭建oracle,不需要加載mysql,快速測試寫的dao

二、導入過程

1. 在pom.xml中導入相關依賴

<dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
</dependency>
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
</dependency>

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<Location,Long> {
    List<Location> 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<Location> 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

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

http://localhost:8089/h2-console

輸入用戶名sa,密碼123456

9. 在打開的頁面中點擊左側的Location。

10. 可以看到右側顯示了SQL:

SELECT * FROM USER

點擊上面的Run執行。

11. 執行完畢后,可以看到下面顯示了我們加入的數據。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM