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支持)