springboot中使用h2數據庫(內存模式)


使用H2的優點,不需要裝有服務端和客戶端,在項目中包含一個jar即可,加上初始化的SQL就可以使用數據庫了

在springboot中引入,我的版本是2.1.4,里面就包含有h2的版本控制

        <!-- 集成h2數據庫 -->
        <dependency>
           <groupId>com.h2database</groupId>
           <artifactId>h2</artifactId>
           <scope>runtime</scope>
        </dependency>

在pom文件中,一般我都包含了下面一段

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <fork>true</fork>
                </configuration>
            </plugin>
        </plugins>
        
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/**</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                    <include>**/*.tld</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
    </build>

截圖:

h2數據庫的配置:application-h2.properties

#spring.datasource.url = jdbc:h2:file:~/.h2/testdb
spring.datasource.url=jdbc:h2:mem:activiti;DB_CLOSE_DELAY=1000
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password= 
spring.datasource.schema=classpath:db/schema.sql
spring.datasource.data=classpath:db/data.sql

如果數據文件有多個,使用逗號拼接就可以了,spring.datasource.data=classpath:db/data.sql,classpath:db/data2.sql

db/data.sql內容:

insert into mytest(name) values('TheoryDance');

schema.sql內容:

create table mytest(id int primary key auto_increment, name varchar(20) not null);

在測試類中添加一個測試方法

package com.grand.mysql_handler;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import com.grand.mysql_handler.mapper.SystemMapper;

@SpringBootTest
@RunWith(SpringRunner.class)
public class MyRestTest2 {
    
    @Resource
    private SystemMapper systemMapper;
    
    @Test
    public void testH2() {
        List<Map<String,Object>> list = systemMapper.selectBySql("select * from mytest");
        System.out.println(list);
    }
    
}

其中SysMapper.java內容如下(使用的Mybatis連接數據庫):

package com.grand.mysql_handler.mapper;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface SystemMapper {

    @Insert("${sql}")
    int insertBySql(@Param("sql")String sql);
    @Delete("${sql}")
    int deleteBySql(@Param("sql")String sql);
    @Update("${sql}")
    int updateBySql(@Param("sql")String sql);
    @Select("${sql}")
    List<Map<String,Object>> selectBySql(@Param("sql")String sql);
    @Select("${sql}")
    Map<String,Object> selectOneBySql(@Param("sql")String sql);

}

 測試結果:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM