日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

h2 不能访问localhost_个人学习系列 - Spring Boot 整合 H2

發(fā)布時(shí)間:2024/4/19 javascript 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 h2 不能访问localhost_个人学习系列 - Spring Boot 整合 H2 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
每當(dāng)有項(xiàng)目需要訪問數(shù)據(jù)庫的時(shí)候總是很苦惱,因?yàn)闊o論哪個(gè)數(shù)據(jù)庫都比較龐大,運(yùn)行起來也比較耗內(nèi)存,有沒有簡單可以隨項(xiàng)目啟動(dòng)的數(shù)據(jù)庫嗎?有,H2來了。。。

1. 搭建Spirng Boot項(xiàng)目

如果啟動(dòng)出現(xiàn)異常,就將中文注釋去掉再啟動(dòng)即可。

1.1 pom.xml

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId> </dependency><dependency><groupId>com.h2database</groupId><artifactId>h2</artifactId><scope>runtime</scope> </dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><optional>true</optional> </dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId> </dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId> </dependency>

1.2 application.yml

server:port: 8888 # 端口號(hào) spring:jpa:show-sql: true # 啟用SQL語句的日志記錄hibernate:ddl-auto: update # 設(shè)置ddl模式datasource:driver-class-name: org.h2.Driver # url: jdbc:h2:mem:h2 # 配置h2數(shù)據(jù)庫連接地址(這里使用的是內(nèi)存,下次啟動(dòng)數(shù)據(jù)就不存在了)url: jdbc:h2:file:./db/h2;AUTO_SERVER=TRUE # 配置h2數(shù)據(jù)庫連接地址(這里使用的是本地存儲(chǔ)方式,下次啟動(dòng)數(shù)據(jù)還存在)username: root # 配置數(shù)據(jù)庫用戶名password: 123456 # 配置數(shù)據(jù)庫密碼h2:console:path: /h2 # 進(jìn)行該配置,你就可以通過YOUR_URL/h2訪問h2 web consloe。YOUR_URL是你程序的訪問URlenabled: true # 配置程序開啟時(shí)就會(huì)啟動(dòng)h2 web consloesettings:web-allow-others: true # 配置h2的遠(yuǎn)程訪問

1.3 新建User實(shí)體類

/*** 實(shí)體類* @author zhouzhaodong*/ @Entity @Table(name="user") public class User {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)private Integer id;@Columnprivate String name;@Columnprivate String phone;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getPhone() {return phone;}public void setPhone(String phone) {this.phone = phone;} }

1.4 新建UserDao類

/*** dao* @author zhouzhaodong*/ public interface UserDao extends JpaRepository<User, Integer> { }

1.5 新建UserController控制層

/*** 控制層* @author zhouzhaodong*/ @RestController public class UserController {@ResourceUserDao userDao;@RequestMapping("/findById")public Object findById(Integer id){return userDao.findById(id);}@RequestMapping("/insert")public void insert(User user){userDao.save(user);}@RequestMapping("/delete")public void delete(Integer id){userDao.deleteById(id);}}

2. 測試

2.1 啟動(dòng)項(xiàng)目

會(huì)發(fā)現(xiàn)db持久化在項(xiàng)目內(nèi)部了:

2.2 打開localhost:8888/h2

2.2.1 登錄界面

2.2.2 填寫完畢后點(diǎn)擊連接:

2.2.3 新建User表

create table if not exists USER( ID INT NOT NULL PRIMARY KEY AUTO_INCREMENT, NAME VARCHAR(100), PHONE VARCHAR(100) );

2.3 訪問接口進(jìn)行數(shù)據(jù)操作

2.3.1 新增數(shù)據(jù)

2.3.2 查詢數(shù)據(jù)

2.3.3 刪除數(shù)據(jù)

個(gè)人博客地址:

http://www.zhouzhaodong.xyz

GitHub代碼地址:

https://github.com/zhouzhaodo...

總結(jié)

以上是生活随笔為你收集整理的h2 不能访问localhost_个人学习系列 - Spring Boot 整合 H2的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。