我正在尝试像内存数据库一样使用
hsqldb
。
在我的
build.gradle
文件中,你可以看到版本:
implementation group: 'org.hsqldb', name: 'hsqldb', version: '2.7.2'
在我的
application.yml
文件中,我有:
spring:
application:
name: ms-people
datasource:
driver-class-name: org.hsqldb.jdbc.JDBCDriver
url: jdbc:hsqldb:mem:testdb;DB_CLOSE_DELAY=-1
username: sa
password:
jpa:
show-sql: true
generate-ddl: true
#database-platform: org.hibernate.dialect.HSQLDialect
hibernate:
ddl-auto: create
在日志中,我获得:
Hibernate: drop table if exists phones cascade
Hibernate: drop table if exists users cascade
Hibernate: create table phones (city_code integer, country_code integer, id bigint generated by default as identity (start with 1), number bigint, user_id bigint not null, primary key (id))
Hibernate: create table users (isactive boolean not null, created timestamp(6), id bigint generated by default as identity (start with 1), last_login timestamp(6), modified timestamp(6), email varchar(255) unique, name varchar(255), password varchar(255), token varchar(255), primary key (id))
Hibernate: alter table phones add constraint FKmg6d77tgqfen7n1g763nvsqe3 foreign key (user_id) references users
但是,当我尝试连接并查看我的数据库和表时。我什么也看不见。
我如何查看执行的查询/命令?
我的错误是什么?
编辑
我正在阅读这篇文章https://www.baeldung.com/java-in-memory-databases
我正在使用
H2
进行测试:
在我的build.gradle
文件中,你可以看到版本:
runtimeOnly group: 'com.h2database', name: 'h2', version: '2.2.224'
在我的
application.yml
文件中,我有:
spring:
application:
name: ms-people
h2:
console:
enabled: true
datasource:
driver-class-name: org.h2.Driver
url: jdbc:h2:mem:testdb
username: sa
password:
jpa:
show-sql: true
generate-ddl: true
hibernate:
ddl-auto: create
除了 H2,H2 允许我们显示什么其他内容?
对于
HSQLDB (HyperSQL Database)
我找到了这篇文章https://mkyong.com/spring/spring-view-content-of-hsqldb-embedded-database/
package org.bz.app.mspeople.configurations;
import jakarta.annotation.PostConstruct;
import org.hsqldb.util.DatabaseManagerSwing;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import javax.sql.DataSource;
@Configuration
public class SpringRootConfig {
@Autowired
DataSource dataSource;
@Bean
public JdbcTemplate getJdbcTemplate() {
return new JdbcTemplate(dataSource);
}
//default username : sa, password : ''
@PostConstruct
public void getDbManager() {
DatabaseManagerSwing.main(
new String[]{"--url", "jdbc:hsqldb:mem:testdb", "--user", "sa", "--password", ""});
}
}
还有这个班级:
package org.bz.app.mspeople.configurations;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import javax.sql.DataSource;
@Configuration
public class DataSourceConfig {
@Bean
public DataSource dataSource(){
//jdbc:hsqldb:mem:testdb
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
EmbeddedDatabase db = builder.setType(EmbeddedDatabaseType.HSQL)
//.addScript("db/hsqldb/db.sql")
.build();
return db;
}
}
此链接:https://gist.github.com/fredjoseph/0d9eabb60860b12ba7b7adbfd4d0c558
然后,我们需要在应用程序启动时添加以下VM参数-Djava.awt.headless=false
我正在使用 IntelliJ:
我正在测试: