引入
模型不匹配(阻抗不匹配)
Java面向对象语言,对象模型,其主要概念有:继承、关联、多态等;数据库是关系模型,其主要概念有:表、主键、外键等。
解决办法
1使用JDBC手工转换。
2使用ORM(Object Relation Mapping对象关系映射)框架来解决,主流的ORM框架有Hibernate、TopLink、OJB。JDBC的优点和缺点
JDBC的优点
直接底层操作,提供了很简单、便捷的访问数据库的方法,跨平台性比较强。灵活性比较强,可以写很复杂的SQL语句。JDBC的缺点
因为JAVA是面向对象的,JDBC没有做到使数据能够面向对象的编程,使程序员的思考仍停留在SQL语句上。
操作比较繁琐,很多代码需要重复写很多次。
如果遇到批量操作,频繁与数据库进行交互,容易造成效率的下降。
Hibernate
为什么要用Hibernate
Hibernate实现了面向对象的数据库编程
Hibernate比起JDBC来,在代码的书写上比较简单化了。
Hibernate提出了缓存机制,这样可以使访问数据的效率提高很大。结论:通过这个例子可以看到利用面向对象的技术也可以操作数据库。Hibernate就是一个面向对象操作数据库的框架,是基于JDBC开发的。
安装配置
- 下载地址http://www.hibernate.org
- 将下载目录/hibernate3.jar和/lib下的hibernate运行时必须的包加入classpath中:
antlr.jar,cglib.jar,asm.jar,commons-collections.jar,commons-logging.jar,jta.jar,dom4j.jar
配置文件hibernate.cfg.xml
持久化类 Customer.java
映射文件 Customer.hbm.xml
- 配置文件hibernate.cfg.xml和hibernate.properties,XML和properties两种,这两个文件的作用一样,提供一个即可,推荐XML格式,下载目录/etc下是示例配置文件。
可以在配置文件指定:
- 数据库的URL、用户名、密码、JDBC驱动类、方言等。 启动时Hibernate会在CLASSPATH里找这个配置文件。
- Hibernate.connection.url 表示要链接的数据库地址
- Hibernate.connection.driver_class 表示要链接的数据库的驱动类
- Hibernate.connection.username 要连接的数据库的用户名
- Hibernate.connection.password 要连接的数据库的密码
Hibernate.dialect 表示要使用的数据库的类型
org.hibernate.dialect.MySQL5Dialect mysql数据库
org.hibernate.dialect.Oracle9Dialect oracle数据库
org.hibernate.dialect.SQLServerDialect SQLServer数据库
hibernate.hbm2ddl.auto
validate:加载hibernate时验证创建表结构
update:加载hibernate时自动更新数据库结构,如果表存在不用创建,如果不存在就创建。
create:每一次加载hibernate时都创建表结构
create-drop:加载hibernate时创建,退出时删除
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<!-- 是用来描述数据库的连接 -->
<session-factory>
<!--
驱动
-->
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<!--
url
-->
<property name="connection.url">
jdbc:mysql://localhost:3306/hibernate0909
</property>
<!--
username
-->
<property name="connection.username">root</property>
<!--
password
-->
<property name="connection.password">123</property>
<!--
hibernate针对建表的操作
update 如果有表,检查表的结构,如果没有则创建
create-drop 启动hibernate创建表,结束hibernate删除表
create 每次启动都重新创建表
validate 每次启动都检查表的结构
-->
<property name="hbm2ddl.auto">update</property>
<property name="show_sql">true</property> <!-- 显示sql语句 -->
<mapping resource="cn/itcast/hibernate0909/domain/Person.hbm.xml" />
</session-factory>
</hibernate-configuration>
- 映射文件(hbm.xml,对象模型和关系模型的映射)。在/eg目录下有完整的hibernate示例。
快速开始小例子
持久化类:
package cn.itcast.hibernate0909.domain;
/**
* Person entity. @author MyEclipse Persistence Tools
*/
public class Person implements java.io.Serializable {
// Fields
private Long pid;
private String pname;
private String psex;
// Constructors
/** default constructor */
public Person() {
}
/** full constructor */
public Person(String pname, String psex) {
this.pname = pname;
this.psex = psex;
}
// Property accessors
public Long getPid() {
return this.pid;
}
public void setPid(Long pid) {
this.pid = pid;
}
public String getPname() {
return this.pname;
}
public void setPname(String pname) {
this.pname = pname;
}
public String getPsex() {
return this.psex;
}
public void setPsex(String psex) {
this.psex = psex;
}
}
- 映射文件:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="cn.itcast.hibernate0909.domain.Person" table="person" catalog="hibernate0909">
<id name="pid" type="long">
<column name="pid" />
<generator class="increment" />
</id>
<property name="pname" type="string">
<column name="pname"/>
</property>
<property name="psex" type="string">
<column name="psex" length="5" />
</property>
</class>
</hibernate-mapping>
- 测试代码
首先创建好数据库
package cn.itcast.hibernate0909.helloworld;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
import cn.itcast.hibernate0909.domain.Person;
public class PersonTest {
private static SessionFactory sessionFactory;
static{
Configuration configuration = new Configuration();
//加载配置文件
configuration.configure();
//采用了工厂模式创建sessionFactory
sessionFactory = configuration.buildSessionFactory();
}
@Test
public void testSavePerson(){
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
Person person = new Person();
//由于在映射文件中已经说明主键的产生方式是hibernate内部产生,所以在程序中不用设置主键
person.setPid(3L);
person.setPname("菜肉");
person.setPsex("男");
session.save(person);
transaction.commit();
session.close();
}
}
- 进行Junit测试,测试结果如下: