在springboot中使用h2数据库
生活随笔
收集整理的這篇文章主要介紹了
在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:~/testdriver-class-name: org.h2.Driverusername: sapassword: 123456# schema: classpath:db/schema.sql # data: classpath:db/data.sqljpa:database: h2hibernate:ddl-auto: updateshow-sql: trueh2:console:path: /h2-consoleenabled: true3. 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 {@Autowiredprivate 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 {@BeanInitializingBean 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. 執行完畢后,可以看到下面顯示了我們加入的數據。
轉載于:https://www.cnblogs.com/mizersy/p/10698409.html
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的在springboot中使用h2数据库的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: zoj 3601
- 下一篇: SQL:我为什么慢你心里没数吗?