How to initialize in-memory HSQLDB using script via Spring

前端 未结 5 1497
死守一世寂寞
死守一世寂寞 2020-12-12 15:29

I am attempting to do unit testing of my DAO (using Spring and Hibernate). I am using HSQLDB per this tutorial. The tutorial states that the in-memory HSQLDB database can be

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-12 16:24

    If you are trying to work with in-memory databases and Spring, there is a new jdbc namespace for Spring 3 that makes working with embedded databases very easy.

    The best part is that it acts as a DataSource, so it can easily be dropped in to replace your existing dataSource bean.

    
        
        
    
    

    If you are more interested in doing this with Java Config, take a look at the EmbeddedDatabaseBuilder (new in Spring 3.0).

    @Configuration
    public class DatabaseTestConfig {
        @Bean
        public DataSource dataSource() {
            return new EmbeddedDatabaseBuilder()
                .setType(EmbeddedDatabaseType.HSQL)
                .addScript("classpath:schema.sql")
                .addScript("classpath:test-data.sql")
                .build();
        }
    }
    

提交回复
热议问题