如何在springboot中使用h2数据库

2024-10-13 00:50:42

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

如何在springboot中使用h2数据库

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

4、domain层,即User类:

如何在springboot中使用h2数据库

6、service层,即UserService:

如何在springboot中使用h2数据库

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

如何在springboot中使用h2数据库

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

如何在springboot中使用h2数据库

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

如何在springboot中使用h2数据库
猜你喜欢