使用 Spring 启动和设置内存数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4671102/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 04:16:51  来源:igfitidea点击:

Start and setup in-memory DB using Spring

databasespringh2imdb

提问by martin

I'm writing a small demo application in Java using Spring, that needs to have access to a database. It should run on different machines and it would be far too much effort to setup a real database. Therefore I want to use an embedded one.

我正在使用 Spring 用 Ja​​va 编写一个小型演示应用程序,它需要访问数据库。它应该在不同的机器上运行,设置一个真正的数据库会花费太多精力。因此我想使用嵌入式。

The DB has a given schema (two tables) and some (very few) pre-defined entries. I'm looking for a simple way to start an in-memory database, create the tables and fill in the data. All of this should happen while initializing the Spring context.

DB 有一个给定的模式(两个表)和一些(很少)预定义的条目。我正在寻找一种简单的方法来启动内存数据库、创建表并填充数据。所有这些都应该在初始化 Spring 上下文时发生。

My approach would be to use H2 as my database and then maybe Spring Batch to load the data from csv- or xml-files. However I was hoping there might be an easier way to achieve this. Are there any databases/frameworks/tools that can do this out-of-the-box?

我的方法是使用 H2 作为我的数据库,然后使用 Spring Batch 从 csv 或 xml 文件加载数据。但是,我希望可能有一种更简单的方法来实现这一目标。是否有任何数据库/框架/工具可以开箱即用?

It would only take a few SQL-commands to set-up everything I need. I'm looking for a way to do this in a Spring-environment as simple as possible.

只需要几个 SQL 命令来设置我需要的一切。我正在寻找一种在 Spring 环境中尽可能简单地做到这一点的方法。

回答by axtavt

Spring has some built-in embedded database support, see embedded database support in the documentation.

Spring 有一些内置的嵌入式数据库支持,请参阅文档中的嵌入式数据库支持

回答by Thomas Mueller

With H2, you could initialize the database in the database URL itself. Example: you have a SQL script 'start.sql' that contains all the scripts to initialize. This can also include creating the tables from CSV file. Then use a database URL of the form jdbc:h2:~/temp/test;init=runscript from '~/temp/start.sql'. The start.sql could look like this (this is an example I'm working on anyway - it shows how to create tables from a CSV file):

使用 H2,您可以在数据库 URL 本身中初始化数据库。示例:您有一个 SQL 脚本“start.sql”,其中包含要初始化的所有脚本。这还可以包括从 CSV 文件创建表。然后使用表单的数据库 URL jdbc:h2:~/temp/test;init=runscript from '~/temp/start.sql'。start.sql 可能看起来像这样(无论如何,这是我正在处理的一个示例 - 它显示了如何从 CSV 文件创建表):

create table if not exists location(id int primary key, country varchar, 
region varchar, city varchar, postalCode varchar, latitude float, longitude float, 
metroCode varchar, areaCode varchar) 
as select * from csvread('~/Downloads/GeoLiteCity/GeoLiteCity-Location.csv');

create table if not exists blocks(start long, end long primary key, location int) 
as select * from csvread('~/Downloads/GeoLiteCity/GeoLiteCity-Blocks.csv');

create alias if not exists ip2id deterministic as $$
long ip2id(String s) {
  String[] x = s.split("\.");
  return (Long.parseLong(x[0]) << 24) + (Long.parseLong(x[1]) << 16) +
    (Long.parseLong(x[2]) << 8) + Long.parseLong(x[3]);
} $$;

create alias if not exists id2ip deterministic as $$
String id2ip(long x) {
  return (x >> 24) + "." + ((x >> 16) & 255) + "." + 
      ((x >> 8) & 255) + "." + (x & 255);
} $$;

回答by Aravind Yarram

Spring 3 added more support for embedded databases starting from 3 with the help of jdbc:embedded-databaseelement. Read this tutorialfor more information.

jdbc:embedded-database元素的帮助下,Spring 3 从 3 开始增加了对嵌入式数据库的更多支持。阅读本教程以获取更多信息。

I'd also recommend using Derby as it comes bundled with JDK 6.

我还建议使用 Derby,因为它与 JDK 6 捆绑在一起。

回答by hvgotcodes

HSQLDBis a good choice.

HSQLDB是一个不错的选择。