springboot中使用h2数据库(内存模式)

摘要:
插件>org.springframework.boot<spring boot maven插件<配置>/配置>/插件>**/*。tld<&书信电报;截图;importjavax.annotation.Resource;importorg.junit.runner.RunWith;

使用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>

截图:

springboot中使用h2数据库(内存模式)第1张

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);

}

 测试结果:

springboot中使用h2数据库(内存模式)第2张

免责声明:文章转载自《springboot中使用h2数据库(内存模式)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇(三) ffmpeg filter学习-编写自己的filterjava-中的代理下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

生物数据库与在线工具

生物数据库       生物数据库是收集自科学实验、出版文献、高通量实验技术和计算分析等生命科学信息库,它包含来自基因组学、蛋白质组学、代谢组学、微阵列基因表达和系统发育学等领域的信息。       生物数据库大致可分为序列、结构和功能数据库。序列数据库储存核酸和蛋白质序列;结构数据库储存RNA和蛋白质的结构信息;功能数据库提供关于基因产物的生理作用信息(...

C# 创建ACCESS数据库(转载)

文章转自 http://www.cnblogs.com/luohoufu/archive/2008/04/10/1147136.html C#中怎么创建ACCESS数据库文件microsoft ado ext.2.8   我想利用C#创建一个ACCESS数据库文件(A.mdb)。请问用什么办法可以实现。 A.mdb文件是原来没有的,程序需要创建一个然后往里...

如何在oracle中导入dmp数据库文件

如何在oracle中导入dmp数据库文件 --如何在oracle中导入dmp数据库文件                          Oracle数据导入导出imp/exp就相当于oracle数据还原与备份。exp命令可以把数据从远程数据库服务器导出到本地的dmp文件,imp命令可以把dmp文件从本地导入到远处的数据库服务器中。 利用这个功能可以构...

浅尝key-value数据库(二)——MongoDB的优与劣

浅尝key-value数据库(二)——MongoDB的优与劣 MongoDB的名字取自英文单词"humongous"的中间五个字母,是一个C++开发的基于分布式文件存储的数据库开源项目。他的文件存储格式是BSON(Binary JSON),因此可以高效存储二进制数据,例如图像、视频等大对象。 由于我是CentOS x86_64的系统,于是安装MongoD...

SuperSlide——再次接触

原来自己很早之前就接触过SuperSlider这个神奇的插件,原谅自己又得了“健忘症”,因此昨天就把自己“坑”了一把: 前言: 交易一期的项目即将上线,在解决测试同事们提出的bug的时候,无意间看到页面有一个神奇的现象: 看着这左右不停变换的图片,心里是不是有点高兴:这样的效果还真不错!可是头疼的却在后面…… 当企业资质下只有一张图片时,它就变成了两张...

前端缓存http请求

需求: 1、 重复的请求,使用缓存 2、 不重复的请求,允许发送 3、 连续两次重复的发送,两次返回的结果是一样的,且第二次不发送请求 1、搭建前端服务 vue-cli 一步到位  <template> <div class="hello"> <button v-on:click="getrs(1)">...