如何在springboot中使用h2数据库

2025-05-04 23:26:16

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数据库
声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢