手抄报 安全手抄报 手抄报内容 手抄报图片 英语手抄报 清明节手抄报 节约用水手抄报

如何在springboot中使用h2数据库

时间:2024-10-13 00:50:42

1、首先在pom.xml里加入h2依赖。

如何在springboot中使用h2数据库

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

如何在springboot中使用h2数据库

3、h2的配置就完成了,简单吧,接下来就是编写常规的domain\dao\service\controller了。

4、domain层,即User类:

如何在springboot中使用h2数据库

5、dao层,即UserRepository接口:

如何在springboot中使用h2数据库

6、service层,即UserService:

如何在springboot中使用h2数据库

7、controller层,即UserController:

如何在springboot中使用h2数据库

8、启动项目,打开浏览器访问:http://localhost:8888/add返回了success,说明添加数据成功。

如何在springboot中使用h2数据库

9、访问http://localhost:8888/list查看数据列表,如下:

如何在springboot中使用h2数据库

10、下面使用H2控制台查看:http://localhost:8888/h2-console输入用户名sa,密码123456

如何在springboot中使用h2数据库

11、在打开的页面中点击左侧的USER。

如何在springboot中使用h2数据库

12、可以看到右侧显示了SQL:SELECT * FROM USER点击上面的Run执行。

如何在springboot中使用h2数据库

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

如何在springboot中使用h2数据库
© 手抄报圈