springboot h2数据库的配置

配置文件

  #h2 数据库配置

#配置数据库连接地址
spring.datasource.url=jdbc:h2:sunniwell:sos
#配置数据库驱动
spring.datasource.driver-class-name=org.h2.Driver
#配置数据库用户名
spring.datasource.username=root
#配置数据库密码
spring.datasource.password=sunniwell
#配置能远程访问
spring.h2.console.settings.web-allow-others=true
#配置访问地址
spring.h2.console.path=/h2-console
#配置项目启动 h2就启动
spring.h2.console.enabled=true
#进行该配置后,每次启动程序,程序都会运行resources/db/schema.sql文件,对数据库的结构进行操作。
#spring.datasource.schema=classpath:db/schema.sql
#进行该配置后,每次启动程序,程序都会运行resources/db/data.sql文件,对数据库的数据操作。
#spring.datasource.data=classpath:db/data.sql

 

 

pom.xml

 

<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>



H2数据库介绍

常用的开源数据库:H2,Derby,HSQLDB,MySQL,PostgreSQL。其中H2,HSQLDB类似,十分适合作为嵌入式数据库使用,其它的数据库大部分都需要安装独立的客户端和服务器端。
H2的优势:
1、h2采用纯Java编写,因此不受平台的限制。
2、h2只有一个jar文件,十分适合作为嵌入式数据库试用。
3、性能和功能的优势

H2比HSQLDB的最大的优势就是h2提供了一个十分方便的web控制台用于操作和管理数据库内容,这点比起HSQLDB的swing和awt控制台实在好用多了。





Spring Boot 和 H2 数据库结合是一个常见的轻量级开发场景,H2 是一个内存型的嵌入式数据库,常用于快速原型开发和测试。下面是一个简单的 Spring Boot 应用使用 H2 的例子: 首先,在 `pom.xml` 文件中添加 H2 数据源依赖: ```xml <dependencies> <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> </dependencies> ``` 然后,配置数据源和 JPA 配置: ```java @Configuration @EnableJpaRepositories(basePackages = "com.example.demo.repository") // 指定仓库模块位置 public class DatabaseConfig { @Value("${spring.datasource.url}") private String url; @Value("${spring.datasource.username}") private String username; @Value("${spring.datasource.password}") private String password; @Bean public DataSource dataSource() { return new EmbeddedDatabaseBuilder() .setType(EmbeddedDatabaseType.H2) // 使用H2 .addScript("schema.sql") // 加载初始化脚本 .build(); } @Bean public PlatformTransactionManager transactionManager(DataSource dataSource) { return new JpaTransactionManager(entityManagerFactory(dataSource)); } @Bean public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource) { HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); Map<String, Object> jpaProperties = new HashMap<>(); jpaProperties.put("hibernate.dialect", H2Dialect.class); jpaProperties.put("hibernate.show_sql", true); // 显示SQL日志 LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean(); factory.setDataSource(dataSource); factory.setJpaVendorAdapter(vendorAdapter); factory.setPackagesToScan("com.example.demo.entity"); // 扫描实体类包 factory.setJpaProperties(jpaProperties); return factory; } } ``` 在这个例子中,你需要创建一个 `schema.sql` 脚本来定义数据库结构。现在你可以创建一个简单的实体类 (`User.java`),并创建对应的 Repository 接口 (`UserRepository.java`)。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值