H2 Database入门

简介: H2 Database做为轻量级的内嵌数据库,功能十分强大,而且运行时只需要一个jar包即可,下表是官网的描述: 更详细的对比见官网页面: http://www.h2database.com/html/features.

H2 Database做为轻量级的内嵌数据库,功能十分强大,而且运行时只需要一个jar包即可,下表是官网的描述:

更详细的对比见官网页面: http://www.h2database.com/html/features.html#comparison

 

下面是开发入门步骤:

一、maven中添加依赖项

1         <dependency>
2             <groupId>com.h2database</groupId>
3             <artifactId>h2</artifactId>
4             <version>1.3.176</version>            
5         </dependency>    

 

二、Spring配置文件

a) 数据源配置

 1     <bean id="dataSource" class="org.h2.jdbcx.JdbcConnectionPool"
 2         destroy-method="dispose">
 3         <constructor-arg>
 4             <bean class="org.h2.jdbcx.JdbcDataSource">
 5                 <!-- 内存模式 -->
 6                 <property name="URL" value="jdbc:h2:mem:test" />
 7                 <!-- 文件模式 -->
 8                 <!-- <property name="URL" value="jdbc:h2:testRestDB" /> -->                 
 9                 <property name="user" value="sa" />
10                 <property name="password" value="sa" />
11             </bean>
12         </constructor-arg>
13     </bean>

注:密码不要留空(可以根据需要修改成其它值),否则运行时会报" 用户名、密码错之类"的Error

b) 初始化数据库脚本

1     <!-- 初始化数据表结构 -->
2     <jdbc:initialize-database data-source="dataSource"
3         ignore-failures="ALL">
4         <jdbc:script location="classpath:database/h2/create-table.sql" />
5         <jdbc:script location="classpath:database/h2/import-data.sql" />
6     </jdbc:initialize-database>

配置这个以后,每次应用启动前,会先执行create-table.sql,然后再执行import-data.sql,十分方便测试前,先初始化db环境

 

c) hibernate支持

 1     <bean id="sessionFactory"
 2         class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
 3         <property name="dataSource" ref="dataSource" />
 4         <property name="packagesToScan">
 5             <list>
 6                 <value>com.cnblogs.yjmyzz.entity</value>
 7             </list>
 8         </property>
 9         <property name="mappingLocations">
10             <list>
11                 <value>classpath*:hibernate/**/*.hbm.xml</value>
12             </list>
13         </property>
14         <property name="hibernateProperties">
15             <props>
16                 <prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
17                 <!-- <prop key="current_session_context_class">thread</prop> -->
18                 <prop key="hibernate.format_sql">true</prop>
19                 <prop key="hibernate.show_sql">true</prop>
20                 <prop key="hibernate.use_sql_comments">true</prop>
21                 <prop key="hibernate.max_fetch_depth">3</prop>
22                 <prop key="hibernate.jdbc.batch_size">20</prop>
23                 <prop key="hibernate.jdbc.fetch_size">20</prop>
24                 <prop key="hibernate.cache.use_second_level_cache">false</prop>
25             </props>
26         </property>
27     </bean>

关键是16行,数据库方言的设置

 

d)mybatis分页插件设置

 1     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
 2         <property name="dataSource" ref="dataSource" />
 3         <property name="configLocation" value="classpath:mybatis-config.xml"></property>
 4         <property name="typeAliasesPackage" value="com.cnblogs.yjmyzz.entity"></property>
 5         <property name="mapperLocations" value="classpath:mybatis/**/*.xml"></property>
 6         <property name="plugins">
 7             <list>
 8                 <bean
 9                     class="com.github.miemiedev.mybatis.paginator.OffsetLimitInterceptor">
10                     <property name="dialectClass"
11                         value="com.github.miemiedev.mybatis.paginator.dialect.H2Dialect"></property>
12                 </bean>
13             </list>
14         </property>
15     </bean>

关键是11行,方言的设置

 

三、代码测试:

 1     @Test
 2     public void testConnH2() throws SQLException, IOException {
 3         ApplicationContext ctx = new ClassPathXmlApplicationContext(
 4                 "spring-datasource.xml");
 5         OrderMapper orderMapper = ctx.getBean(OrderMapper.class);
 6         List<TOrder> orders = orderMapper.getAllOrder();
 7         for (TOrder o : orders) {
 8             System.out.println(o.getClientname());
 9         }
10         ((ClassPathXmlApplicationContext) ctx).close();
11 
12     }

上面演示了mybatis结合H2的使用示例

 

四、控制台界面

H2还提供了一个可视化界面

Windows开始菜单->H2->H2 console,点击connect连接就行了

有不熟悉的函数啥的,直接在SQL输入区,输入 help,然后run,下面就有帮助信息 

完整的示例代码下载  (基于struts2的rest-plugin showcase,增加了mybatis3 + hibernate4 + spring4 + velocity支持)

目录
相关文章
|
SQL XML 网络协议
Liquibase----XML格式通过update更新H2 Database数据库
Liquibase----XML格式通过update更新H2 Database数据库
263 0
Liquibase----XML格式通过update更新H2 Database数据库
|
网络协议 Java 数据库连接
java:如何用代码控制H2 Database启动
1、纯手动start/stop 1 package com.cnblogs.yjmyzz.h2; 2 3 import java.sql.Connection; 4 import java.
1052 0
|
6月前
|
SQL Oracle 关系型数据库
WARNING: Too Many Parse Errors With error=911 When Running a JDBC Application Connected to an Oracle 19c database
WARNING: Too Many Parse Errors With error=911 When Running a JDBC Application Connected to an Oracle 19c database (
88 2
|
6月前
|
Oracle 关系型数据库
19c 开启Oracle Database Vault
19c 开启Oracle Database Vault
161 1
|
6月前
|
SQL Oracle 关系型数据库
Connect to Autonomous Database Using Oracle Database Tools
Connect to Autonomous Database Using Oracle Database Tools
60 1
|
5月前
|
Oracle 关系型数据库 Linux
Requirements for Installing Oracle Database/Client 19c on OL8 or RHEL8 64-bit (x86-64) (Doc ID 2668780.1)
Requirements for Installing Oracle Database/Client 19c on OL8 or RHEL8 64-bit (x86-64) (Doc ID 2668780.1)
45 0
|
6月前
|
人工智能 Oracle 关系型数据库
一篇文章弄懂Oracle和PostgreSQL的Database Link
一篇文章弄懂Oracle和PostgreSQL的Database Link
|
6月前
|
SQL Oracle 安全
Oracle Database Vault Access Control Components
Oracle Database Vault Access Control Components
52 0
|
6月前
|
Oracle 安全 关系型数据库
What Is Oracle Database Vault?
The Oracle Database Vault security controls protect application data from unauthorized access, and helps you to comply with privacy and regulatory requirements. You can deploy controls to block privileged account access to application data and control sensitive operations inside the database using
41 0
|
6月前
|
Oracle 关系型数据库 Linux
服务器Centos7 静默安装Oracle Database 12.2
服务器Centos7 静默安装Oracle Database 12.2
263 0