</pre>引言: <span style="color:rgb(51,51,51); font-family:arial,宋体,sans-serif; font-size:14px; line-height:24px; text-indent:28px">jdbc的缺点:查询代码特别繁琐;重复代码特别多,频繁的try,catch;没有数据的缓存;SQL的移植性不好</span><p></p><p><span style="color:rgb(51,51,51); font-family:arial,宋体,sans-serif; font-size:14px; line-height:24px; text-indent:28px"> <span style="white-space:pre"></span> jdbc的优点:</span><span style="font-family:宋体; font-size:12pt; text-indent:0in; color:rgb(51,51,51); line-height:24px">速度比较快;</span><span style="font-family:宋体; font-size:12pt; text-indent:0in; color:rgb(51,51,51); line-height:24px">把控性比较好</span></p><p><span style="color:rgb(51,51,51); font-family:arial,宋体,sans-serif; font-size:14px; line-height:24px">概念: </span><span style="color:rgb(51,51,51); font-family:arial,宋体,sans-serif; font-size:14px; line-height:24px">Hibernate是一个开放源代码的<strong>对象 关系 映射框架(ORM)</strong>,它对JDBC进行了非常轻量级的对象封装,使得Java程序员可以随心所欲的<strong>使用对象编程思维来操纵数据库</strong>。 Hibernate可以应用在任何使用JDBC的场合,既可以在Java的客户端程序使用,也可以在Servlet/JSP的Web应用中使用,最具革命意义的是,<em>Hibernate可以在应用EJB的J2EE</em></span><span style="color:rgb(51,51,51); font-family:arial,宋体,sans-serif; font-size:14px; line-height:24px; text-indent:28px"><em>架构中取代CMP,完成数据持久化</em></span><span style="color:rgb(51,51,51); font-family:arial,宋体,sans-serif; font-size:14px; line-height:24px; text-indent:28px"><em>的重任</em>。-----来自百度百科</span></p><p><span style="color:rgb(51,51,51); font-family:arial,宋体,sans-serif; font-size:14px; line-height:24px; text-indent:28px">引入hibernate后,代码变得比较简单;还可以实现数据缓存,一级缓存,二级缓存,查询缓存;代码移植性比较。但也存在以下缺点,因为sql语句是hibernate内部生成的,所以程序员干预不了,不可控;如果数据库特别大,不适合hibernate。</span></p><p><span style="color:rgb(51,51,51); line-height:24px; text-indent:28px; font-size:14px; font-family:arial,宋体,sans-serif"></span></p><p style="margin-top:0pt; margin-bottom:0pt; margin-left:0in; text-indent:0in; direction:ltr; unicode-bidi:embed"><span style="font-size:12pt; font-family:宋体">hibernate的核心是:ormapping</span><span style="font-size:12pt; font-family:宋体">对象关系映射---</span><span style="font-family:宋体; font-size:12pt; text-indent:0in">让关系型数据库和对象发生关联,</span></p><p style="margin-top:0pt; margin-bottom:0pt; margin-left:0in; text-indent:0in; direction:ltr; unicode-bidi:embed"><span style="font-family:宋体; font-size:12pt; text-indent:0in">实现方式是通过XML映射文件进行关联,</span></p><p style="margin-top:0pt; margin-bottom:0pt; margin-left:0in; text-indent:0in; direction:ltr; unicode-bidi:embed"><span style="font-size:12pt; font-family:宋体">*.hbm.xml(</span><span style="font-size:12pt; font-family:宋体">映射文件</span><span style="font-size:12pt; font-family:宋体">)</span></p><p style="margin-top:0pt; margin-bottom:0pt; margin-left:0in; text-indent:0in; direction:ltr; unicode-bidi:embed"><span style="font-size:12pt; font-family:宋体"> </span><span style="font-size:12pt; font-family:宋体">类与表的对应关系</span></p><p style="margin-top:0pt; margin-bottom:0pt; margin-left:0in; text-indent:0in; direction:ltr; unicode-bidi:embed"><span style="font-size:12pt; font-family:宋体"> 类上的属性的名称和表中的字段的名称对应关系</span></p><p style="margin-top:0pt; margin-bottom:0pt; margin-left:0in; text-indent:0in; direction:ltr; unicode-bidi:embed"><span style="font-size:12pt; font-family:宋体"> 类上的属性的类型和表中的字段的类型对应关系</span></p><p style="margin-top:0pt; margin-bottom:0pt; margin-left:0in; text-indent:0in; direction:ltr; unicode-bidi:embed"><span style="font-size:12pt; font-family:宋体"> 把一对多和多对多的关系转化成面向对象的关系</span></p><p style="margin-top:0pt; margin-bottom:0pt; margin-left:0in; text-indent:0in; direction:ltr; unicode-bidi:embed"><span style="font-size:12pt; font-family:宋体"></span></p><p style="margin-top:0pt; margin-bottom:0pt; margin-left:0in; text-indent:0in; direction:ltr; unicode-bidi:embed"><span style="font-size:12pt; font-family:宋体"></span></p><p style="margin-top:0pt; margin-bottom:0pt; margin-left:0in; text-indent:0in; direction:ltr; unicode-bidi:embed"><span style="font-size:12pt; font-family:宋体">hibernate</span><span style="font-size:12pt; font-family:宋体">的配置文件</span><span style="font-size:12pt; text-indent:0in">作用,主要是用来连接数据库!</span></p><p style="margin-top:0pt; margin-bottom:0pt; margin-left:0in; text-indent:0in; direction:ltr; unicode-bidi:embed"><span style="font-size:12pt; text-indent:0in">所以学习hibernate,必须熟知以下内容</span></p><p style="margin-top:0pt; margin-bottom:0pt; margin-left:0in; text-indent:0in; direction:ltr; unicode-bidi:embed"><span style="font-size:12pt; text-indent:0in"></span></p><p style="margin-top:0pt; margin-bottom:0pt; margin-left:0in; text-indent:0in; direction:ltr; unicode-bidi:embed"><span style="font-size:12pt; font-family:宋体"> 1</span><span style="font-size:12pt; font-family:宋体">、</span><span style="font-size:12pt; font-family:宋体">hibernate</span><span style="font-size:12pt; font-family:宋体">的加载流程</span></p><p style="margin-top:0pt; margin-bottom:0pt; margin-left:0in; text-indent:0in; direction:ltr; unicode-bidi:embed"><span style="font-size:12pt; font-family:宋体"></span></p><p style="margin-top:0pt; margin-bottom:0pt; margin-left:0in; text-indent:0in; direction:ltr; unicode-bidi:embed"><span style="font-size:12pt; font-family:宋体"> </span><span style="font-size:12pt; font-family:宋体">2</span><span style="font-size:12pt; font-family:宋体">、</span><span style="font-size:12pt; font-family:宋体">crud</span><span style="font-size:12pt; font-family:宋体">的操作(增删改查的操作)</span></p><p style="margin-top:0pt; margin-bottom:0pt; margin-left:0in; text-indent:0in; direction:ltr; unicode-bidi:embed"><span style="font-size:12pt; font-family:宋体"><span style="font-size:12pt; font-family:宋体"> 3</span><span style="font-size:12pt; font-family:宋体">、关系操作--重点</span></span></p><p style="margin-top:0pt; margin-bottom:0pt; margin-left:0in; text-indent:0in; direction:ltr; unicode-bidi:embed"><span style="font-size:12pt; font-family:宋体"><span style="font-size:12pt; font-family:宋体"><span style="font-size:12pt; font-family:宋体"> 4</span><span style="font-size:12pt; font-family:宋体">、</span><span style="font-size:12pt; font-family:宋体">hibernate</span><span style="font-size:12pt; font-family:宋体">的优化--重点</span></span></span></p><p style="margin-top:0pt; margin-bottom:0pt; margin-left:0in; text-indent:0in; direction:ltr; unicode-bidi:embed"><span style="font-size:12pt; font-family:宋体"><span style="font-size:12pt; font-family:宋体"><span style="font-size:12pt; font-family:宋体"></span></span></span></p><p style="margin-top:0pt; margin-bottom:0pt; margin-left:0in; text-indent:0in; direction:ltr; unicode-bidi:embed"><span style="font-size:12pt; font-family:宋体"> 数据缓存</span></p><p style="margin-top:0pt; margin-bottom:0pt; margin-left:0in; text-indent:0in; direction:ltr; unicode-bidi:embed"><span style="font-size:12pt; font-family:宋体"> 懒加载</span></p><p style="margin-top:0pt; margin-bottom:0pt; margin-left:0in; text-indent:0in; direction:ltr; unicode-bidi:embed"><span style="font-size:12pt; font-family:宋体"> 抓取策略</span></p><p style="margin-top:0pt; margin-bottom:0pt; margin-left:0in; text-indent:0in; direction:ltr; unicode-bidi:embed"><span style="font-size:12pt; font-family:宋体"> </span><span style="font-size:12pt; font-family:宋体">……</span></p><p style="margin-top:0pt; margin-bottom:0pt; margin-left:0in; text-indent:0in; direction:ltr; unicode-bidi:embed"><span style="font-size:12pt; font-family:宋体"><span style="font-size:12pt; font-family:宋体"> 5</span><span style="font-size:12pt; font-family:宋体">、</span><span style="font-size:12pt; font-family:宋体">hql</span><span style="font-size:12pt; font-family:宋体">语句</span></span></p><h3><span style="font-size:12pt; font-family:宋体"><span style="font-size:12pt; font-family:宋体"><span style="font-size:12pt; font-family:宋体">第一个hibernate实例</span></span></span></h3><div><span style="white-space:pre"></span>新建java web 工程添加如下jar包</div><div>antlr-2.7.6.jarbackport-util-concurrent.jarc3p0-0.9.1.jarcommons-collections-3.1.jarcommons-logging-1.1.1.jardom4j-1.6.1.jarehcache-1.5.0.jarhibernate3.jar(核心包)javassist-3.9.0.GA.jarjta-1.1.jarlog4j.jarmysql-connector-java-5.1.10-bin.jarslf4j-api-1.5.8.jarslf4j-log4j12.jar引入测试包JUnit包</div>了解 <span style="color:rgb(51,51,51); font-family:arial,宋体,sans-serif; font-size:14px; line-height:24px; text-indent:28px">POJO(Plain Ordinary Java Object)简单的Java对象,实际就是普通JavaBeans,是为了避免和EJB混淆所创造的简称。也叫持久化对象</span><p style="margin-top:0pt; margin-bottom:0pt; margin-left:0in; text-indent:0in; direction:ltr; unicode-bidi:embed"><span style="font-size:12pt; font-family:宋体"><span style="color:rgb(51,51,51); font-family:arial,宋体,sans-serif; font-size:14px; line-height:24px; text-indent:28px">继承Serializable接口 的作用是将对象序列化,其作用是可以让对象在网络上进行传输</span></span></p><p style="margin-top:0pt; margin-bottom:0pt; margin-left:0in; text-indent:0in; direction:ltr; unicode-bidi:embed"></p>新建一个Person.java 文件和Person.hbm.xml<pre name="code" class="java">package com.someone.hibernate.sh.domain;
import java.io.Serializable;
/**
* 对象的序列化的作用:让对象在网络上传输,以二进制的形式传输
* @author Think
* Serializable标示接口
*/
public class Person implements Serializable{
private Long pid;
private String pname;
public Person(){}
public Person(String pname){
this.pname = pname;
}
public Long getPid() {
return pid;
}
public void setPid(Long pid) {
this.pid = pid;
}
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
public String getPsex() {
return psex;
}
public void setPsex(String psex) {
this.psex = psex;
}
private String 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">
<hibernate-mapping>
<!--
用来描述一个持久化类
name 类的全名
table 可以不写 默认值和类名一样
catalog 数据库的名称 一般不写
-->
<class name="com.someone.hibernate.sh.domain.Person">
<!--
标示属性 和数据库中的主键对应
name 属性的名称
column 列的名称
-->
<id name="pid" column="pid" length="200" type="java.lang.Long">
<!--
主键的产生器
就该告诉hibernate容器用什么样的方式产生主键
-->
<strong><generator class="increment"></generator></strong>
</id>
<!--
描述一般属性
-->
<property name="pname" column="pname" length="20" type="string">
</property>
<property name="psex" column="psex" length="10" type="java.lang.String"></property>
</class>
</hibernate-mapping>
然后新建配置文件 hibernate.cfg.xml
<?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只能连接一个数据库
-->
<session-factory>
<!--
数据库的用户名
-->
<property name="connection.username">root</property>
<!--
密码
-->
<property name="connection.password">tiger</property>
<!--
url
-->
<property name="connection.url">
jdbc:mysql://localhost:3306/itcast_sh_hibernate
</property>
<!--
作用:根据持久化类和映射文件生成表
validate 只验证
create-drop 启动生产结束删除
create 每次启动后生产表
update 启动hibernate容器的时候检查持久化类、映射文件 和数据库表是否对应,不对应创建
-->
<property name="hbm2ddl.auto">update</property>
<!--
显示hibernate内部生成的sql语句
-->
<property name="show_sql">true</property>
<mapping resource="com/somneone/hibernate/sh/domain/Person.hbm.xml" />
</session-factory>
</hibernate-configuration>
新建一个test.java文件
/hibernate_sh_base/test/com/someone/hibernate/sh/test/CreateTable.java
package com.someone.hibernate.sh.test;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
public class CreateTable {
@Test
public void testCreateTable(){
Configuration configuration = new Configuration();
configuration.configure();
configuration.buildSessionFactory();
}
}
运行测试就可以在数据库中动态创建 Person表 pid pname psex
下一步做一个增删改查 一个utils里面放工具类,然后写一个PersonTest
package com.someone.hibernate.sh.utils;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HiberanteUtils {
public static SessionFactory sessionFactory;
static{
/**
* 创建configuration对象
*/
Configuration configuration = new Configuration();
configuration.configure();
/**
* 加载hibernate的配置文件
*/
//configuration.configure("");
sessionFactory = configuration.buildSessionFactory();
}
}
package com.someone.hibernate.sh.test;
import java.io.Serializable;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;
import com.someone.hibernate.sh.domain.Person;
import com.someone.hibernate.sh.utils.HiberanteUtils;
public class PersonTest extends HiberanteUtils{
@Test
public void testSavePerson(){
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
Person person = new Person();
person.setPname("上海第一期班长");
person.setPsex("女");
/**
* 参数必须持久化对象
*/
session.save(person);
transaction.commit();
session.close();
}
@Test
public void testQueryPerson(){
Session session = sessionFactory.openSession();
List<Person> personList = session.createQuery("from Person").list();
for(Person person:personList){
System.out.println(person.getPname());
}
session.close();
}
@Test
public void testQueryPersonByID(){
Session session = sessionFactory.openSession();
/**
* 按照主键的方式查询数据库表中的记录
* 第二个参数的类型必须和持久化中标示符的类型保持一致
*/
Person person = (Person)session.get(Person.class, 1L);
System.out.println(person.getPname());
session.close();
}
/**
* hibernate内部会检查标示符,看标示符中的值在数据库相应的表中有没有对应的记录,如果有,则删除
*/
@Test
public void testDeletePerson(){
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
/**
* 1、根据id把值从数据库中查找出来
* 2、把对象删除掉
*/
// Person person = (Person)session.get(Person.class, 1L);
// session.delete(person);
/**
* 1、新创建一个person对象
* 2、给person对象的标示符赋值
* 3、调用session.delete方法删除
*/
Person person = new Person();
//person.setPid(2L);
session.delete(person);
transaction.commit();
session.close();
}
@Test
public void testUpdatePerson(){
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
/**
* 1、根据id把持久化对象提取出来
* 2、进行修改
* 3、执行upate操作
*/
Person person = (Person)session.get(Person.class, 1L);
person.setPsex("不详");
// Person person = new Person();
// person.setPid(1L);
session.update(person);
transaction.commit();
session.close();
}
@Test
public void testIdentity(){
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
Person person = (Person)session.get(Person.class, 1L);
Person person2 = new Person();
//person2.setPid(1L);
session.update(person2);
transaction.commit();
session.close();
}
}