服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - springboot配置内存数据库H2教程详解

springboot配置内存数据库H2教程详解

2020-11-28 14:55zhanglf02 Java教程

这篇文章主要介绍了springboot配置内存数据库H2的详细教程,需要的朋友可以参考下

业务背景:因soa系统要供外网访问,处于安全考虑用springboot做了个前置模块,用来转发外网调用的请求和soa返回的应答。其中外网的请求接口地址在db2数据库中对应专门的一张表来维护,要是springboot直接访问数据库,还要专门申请权限等,比较麻烦,而一张表用内置的h2数据库维护也比较简单,就可以作为替代的办法。

环境:springboot+maven3.3+jdk1.7

1.springboot的maven工程结构

springboot配置内存数据库H2教程详解

说明一下,resource下的templates文件夹没啥用。我忘记删掉了。。。

2. 首先引入依赖jar包 pom.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<!--?xml version="1.0" encoding="utf-8"?-->
<project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelversion>4.0.0</modelversion>
 <groupid>com.zlf</groupid>
 spring-boot</artifactid>
 <version>1.0-snapshot</version>
 <!-- 增加父pom ,spring-boot-starter-parent包含了大量配置好的依赖管理,他是个特殊的starter,它提供了有用的maven默认设置 -->
 <parent>
  <groupid>org.springframework.boot</groupid>
  spring-boot-starter-parent</artifactid>
  <version>1.4.3.release</version>
 </parent>
 <!-- spring默认使用jdk1.6,如果你想使用jdk1.8,你需要在pom.xml的属性里面添加java.version,如下: -->
 <properties>
  <project.build.sourceencoding>utf-8</project.build.sourceencoding>
  <tomcat.version>7.0.72</tomcat.version>
  <java.version>1.8</java.version>
 </properties>
 <!-- spring通过添加spring-boot-starter-*这样的依赖就能支持具体的某个功能。 -->
 <!-- 我们这个示例最终是要实现web功能,所以添加的是这个依赖。 -->
 <dependencies>
  <dependency>
   <!-- 指定为web应用,并启动一个内嵌的servlet容器(默认是tomcat)用于处理http请求 -->
   <groupid>org.springframework.boot</groupid>
   spring-boot-starter-web</artifactid>
  </dependency>
  <!-- 对java 持久化api的支持,包括spring-data-jap,spring-orm,hibernate-->
  <dependency>
   <groupid>org.springframework.boot</groupid>
   spring-boot-starter-data-jpa</artifactid>
  </dependency>
  <!-- lombok插件,方便model对象的处理 -->
  <dependency>
   <groupid>org.projectlombok</groupid>
   lombok</artifactid>
  </dependency>
  <!-- 内嵌数据库 -->
  <dependency>
   <groupid>com.h2database</groupid>
   h2</artifactid>
  </dependency>
  <!-- mysql驱动 -->
<!--  <dependency> -->
<!--   <groupid>mysql</groupid> -->
<!--   mysql-connector-java</artifactid> -->
<!--  </dependency> -->
  <dependency>
   <groupid>junit</groupid>
   junit</artifactid>
   <scope>test</scope>
  </dependency>
<!--  <dependency> -->
<!--   <groupid>javax.servlet</groupid> -->
<!--   jstl</artifactid> -->
<!--  </dependency> -->
 </dependencies>
 <build>
  <!-- 打包后的jar包名称 -->
  <finalname>example</finalname>
  <plugins>
   <plugin>
    <groupid>org.springframework.boot</groupid>
    <!-- 必须要的springboot继承的maven插件,缺少了无法打包jar。 -->
    spring-boot-maven-plugin</artifactid>
    <dependencies>
     <!-- 在我们开发过程中,我们需要经常修改,为了避免重复启动项目,我们可以启用热部署。 spring-loaded项目提供了强大的热部署功能,
      添加/删除/修改 方法/字段/接口/枚举 等代码的时候都可以热部署,速度很快,很方便。 想在spring boot中使用该功能非常简单 ,就是在spring-boot-maven-plugin插件下面添加依赖: -->
     <dependency>
      <groupid>org.springframework</groupid>
      springloaded</artifactid>
      <version>1.2.5.release</version>
     </dependency>
    </dependencies>
   </plugin>
  </plugins>
 </build>
</project>

3.在src/main/resource根目录下进行配置h2数据库。

schema.sql中建表。可以见多个表。用分号隔开

?
1
2
3
4
5
create table staff(
id char(20) not null primary key,
name char(20),
age integer
);

data.sql 为新建的表进行初始化数据的操作。可以放入多个表的插入语句。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
insert into staff values(
 's01',
 '张三',
 26
);
insert into staff values(
 's02',
 '春天里asdglkj',
 23
);
insert into staff values(
 's03',
 '刘三',
 26
);
insert into staff values(
 's04',
 '万里高空',
 26
);
insert into staff values(
 's05',
 '火影',
 26
);
insert into staff values(
 's06',
 'xiaopang',
 26
);
insert into staff values(
 's07',
 '海贼王',
 26
);
insert into staff values(
 's08',
 '王者荣耀',
 26
)

application.properties db2数据库设置和控制台现实设置等。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#spring.datasource.url = jdbc:mysql://localhost:3306/zhanglfdatabase
#spring.datasource.username = root
#spring.datasource.password =
#spring.datasource.driverclassname = com.mysql.jdbc.driver
#数据库支持多种连接模式和连接设置,不同的连接模式和连接设置是通过不同的url来区分的,url中的设置是不区分大小写。内存数据库(私有)
#jdbc:h2:mem:
#内存数据库(被命名)
#jdbc:h2:mem:<databasename>
#jdbc:h2:mem:test_mem
spring.datasource.url =jdbc:h2:mem:soa_service_api
spring.datasource.username = root
spring.datasource.password = root
spring.datasource.driverclassname = org.h2.driver
#进行该配置后,每次启动程序,程序都会运行resources/schema.sql文件,对数据库的结构进行操作,相当于新建一个表。
spring.datasource.schema=classpath:schema.sql
#进行该配置后,每次启动程序,程序都会运行resources/data.sql文件,对数据库的数据操作,相当于往表中插入数据。
spring.datasource.data=classpath:data.sql
# 数据库类型声明
spring.jpa.database = h2
# 是否开启查询语句在控制台打印
spring.jpa.show-sql = true
# hibernate ddl auto (create, create-drop, update)
spring.jpa.hibernate.ddl-auto = update
# naming strategy
#spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.improvednamingstrategy
#开启h2控制台功能和访问地址。
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console</databasename>
这里有必要强调h2数据库设置的属性:
spring.datasource.url =jdbc:h2:mem:soa_service_api

因为数据库支持多种连接模式和连接设置,不同的连接模式和连接设置是通过不同的url来区分的,url中的设置是不区分大小写。其中几种常用的设置如下图

springboot配置内存数据库H2教程详解

前两种对应的效果是:

1. jdbc:h2:file:e:/data/h2 表示将初始化的数据和h2 console控制台执行的数据保存到e盘下data/h2文件夹中,即使应用重启,数据不会丢失。

2. jdbc:h2:~/testdatabase这里就需要说明一下”~”这个符号在window操作系统下代表什么意思了,在window操作系统下,”~”这个符号代表的就是当前登录到操作系统的用户对应的用户目录,所以testdatabase数据库对应的文件存放在登录到操作系统的用户对应的用户目录当中,比如我当前是使用administrator用户登录操作系统的,所以在”c:\documents and settings\administrator.h2”目录中就可以找到test数据库对应的数据库文件了

持久化本地的问题:由于本地已经存在表,而应用每次启动都会创建表,导致下次启动时会启动报错。除非手动注掉application.properties中新建表的配置,或则删除本地对应目录的文件。

3.jdbc:h2:mem:soa_service_api、jdbc:h2:mem:~/.h2/url类似与这种配置的,表示将初始化和h2 console控制台上操作的数据保存在内存(mem-memory)

保存到内存的问题:由于每次重启应用内存释放掉后,对应的数据也会消失,当然初始化的表+初始化数据就都没了。然后重启会从data.sql中重新初始化数据,启动正常。但是你通过h2
console操作的其他数据则全部丢失。解决办法是把在h2 console新添加的接口地址配置到data.sql中。然后重新启动才行。

4.h2的配置说完,下面开始进行h2的测试代码java文件部分,看看有没有配置成功。我们用到jpa这个持久化的接口工具。

a.映射的pojo实体类-staffbo,注意点和说明都在代码里了。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.zlf.bo;
import java.io.serializable;
import javax.persistence.column;
import javax.persistence.entity;
import javax.persistence.id;
import javax.persistence.table;
import lombok.data;
@data//可以省略get+set方法。
@entity
@table(name="staff")//注解的name属性值要和schema.sql定义的表名一致。不然启动虽然不报错,但和表映射不上,就取不到任何值。
public class staffbo implements serializable {
 private static final long serialversionuid = 1l;
 //主键是必须声明的。不然启动会报实体中无定义主键的错: no identifier specified for entity: com.zlf.bo.staffbo
 @id
 private string id;
// @column(name="name") 如果表中字段名称和这里的属性名称一样,可以不同加column注解。
 private string name;
 @column(name="age")
 private int age;
}

b.然后是类似与dao层接口的操作数据库的接口,staffrepository这个接口要继承pagingandsortingrepository才能实现对数据库的crud操作。

?
1
2
3
4
5
6
package com.zlf.repository;
import org.springframework.data.repository.pagingandsortingrepository;
import com.zlf.bo.staffbo;
public interface staffrepository extends pagingandsortingrepository<staffbo,string> {
}
</staffbo,string>

c. 然后就是service层的接口和实现类,在实现类中注入api接口的实例,并用实例操作数据库,这里是h2数据库。

?
1
2
3
4
5
6
7
package com.zlf.service;
import java.util.list;
import com.zlf.bo.staffbo;
public interface istaffservice {
 public list<staffbo> queryallstafflist();
}
</staffbo>

这里我们只做了个查询所有的操作。然后希望在页面打印出来这些实体信息。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package com.zlf.service.impl;
import java.util.arraylist;
import java.util.iterator;
import java.util.list;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.stereotype.service;
import com.zlf.bo.staffbo;
import com.zlf.repository.staffrepository;
import com.zlf.service.istaffservice;
@service
public class staffserviceimpl implements istaffservice {
 @autowired
 private staffrepository staffrepository;
 @override
 public list<staffbo> queryallstafflist() {
  iterable<staffbo> iterable = staffrepository.findall();
  list<staffbo> list=new arraylist<staffbo>();
  iterator<staffbo> iterator = iterable.iterator();
  while(iterator.hasnext()){
   staffbo next = iterator.next();
   list.add(next);
  }
  return list;
 }
}
</staffbo></staffbo></staffbo></staffbo></staffbo>

d. controller层

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package com.zlf.controller;
import java.util.list;
import org.slf4j.logger;
import org.slf4j.loggerfactory;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.stereotype.controller;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.responsebody;
import com.zlf.bo.staffbo;
import com.zlf.service.istaffservice;
@controller
@requestmapping(path="/staff",produces="application/json;charset=utf-8")
public class staffcontroller {
 private static final logger logger=loggerfactory.getlogger(staffcontroller.class);
 @autowired
 private istaffservice staffservice;
 @requestmapping("/getlist")
 @responsebody
 public list<staffbo> getalllist(){
  list<staffbo> stafflist=null;
  try {
   stafflist = staffservice.queryallstafflist();
  } catch (exception e) {
   logger.error("查询失败");
  }
  return stafflist;
 }
}
</staffbo></staffbo>

e. 重点来了–应用的启动入口application.java

这个java类最好放到和其他层级同级的根目录中,这里就是com.zlf下,因为这个类的注解@springbootapplication会默认扫描与它同级目录的其他文件。这样才能完成注入等操作。如果你把它放到了和其他层的代码一样的级别中,则要用这种注解才行。

比如mianapplication下的application.java,就是我说的另一种放到和其他层同级的结构中的情形。

springboot配置内存数据库H2教程详解

它应该如何配置才能正常启动呢?

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package mainapplication;
import org.springframework.boot.springapplication;
import org.springframework.boot.autoconfigure.springbootapplication;
import org.springframework.context.annotation.componentscan;
import org.springframework.stereotype.controller;
import org.springframework.web.bind.annotation.requestmapping;
/**
 * spring boot建议将我们main方法所在的这个主要的配置类配置在根包名下。
 *
 * @author administrator
 *
 *
 * @springbootapplication是spring boot提供的注解,他相当于加上如下注解:
 * @configuration,表明application是一个spring的配置对象,用于配置spring应用上下文。
 * @enableautoconfiguration,spring boot会根据类路径(classpath)以及一些属性值来自动完成一些配置行为,例如:开发基于spring
 *  mvc的web应用,需要在配置中加上
 * @enablewebmvc直接来激活一些默认的web配置, 一旦spring boot发现运行时类路径上包含了 spring-webmvc
 *        依赖,它会自动的完成一个web应用的基本配置
 *        ——例如配置dispatcherservlet等等。
 * @componenscan告知spring应用从什么位置去发现spring构件(@component, @service,
 *              @configuration)等等
 */
@springbootapplication
@controller
// @restcontroller因为我们例子是写一个web应用,因此写的这个注解,这个注解相当于同时添加@controller和@responsebody注解
// @enableautoconfiguration// spring boot会自动根据你jar包的依赖来自动配置项目的数据源依赖
@componentscan(basepackages = { "controller", "service", "dao" })
// @componentscan路径被默认设置为samplecontroller的同名package,也就是该package下的所有@controller
// ,@service , @component, @repository都会被实例化后并加入spring context中。这也是为什么要把这个类最好放到与其他包同级目录 的原因了。
public class application {
 @requestmapping("")
 public string home() {
  // return "xiaozhang ,hello world!";
  return "/index";
 }
 public static void main(string[] args) {
  // 启动spring boot项目最简单的方法就是执行下面的方法
  springapplication.run(application.class);
 }
}

这样就完成了测试代码的简单开发。在application.java中右键run as java application ,启动程序。效果如下

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
. ____   _   __ _ _
 /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/ ___)| |_)| | | | | || (_| | ) ) ) )
 ' |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: spring boot ::  (v1.4.3.release)
2017-07-05 20:50:07.579 info 5956 --- [   main] com.zlf.application      : starting application on pc-itwzhanglf02 with pid 5956 (e:\workspace\tsqa_springboot2_hibernate_jpa\target\classes started by administrator in e:\workspace\tsqa_springboot2_hibernate_jpa)
2017-07-05 20:50:07.579 info 5956 --- [   main] com.zlf.application      : no active profile set, falling back to default profiles: default
2017-07-05 20:50:07.657 info 5956 --- [   main] ationconfigembeddedwebapplicationcontext : refreshing org.springframework.boot.context.embedded.annotationconfigembeddedwebapplicationcontext@50103bfb: startup date [wed jul 05 20:50:07 cst 2017]; root of context hierarchy
2017-07-05 20:50:09.655 info 5956 --- [   main] trationdelegate$beanpostprocessorchecker : bean 'org.springframework.transaction.annotation.proxytransactionmanagementconfiguration' of type [class org.springframework.transaction.annotation.proxytransactionmanagementconfiguration$$enhancerbyspringcglib$$c3c549d9] is not eligible for getting processed by all beanpostprocessors (for example: not eligible for auto-proxying)
2017-07-05 20:50:10.094 info 5956 --- [   main] s.b.c.e.t.tomcatembeddedservletcontainer : tomcat initialized with port(s): 8080 (http)
2017-07-05 20:50:10.110 info 5956 --- [   main] o.apache.catalina.core.standardservice : starting service tomcat
2017-07-05 20:50:10.110 info 5956 --- [   main] org.apache.catalina.core.standardengine : starting servlet engine: apache tomcat/7.0.72
2017-07-05 20:50:10.297 info 5956 --- [ost-startstop-1] org.apache.catalina.startup.tldconfig : at least one jar was scanned for tlds yet contained no tlds. enable debug logging for this logger for a complete list of jars that were scanned but no tlds were found in them. skipping unneeded jars during scanning can improve startup time and jsp compilation time.
2017-07-05 20:50:10.297 info 5956 --- [ost-startstop-1] o.a.c.c.c.[tomcat].[localhost].[/]  : initializing spring embedded webapplicationcontext
2017-07-05 20:50:10.297 info 5956 --- [ost-startstop-1] o.s.web.context.contextloader   : root webapplicationcontext: initialization completed in 2655 ms
2017-07-05 20:50:10.540 info 5956 --- [ost-startstop-1] o.s.b.w.servlet.servletregistrationbean : mapping servlet: 'dispatcherservlet' to [/]
2017-07-05 20:50:10.540 info 5956 --- [ost-startstop-1] o.s.b.w.servlet.servletregistrationbean : mapping servlet: 'webservlet' to [/h2-console/*]
2017-07-05 20:50:10.540 info 5956 --- [ost-startstop-1] o.s.b.w.servlet.filterregistrationbean : mapping filter: 'characterencodingfilter' to: [/*]
2017-07-05 20:50:10.540 info 5956 --- [ost-startstop-1] o.s.b.w.servlet.filterregistrationbean : mapping filter: 'hiddenhttpmethodfilter' to: [/*]
2017-07-05 20:50:10.540 info 5956 --- [ost-startstop-1] o.s.b.w.servlet.filterregistrationbean : mapping filter: 'httpputformcontentfilter' to: [/*]
2017-07-05 20:50:10.540 info 5956 --- [ost-startstop-1] o.s.b.w.servlet.filterregistrationbean : mapping filter: 'requestcontextfilter' to: [/*]
2017-07-05 20:50:10.961 info 5956 --- [   main] o.s.jdbc.datasource.init.scriptutils  : executing sql script from class path resource [schema.sql]
2017-07-05 20:50:10.976 info 5956 --- [   main] o.s.jdbc.datasource.init.scriptutils  : executed sql script from class path resource [schema.sql] in 15 ms.
2017-07-05 20:50:10.992 info 5956 --- [   main] o.s.jdbc.datasource.init.scriptutils  : executing sql script from class path resource [data.sql]
2017-07-05 20:50:10.992 info 5956 --- [   main] o.s.jdbc.datasource.init.scriptutils  : executed sql script from class path resource [data.sql] in 0 ms.
2017-07-05 20:50:11.118 info 5956 --- [   main] j.localcontainerentitymanagerfactorybean : building jpa container entitymanagerfactory for persistence unit 'default'
2017-07-05 20:50:11.133 info 5956 --- [   main] o.hibernate.jpa.internal.util.loghelper : hhh000204: processing persistenceunitinfo [
 name: default
 ...]
2017-07-05 20:50:11.227 info 5956 --- [   main] org.hibernate.version     : hhh000412: hibernate core {5.0.11.final}
2017-07-05 20:50:11.227 info 5956 --- [   main] org.hibernate.cfg.environment   : hhh000206: hibernate.properties not found
2017-07-05 20:50:11.227 info 5956 --- [   main] org.hibernate.cfg.environment   : hhh000021: bytecode provider name : javassist
2017-07-05 20:50:11.273 info 5956 --- [   main] o.hibernate.annotations.common.version : hcann000001: hibernate commons annotations {5.0.1.final}
2017-07-05 20:50:11.398 info 5956 --- [   main] org.hibernate.dialect.dialect   : hhh000400: using dialect: org.hibernate.dialect.h2dialect
2017-07-05 20:50:11.793 info 5956 --- [   main] org.hibernate.tool.hbm2ddl.schemaupdate : hhh000228: running hbm2ddl schema update
2017-07-05 20:50:11.840 info 5956 --- [   main] j.localcontainerentitymanagerfactorybean : initialized jpa entitymanagerfactory for persistence unit 'default'
2017-07-05 20:50:12.536 info 5956 --- [   main] s.w.s.m.m.a.requestmappinghandleradapter : looking for @controlleradvice: org.springframework.boot.context.embedded.annotationconfigembeddedwebapplicationcontext@50103bfb: startup date [wed jul 05 20:50:07 cst 2017]; root of context hierarchy
2017-07-05 20:50:12.626 info 5956 --- [   main] s.w.s.m.m.a.requestmappinghandlermapping : mapped "{[]}" onto public java.lang.string com.zlf.application.home()
2017-07-05 20:50:12.629 info 5956 --- [   main] s.w.s.m.m.a.requestmappinghandlermapping : mapped "{[/staff/getlist],produces=[application/json;charset=utf-8]}" onto public java.util.list<com.zlf.bo.staffbo> com.zlf.controller.staffcontroller.getalllist()
2017-07-05 20:50:12.631 info 5956 --- [   main] s.w.s.m.m.a.requestmappinghandlermapping : mapped "{[/error]}" onto public org.springframework.http.responseentity<java.util.map<java.lang.string, java.lang.object="">> org.springframework.boot.autoconfigure.web.basicerrorcontroller.error(javax.servlet.http.httpservletrequest)
2017-07-05 20:50:12.632 info 5956 --- [   main] s.w.s.m.m.a.requestmappinghandlermapping : mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.modelandview org.springframework.boot.autoconfigure.web.basicerrorcontroller.errorhtml(javax.servlet.http.httpservletrequest,javax.servlet.http.httpservletresponse)
2017-07-05 20:50:12.670 info 5956 --- [   main] o.s.w.s.handler.simpleurlhandlermapping : mapped url path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.resourcehttprequesthandler]
2017-07-05 20:50:12.670 info 5956 --- [   main] o.s.w.s.handler.simpleurlhandlermapping : mapped url path [/**] onto handler of type [class org.springframework.web.servlet.resource.resourcehttprequesthandler]
2017-07-05 20:50:12.733 info 5956 --- [   main] o.s.w.s.handler.simpleurlhandlermapping : mapped url path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.resourcehttprequesthandler]
2017-07-05 20:50:13.232 info 5956 --- [   main] o.s.j.e.a.annotationmbeanexporter  : registering beans for jmx exposure on startup
2017-07-05 20:50:13.310 info 5956 --- [   main] s.b.c.e.t.tomcatembeddedservletcontainer : tomcat started on port(s): 8080 (http)
2017-07-05 20:50:13.310 info 5956 --- [   main] com.zlf.application      : started application in 6.213 seconds (jvm running for 6.478)
</java.util.map<java.lang.string,></com.zlf.bo.staffbo>

打开浏览器,访问地址:http://localhost:8080/staff/getlist,可以看到初始化的数据都出来了。

springboot配置内存数据库H2教程详解

然后访问地址:http://localhost:8080/h2-console 出现下面的h2 console界面

springboot配置内存数据库H2教程详解

在登陆页面输入在application.properties中配置的h2数据库信息,登陆后可以看到左侧已经有我们初始化的表,查询数据,也能看到数据应初始化进来。则证明成功了!

springboot配置内存数据库H2教程详解

以上所述是小编给大家介绍的springboot配置内存数据库h2教程详解,希望对大家有所帮助!

原文链接:http://www.2cto.com/database/201707/654694.html

延伸 · 阅读

精彩推荐