在看一些教程,偶尔看到H2,被其精简的安装(只有一个jar包就好了..根本不用安装啊...)和完善的功能吸引了.
也做了一个很简单的东西,因为嫌写配置文件麻烦,所以就木有引spring进来了(这个我也是为了写教程里一个示例程序搭一下的 教程里直接JDBC然后写SQL 我这里加入一个hibernate 内容很简单 说错了欢迎指正呢)
环境:
h2-1.3.169
hibernate 3.6.10
项目结构如下:
先说下hibernate的配置文件:
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- 驱动位置 --> <property name="connection.driver_class">org.h2.Driver</property> <!-- 存放位置 我这边放在了h盘下h2目录的product 会在相应位置生成一个.h2文件--> <property name="connection.url">jdbc:h2:h:/h2/product</property> <!-- 用户名 下面是密码 --> <property name="connection.username">sa</property> <property name="connection.password">sa</property> <!-- 使用的数据库方言 --> <property name="dialect">org.hibernate.dialect.H2Dialect</property> <property name="show_sql">true</property> <property name="hbm2ddl">update</property> <property name="current_session_context_class">thread</property> <!-- 引入的实体 --> <mapping class="org.cc.ws.entity.Product"/> </session-factory> </hibernate-configuration>
接下去是实体类的文件:
package org.cc.ws.entity; import java.util.Date; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; @Entity public class Product { @Id @GeneratedValue @Column(name="id") private int id; @Column(unique=true,nullable=false) private String name; private double price; private Date createTime; private int count; private String manufacturer; private String phoneNumber; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } public int getCount() { return count; } public void setCount(int count) { this.count = count; } public String getManufacturer() { return manufacturer; } public void setManufacturer(String manufacturer) { this.manufacturer = manufacturer; } public String getPhoneNumber() { return phoneNumber; } public void setPhoneNumber(String phoneNumber) { this.phoneNumber = phoneNumber; } @Override public String toString() { return "Product [id=" + id + ", name=" + name + ", price=" + price + ", createTime=" + createTime + ", count=" + count + ", manufacturer=" + manufacturer + ", phoneNumber=" + phoneNumber + "]"; } }
然后利用SchemaExport来生成一下数据库:
package org.cc.ws.utils; // 省略下引包 public class ExportDB { /** * @param args */ public static void main(String[] args) { //默认读取hibernate.cfg.xml文件..... Configuration cfg=new Configuration().configure(); SchemaExport export=new SchemaExport(cfg); export.create(true, true); } }
正常的话 会出现下面的语句(show_sql选项打开的话):
写道
drop table Product if exists
create table Product (id integer generated by default as identity, count integer not null, createTime timestamp, manufacturer varchar(255), name varchar(255) not null unique, phoneNumber varchar(255), price double not null, primary key (id))
create table Product (id integer generated by default as identity, count integer not null, createTime timestamp, manufacturer varchar(255), name varchar(255) not null unique, phoneNumber varchar(255), price double not null, primary key (id))
好了 接下去的操作就很简单了
像平常使用hibernate一样
发一下Dao的实现类:
package org.cc.ws.dao.impl; // 省略引包 public class ProductDaoImpl implements ProductDao { @Override public boolean insert(Product product) { Session session=FactoryHolder.getSessionFactory().getCurrentSession(); Transaction tas=session.beginTransaction(); try{ product.setCreateTime(new Date()); session.save(product); tas.commit(); }catch(Exception e){ e.printStackTrace(); tas.rollback(); return false; } return true; } @Override public Product getByName(String name) { Session session=FactoryHolder.getSessionFactory().getCurrentSession(); Transaction tas=session.beginTransaction(); Product product=null; try{ Query query=session.createQuery("from Product t where t.name=:name"); query.setString("name", name); product=(Product) query.uniqueResult(); tas.commit(); } catch(Exception e){ tas.rollback(); e.printStackTrace(); } return product; } @Override public List<Product> getByManufacture(String manufacturer) { Session session=FactoryHolder.getSessionFactory().getCurrentSession(); Transaction tas=session.beginTransaction(); List<Product> products=null; try{ Query query=session.createQuery("from Product t where t.manufacturer=:manufacturer"); query.setString("manufacturer", manufacturer); products=(List<Product>) query.list(); tas.commit(); } catch(Exception e){ tas.rollback(); e.printStackTrace(); } return products; } }
最后是一个测试文件:
package org.cc.ws.dao.impl; import org.cc.ws.dao.ProductDao; import org.cc.ws.entity.Product; import junit.framework.TestCase; public class ProductDaoTest extends TestCase{ private ProductDao dao; public void setUp(){ dao=new ProductDaoImpl(); } public void testInsert(){ Product product=new Product(); product.setName("测试物品1"); product.setManufacturer("cc"); product.setPrice(10.0); dao.insert(product); } public void testName(){ System.out.println(dao.getByName("测试物品1").getId()); } }
执行第一个:
写道
Hibernate: insert into Product (id, count, createTime, manufacturer, name, phoneNumber, price) values (null, ?, ?, ?, ?, ?, ?)
然后执行第二个:
写道
Hibernate: select product0_.id as id0_, product0_.count as count0_, product0_.createTime as createTime0_, product0_.manufacturer as manufact4_0_, product0_.name as name0_, product0_.phoneNumber as phoneNum6_0_, product0_.price as price0_ from Product product0_ where product0_.name=?
1
1
可见使用正常.
相关推荐
博文链接:https://congpeixue.iteye.com/blog/218782
springboot+h2+mybatisplus+swagger使用例子 h2数据库例子 H2是一个开源的嵌入式数据库引擎,采用java语言编写,不受平台的限制,同时H2提供了一 个十分方便的web控制台用于操作和管理数据库内容。H2还提供兼容...
哈弗H2汽车使用手册用户说明书pdf电子版下载
H2就不做很多介绍了。资源包内容列表是我进行H2预研是收集的H2资料,应该是最全面的的了: 1、h2.pdf (H2 API) 2、h2-1.3.173.jar (截止2013-8-15最新的H2.jar) 3、h2-2013-07-28.zip (截止...13、H2数据库使用.doc
H2DB使用方法
其中H2, HSQLDB类似,十分适合作为嵌入式数据库使用,其它的数据库大部分都需要安装独 立的客户端和服务器端。2.H2的优势: 1、h2采用纯Java编写,因此不受平台的限制。 2、h2只有一个jar文件,十分适合作为嵌入式...
H2数据库使用.doc
H2数据库使用说明,包括怎么安装,怎么启动服务,样例,执行,连接,端口,断开连接等
在windows下如何开启H2的控制台并且简单使用
长城哈弗H2红标汽车使用手册用户操作说明书pdf电子版下载
持久性 使用JPA和H2数据库进行Hibernate的最简单配置
H2嵌入式数据库的使用教程,帮助大家学习H2数据的界面操作使用
H2DB数据库使用方法,一个简单的ppt
使用Spring Hibernate H2指标-需要更正的注意事项 实际测试-Marcos 要求 Java 1.7.x Jboss 6.4 EAP GA H2 3.0.1 Maven的3.x Postgres 9.1 银行脚本执行: 要创建大量数据,必须执行位于以下位置的sql脚本: ...
网上找不到这样的例子,只好我自已整理一份了。 该demo是基于maven集成spring5、hibernate5、spring-data-jpa2、H2的可以运行的最基本的main例子。 希望能对大家有帮助。
本文档中包含最新、最全的H2数据使用指南,堪称H2数据库官方使用手册。例案从入门级到进阶级,应有尽有,详细实用
h2数据库批量导出,文档描述h2数据库使用方法和数据库创建和插入脚本批量导出
简单的H2数据库和Ibatis集成,使用Myeclipes,H2则选择内存模式和嵌入式模式启动
#hibernate.connection.url jdbc:h2:mem:./build/db/h2/hibernate #hibernate.connection.url jdbc:h2:testdb/h2test #hibernate.connection.url jdbc:h2:mem:imdb1 #hibernate.connection.url jdbc:h2:tcp://dbserv...
H2数据库下载和安装,简单使用H2数据库,登录H2数据库的WebConsole控制台,创建表,常用sql 等。