﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-海水正蓝-随笔分类-H2</title><link>http://www.blogjava.net/xiaohuzi2008/category/53817.html</link><description>面朝大海，春暖花开</description><language>zh-cn</language><lastBuildDate>Wed, 14 Aug 2013 01:35:36 GMT</lastBuildDate><pubDate>Wed, 14 Aug 2013 01:35:36 GMT</pubDate><ttl>60</ttl><item><title>sping2+struts2+hibernate3项目整合H2数据库 </title><link>http://www.blogjava.net/xiaohuzi2008/archive/2013/08/14/402773.html</link><dc:creator>小胡子</dc:creator><author>小胡子</author><pubDate>Wed, 14 Aug 2013 01:33:00 GMT</pubDate><guid>http://www.blogjava.net/xiaohuzi2008/archive/2013/08/14/402773.html</guid><wfw:comment>http://www.blogjava.net/xiaohuzi2008/comments/402773.html</wfw:comment><comments>http://www.blogjava.net/xiaohuzi2008/archive/2013/08/14/402773.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/xiaohuzi2008/comments/commentRss/402773.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/xiaohuzi2008/services/trackbacks/402773.html</trackback:ping><description><![CDATA[<div><p>大家可以参考下这个网站http://eoffice.im.fju.edu.tw/phpbb/viewtopic.php?p=28685<br /> </p> <p><br /> </p> 1.先启动项目上的h2/bin下的h2.bat或h2w.bat文件，把h2数据库启动起来<br /> <br /> 2.SSH2框架和h2数据库整合方法<br /> 2.1先在数据库下创建 schema目录(相当于一个数据库实例)<br /> <p>&nbsp; create schema fdrkftcode</p> <p>目的是解决这种异常<span style="color:red"><span style="font-weight:bold">org.h2.jdbc.JdbcSQLException: Schema "fdrkftcode" not found; ...</span></span><br /> </p> 2.2在schema目录下创建表，如创建系统用户表admin<br /> &nbsp; create table fdrkftcode.admin(<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; id int primary key,<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; adminname varchar(50),<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; username varchar(50),<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; userpwd varchar(50),<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; adminrights varchar(50),<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; createdate datetime,<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; usedtimes int,<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; lastlogin datetime,<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; curstatus int,<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; remark varchar(200)<br /> &nbsp; )<br /> &nbsp; <br /> 3.为了使用hibernate操作h2，需要作如下设置，在sql编辑窗口输入下面这些脚本<br /> 对于实体pojo对象的映射，我是用的annotation，关键是id主键的映射，如下：<br /> @Column(name = "ID", nullable = false)<br /> <br /> @Id<br /> <br /> @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "ABC_ID_SEQ")<br /> <br /> @SequenceGenerator(name = "ABC_ID_SEQ", sequenceName = "ABC_ID_SEQ")<br /> <br /> protected Long id;<br /> <br /> &nbsp;注意这里的GeneratedValue和SequenceGenerator的使用，这属于JPA规范，全部来自javax.persisten<br /> <br /> 4.配置applicationContext.xml文件,主要有三个地方要注意：<br /> 4.1修改连接数据库的JDBC驱动 driverClass的值为org.h2.Driver<br /> 4.2修改连接数据库所用的URL字符串 jdbcUrl的值为jdbc:h2:tcp://localhost/~/FDRKFTCODE;MODE=MySQL;AUTO_SERVER=TRUE<br /> 4.3修改Hibernate的数据库方言hibernate.dialect为org.hibernate.dialect.H2Dialect<br /> <br /> 5.h2数据库一些常用操作<br /> 5.1帮助命令help<br /> 5.2表中某字段重命名&nbsp; ALTER TABLE&nbsp; fdrkftcode.admin ALTER COLUMN usepwd rename to userpwd<br /> 5.3表中新增字段&nbsp; ALTER TABLE fdrkftcode.admin ADD IF NOT EXISTS abc varchar(50) <br /> 5.4表中删除字段&nbsp; ALTER TABLE fdrkftcode.admin DROP COLUMN IF EXISTS abc<br /> 5.5查找表中记录 SELECT * from fdrkftcode.admin<br /> 5.6往表中插入记录 INSERT INTO fdrkftcode.admin VALUES  (1,'管理员','admin','admin','10000000000000000000','2013-05-1  00:12:34',3,'2013-05-1 15:32:57',1,'超过级管理员')<br /> 5.7修改表中某记录 UPDATE fdrkftcode.admin SET fdrkftcode.admin.adminname='超级管理员' where fdrkftcode.admin.id=1<br /> <p>5.8删除表中某记录 DELETE FROM fdrkftcode.admin WHERE fdrkftcode.admin.id=1</p> <p><br /> </p> <p>6.下面是我项目的applicationContext.xml配置方法，大家可以参考下</p> &lt;?xml version="1.0" encoding="UTF-8"?&gt;<br /> &lt;beans<br /> &nbsp;&nbsp; &nbsp;xmlns="http://www.springframework.org/schema/beans"<br /> &nbsp;&nbsp; &nbsp;xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"<br /> &nbsp;&nbsp; &nbsp;xmlns:p="http://www.springframework.org/schema/p"<br /> &nbsp;&nbsp; &nbsp;xsi:schemaLocation="http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"&gt;<br /> &nbsp;&nbsp; &nbsp;<br /> &nbsp;&nbsp; &nbsp;&lt;!-- 定义使用C3P0连接池的数据源 --&gt;<br /> &nbsp;&nbsp; &nbsp;&lt;bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"&gt;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&lt;!-- 指定连接数据库的JDBC驱动 --&gt;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&lt;property name="driverClass"&gt;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&lt;value&gt;org.h2.Driver&lt;/value&gt;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &lt;/property&gt;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&lt;!-- 连接数据库所用的URL --&gt;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&lt;property name="jdbcUrl"&gt;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&lt;value&gt;jdbc:h2:tcp://localhost/~/FDRKFTCODE;MODE=MySQL;AUTO_SERVER=TRUE&lt;/value&gt;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&lt;/property&gt;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&lt;!-- 连接数据库的用户名 --&gt;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&lt;property name="user"&gt;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&lt;value&gt;sa&lt;/value&gt;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&lt;/property&gt;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&lt;!-- 连接数据库的密码 --&gt;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&lt;property name="password"&gt;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&lt;value&gt;&lt;/value&gt;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&lt;/property&gt;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&lt;!-- 设置数据库连接池的最大连接数 --&gt;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&lt;property name="maxPoolSize"&gt;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&lt;value&gt;50&lt;/value&gt;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&lt;/property&gt;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&lt;!-- 设置数据库连接池的最小连接数 --&gt;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&lt;property name="minPoolSize"&gt;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&lt;value&gt;5&lt;/value&gt;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&lt;/property&gt;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&lt;!-- 设置数据库连接池的初始化连接数 --&gt;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&lt;property name="initialPoolSize"&gt;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&lt;value&gt;5&lt;/value&gt;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&lt;/property&gt;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&lt;!-- 设置数据库连接池的连接的最大空闲时间,单位为秒 --&gt;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&lt;property name="maxIdleTime"&gt;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&lt;value&gt;20&lt;/value&gt;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&lt;/property&gt;<br /> &nbsp;&nbsp; &nbsp;&lt;/bean&gt;<br /> &nbsp;&nbsp; &nbsp;<br /> &nbsp;&nbsp;&nbsp; &lt;!-- 定义Hibernate的SessionFactory --&gt;<br /> &nbsp;&nbsp;&nbsp; &lt;bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"&gt;<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;!-- 依赖注入上面定义的数据源dataSource --&gt;<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;property name="dataSource" ref="dataSource"/&gt;<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;!-- 注册Hibernate的ORM映射文件 --&gt;<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;property name="mappingResources"&gt;<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;list&gt;<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;value&gt;com/sungoal/ORM/Admin.hbm.xml&lt;/value&gt;<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/list&gt;<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/property&gt;<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;!-- 设置Hibernate的相关属性 --&gt;<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;property name="hibernateProperties"&gt;<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;props&gt;<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;!-- 设置Hibernate的数据库方言 --&gt;<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.H2Dialect&lt;/prop&gt;<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;!-- 设置Hibernate是否在控制台输出SQL语句，开发调试阶段通常设为true --&gt;<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;prop key="show_sql"&gt;true&lt;/prop&gt;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&lt;!-- 设置Hibernate一个提交批次中的最大SQL语句数 --&gt;<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;prop key="hibernate.jdbc.batch_size"&gt;50&lt;/prop&gt;<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/props&gt;<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/property&gt;<br /> &nbsp;&nbsp;&nbsp; &lt;/bean&gt;<br /> &nbsp;&nbsp; &nbsp;<br /> &nbsp;&nbsp;&nbsp;&nbsp; &lt;!--定义Hibernate的事务管理器HibernateTransactionManager --&gt;<br /> &nbsp;&nbsp;&nbsp; &lt;bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt;<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;!-- 依赖注入上面定义的sessionFactory --&gt;<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;property name="sessionFactory" ref="sessionFactory"/&gt;<br /> &nbsp;&nbsp;&nbsp; &lt;/bean&gt;<br /> &nbsp;&nbsp;&nbsp; &lt;!-- 装配通用数据库访问类BaseDAOImpl --&gt;&nbsp;&nbsp; &nbsp;<br /> &nbsp;&nbsp; &nbsp;&lt;bean id="dao" class="com.sungoal.DAO.BaseDAOImpl"&gt;<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;!-- 依赖注入上面定义的sessionFactory --&gt;<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;property name="sessionFactory" ref="sessionFactory"/&gt;<br /> &nbsp;&nbsp; &nbsp;&lt;/bean&gt;<br /> &nbsp;&nbsp;&nbsp; &lt;!-- 部署系统用户管理业务控制器AdminAction --&gt; <br /> &nbsp;&nbsp; &nbsp;&lt;bean id="adminAction" class="com.sungoal.struts.action.AdminAction" scope="prototype"&gt;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&lt;property name="dao" ref="dao"/&gt;<br /> &nbsp;&nbsp; &nbsp;&lt;/bean&gt;<br /> &lt;/beans&gt;</div><img src ="http://www.blogjava.net/xiaohuzi2008/aggbug/402773.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/xiaohuzi2008/" target="_blank">小胡子</a> 2013-08-14 09:33 <a href="http://www.blogjava.net/xiaohuzi2008/archive/2013/08/14/402773.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>