简体   繁体   English

如何用真实数据库表填充h2内存表?

[英]How to populate h2 memory table with a Real Database Table?

I hope i can explain this in a proper way. 我希望我能以适当的方式解释一下。

I have a Sql Server Database with serveral tables. 我有一个带有服务器表的Sql Server数据库。 In my Spring - Boot application,i need to query to those tables,there are a couple of tables that wont change ever. 在我的Spring-Boot应用程序中,我需要查询这些表,有几个表永远不会改变。

For example,lets suppose for simplicity it sa Book table,and i query the books in different ways,by price,by id,by ISBN ,what ever. 例如,为了简单起见,让我们假设它是一个Book table,而我以不同的方式,通过价格,id,ISBN等等查询书籍。

So first thing i thought ,it s to use Redis,but that s not good for me.Why? 所以我首先想到的是使用Redis,但这对我不利。为什么?

Well,Redis stores the data in a key-value pair,so if i catch the entire table,then in my code when i want to do same complex query to that data (Give me the book with this ISBN),that s not possible,i will need to create different Catched methods for each operations,so the next time i ask for same data ,if exist in the cache,it wont go to query to the Database. 好吧,Redis将数据存储在一个键值对中,因此,如果我捕获了整个表,那么当我想对数据进行相同的复杂查询时(在我的书中附上该ISBN),那么在我的代码中就不可能了,我将需要为每个操作创建不同的Catched方法,因此,下次我请求相同的数据时,如果缓存中存在该数据,则不会去查询数据库。

Thats why i though about H2 memory database,i could fill my "Book" in memory table and make all kind of operation on it,because it works like a normal database and i could query anything. 那就是为什么我虽然关于H2内存数据库,却可以在内存表中填写我的“书”并对其进行各种操作,因为它的工作方式类似于普通数据库,并且我可以查询任何内容。

Now,comes my questiion: 现在,提出我的问题:

I know i can load initial data into H2 pointing to some script,that s ok. 我知道我可以将指向一些脚本的初始数据加载到H2中,这样就可以了。 But i would like to populate my "Book" in memory database,with the same data that i already have in my SQL Server Book table. 但是我想用我在SQL Server Book表中已经拥有的相同数据填充内存数据库中的“ Book”。

What do you think? 你怎么看? Is that possible?Any ideas? 有可能吗?有什么想法吗?

Thanks in advance!! 提前致谢!!

You could implement CommandLineRunner or ApplicationRunner for data initialization https://dzone.com/articles/spring-boot-applicationrunner-and-commandlinerunne 您可以实现CommandLineRunner或ApplicationRunner进行数据初始化https://dzone.com/articles/spring-boot-applicationrunner-and-commandlinerunne

(Also script.sql in resources with "spring.datasource.data=script.sql" property could be used for data load) (还可以将资源中具有“ spring.datasource.data = script.sql”属性的script.sql用于数据加载)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM