hibernate简介
hibernate寓意:let java objects hibernate in the relational database.
hibernate 是java应用和关系数据库之间的桥梁,负责java对象和关系数据库之间的映射的orm中间件。hibernate是一个开放源代码的对象关系映射框架,它对jdbc进行了非常轻量级的对象封装,使得java开发人员可以随心所欲的使用对象编程思维来操纵数据库。简单的说就是:
1.封装了通过jdbc访问数据库操作。
2.向上层应用提供访问面向对象数据访问的api。
创建hibernate配置文件
通过一个例子practice 演示如何运用hibernate来访问关系数据库。
practice 工程的功能:
通过hibernate保存客户(customer)信息。
其hibernate应用结构图如下:
?d?d?d?d?d?d?d?d?d?d?d?d?d?d?d?d?d?d?d?d?d?d?d?d?d?d?d
practice 应用
customer class ; action class;business class
?d?d?d?d?d?d?d?d?d?d?d?d?d?d?d?d?d?d?d?d?d?d?d?d?d?d?d?d
?d?d?d?d?d?d?d?d?d?d?d?d?d?d?d?d?d?d?d?d?d?d?d?d?d?d?d?d
hibernate xml
对象-关系映射文件 hibernate api
hibernate 配置文件
?d?d?d?d?d?d?d?d?d?d?d?d?d?d?d?d?d?d?d?d?d?d?d?d?d?d?d?d
?d?d?d?d?d?d?d?d?d?d?d?d?d?d?d?d?d?d?d?d?d?d?d?d?d?d?d?d
关系数据库(mysql)
customers 表
?d?d?d?d?d?d?d?d?d?d?d?d?d?d?d?d?d?d?d?d?d?d?d?d?d?d?d?d
创建持久化类
hibernate 从hibernate配置文件中读取和数据库连接相关的信息。
配置文件有两种形式:
一种是xml格式的文件:hibernate.cfg.xml
一种是java属性文件:hibernate.properties
这个实例中我们将使用hibernate.cfg.xml。
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">
插入位置在src目录下:
创建o/r对象-关系映射文件
创建持久化的类customer.java
package entity;import java.io.serializable;
public class customers implements serializable {
private integer id;
private string name;
private int age;
public customers() {
}
public int getage() {
return age; }
public void setage(int age) {
this.age = age;
}
public integer getid() {
return id;
}
public void setid(integer id) {
this.id = id;
}
public string getname() {
return name;
}
public void setname(string name) {
this.name = name;
}}
get/set 方法的命名必须符合javabean的规范,否则hibernate会报出异常,具体请参照相关资料。
关于serializable接口:
hibernate 并不要求持久化类必须实现java.io.serializable接口,但是对于采用分布式结构的java应用,当java对象在不同的进程节点之间传输时,这个对象必须实现这个接口;如果希望对httpsession中存放的java对象进行持久化,那么这个java对象必须实现serializable接口。
关于不带参数的构造方法:
public customers() { }
hibernate要求持久化类必须提供一个不带参数的默认的构造方法,原因请参考相关资料。
创建mysql数据库
数据库名称:netstroe
customer表ddl定义如下:create table `customers` ( `id` bigint(20) not null default '0', `name` varchar(15) default null, `age` int(11) default null, primary key (`id`)) type=myisam;
创建对象-关系映射文件
创建对象-关系映射文件:customers.hbm.xml
代码如下:
"-//hibernate/hibernate mapping dtd 2.0//en"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
http://boss.bekk.no/boss/middlegen/
http://hibernate.sourceforge.net/-->
table="customers">
type="java.lang.integer"
column="id"
>
type="java.lang.string"
column="name"
length="15"
/>
type="int"
column="age"
length="11"
/>
引入hibernate所需的jar包
hibernate2.jar、hibernate-tools.jar
通过hibernate api 访问mysql数据库
创建业务逻辑类:usehibernate.java
代码如下:
package business;
import entity.customers;
import net.sf.hibernate.session;
import net.sf.hibernate.sessionfactory;
import net.sf.hibernate.transaction;
import net.sf.hibernate.cfg.configuration;
public class usehibernate {
public static sessionfactory sessionfactory;
/** 初始化hibernate,创建sessionfactory实例 */
public void savecustomers(customers customers) throws exception {
configuration config = null;
config = new configuration().configure();
// 创建一个sessionfactory 实例
sessionfactory = config.buildsessionfactory();
session session = sessionfactory.opensession();
transaction tx = null;
try {
/* 开始一个事务 */
tx = session.begintransaction();
session.save(customers);
/* 提交事务 */
tx.commit();
} catch (exception e) {
// todo auto-generated catch block
if (tx != null)
tx.rollback();
throw e;
} finally {
session.close();
}
}}
测试hibernate配置是否成功
创建junit测试:testhibernate.java
有关junit请参考相关资料:
package test;
import business.usehibernate;
import entity.customers;import junit.framework.testcase;
customers customers = new customers();
customers.setid(new integer(330121));
customers.setage(24);
customers.setname("huhpreal");
usehibernate usehibernate = new usehibernate();
try {
usehibernate.savecustomers(customers);
} catch (exception e) {
// todo auto-generated catch block
e.printstacktrace();
}
}}
查看后台打印信息:
(cfg.environment 403 ) hibernate 2.0.3
(cfg.environment 432 ) hibernate.properties not found
(cfg.environment 452 ) using cglib reflection optimizer
(cfg.environment 462 ) jvm proxy support: true
(cfg.configuration 703 ) configuration resource: /hibernate.cfg.xml
(cfg.configuration 270 ) mapping resource: hbm/customers.hbm.xml
(cfg.binder 178 ) mapping class: entity.customers -> customers
(cfg.configuration 885 ) configured sessionfactory: null
(cfg.configuration 492 ) processing one-to-many association mappings
(cfg.configuration 503 ) processing foreign key constraints
(impl.sessionfactoryimpl 132 ) building session factory
(dialect.dialect 83 ) using dialect: net.sf.hibernate.dialect.mysqldialect
(connection.drivermanagerconnectionprovider 41 ) hibernate connection pool size: 20
(connection.drivermanagerconnectionprovider 70 ) using driver: org.gjt.mm.mysql.driver at url: jdbc:mysql://localhost:3306/netstore
(connection.drivermanagerconnectionprovider 71 ) connection properties: {useunicode=true, user=root, password=123456, characterencoding=gb2312}
(impl.sessionfactoryimpl 162 ) use outer join fetching: false
(impl.sessionfactoryimpl 185 ) use scrollable result sets: true
(impl.sessionfactoryimpl 186 ) jdbc 2 max batch size: 15
(impl.sessionfactoryimpl 194 ) echoing all sql to stdout
(impl.sessionfactoryobjectfactory 82 ) no jdni name configured
(impl.sessionfactoryimpl 269 ) query language substitutions: {}
hibernate 配置使用成功
查看数据库:
插入成功!
闽公网安备 35060202000074号