spring boot2.x中集成H2数据库
H2数据库介绍 查看
在spring boot中集成
1.添加依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency>
2.添加H2相关配置,修改application.properties文件
spring.jpa.database=h2 spring.jpa.show-sql=true #ddl执行方式,update create 等 spring.datasource.url=jdbc:h2:./data/test;AUTO_SERVER=TRUE spring.jpa.hibernate.ddl-auto=update spring.datasource.username=sa spring.datasource.password=123456 spring.datasource.driverClassName=org.h2.Driver spring.h2.console.path=/h2-console spring.h2.console.enabled=true
说明:
spring.datasource.url
数据库文件
(1)内存数据库
jdbc:h2:mem:DBName
内存数据库的数据存在内存中,当程序停止时,不会被保存会丢失
eg:
spring.datasource.url=jdbc:h2:mem:test
(2)文件数据库
jdbc:h2:file:{FilePath} 也可以简化为 jdbc:h2:{FilePath}
FilePath的格式
a) ./{path}/{fileName} 在当前程序的根目录下创建目录和数据库文件
b) ~/{path}/{fileName} 在当前用户的根目录下创建目录和数据库文件
c) C:/{path}/{fileName} 在指定盘符的指定目录下创建数据库文件
(3)远程数据库
jdbc:h2:tcp://<{IP|hostname}>[:{Port}]/[]<{dbName}>
附加参数:
AUTO_SERVER=TRUE 启动自动混合模式,允许开启多个连接,该参数不支持在内存中运行模式
DB_CLOSE_ON_EXIT=FALSE,当虚拟机退出时并不关闭数据库
3.代码
domain层,即User类(entity)
package com.example.demo.domain; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import javax.persistence.*; @Entity @Table(name = "user") @Data public class User { @Id @GeneratedValue(strategy= GenerationType.AUTO) private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
dao层,即UserRepository 接口
package com.example.demo.dao; import com.example.demo.domain.User; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; import java.util.List; @Repository public interface UserRepository extends JpaRepository<User,Integer> { List<User> getUsersByName(String Name); }
controller层,即Demo
package com.example.demo.controller; import com.example.demo.dao.UserRepository; import com.example.demo.domain.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController public class Demo { @Autowired private UserRepository repo; @RequestMapping("find") public List<User> find() { return (List<User>) repo.findAll(); } }
编写DemoApplication
package com.example.demo; import com.example.demo.dao.UserRepository; import com.example.demo.domain.User; import org.springframework.beans.factory.InitializingBean; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; @SpringBootApplication public class DemoApplication { @Bean InitializingBean saveData(UserRepository repo){ return ()->{ User u = new User(); u.setName("abc"); repo.save(u); User u1 = new User(); u1.setName("zyx"); repo.save(u1); }; } public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
启动项目,打开浏览器访问http://localhost:8080/find
访问http://localhost:8080/h2-console/
连接上后查询数据
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)