Hibernate框架入门(一)

来源:互联网 发布:geo数据库挖掘 编辑:程序博客网 时间:2024/04/29 19:51

Hibernate
1hibernate的概念
hibernate是一个ormapping框架
hibernate是一个数据库的操作框架
2JDBC的缺点
1)代码相当繁琐
2)从一个数据库移值到另外一个数据库,得改变很多内容,比较差
3)看下图:
jdbc

1.在进行请求的时候,直接从内存中提取数据就可以了,这样的过程称为数据缓存
2.Jdbc虽然提供了数据库连接池,但是没有提供数据缓存
4)jdbc技术是面向sql语句操作,而不是面向对象操作的
3JDBC的优点
如果操作数据库,则越底层效率越高
4Hibernate的优点
Hibdrnate的代码是比较简单的
Hibernate是面向对象的操作
Hibernate的移动植性很强
Hibernate的缓存是世界级的
5Hibernate的缺点
1、不能干预sql语句的生成
例如:查询person表的pid为1,2,3的数据,如果写sql语句,会有如下的写法:
select * from person where pid in(1,2,3)

select * from person where pid=1 or pid=2 or pid=3

select * from person where 1<=pid<=3

select * from person where pid=1
union
select * from person where pid=2
union
select * from person where pid=3
以上四种情况
所以如果一个项目中,如果对sql语句的优化要求比较高,那么不适合用hibernate
2、如果一张表中有千万级别的数据量,也不适合用hibernate
3、hibernate适合用中小型企业开发软件

Hibernate的第一个程序
1、创建一个java project
2、导入jar包
3、创建hibernate的配置文件:hibernate.cfg.xml

<hibernate-configuration>    <session-factory>        <!--             要链接数据库的用户名         -->        <property name="connection.username">root</property>        <!--             要链接数据库的密码         -->            <property name="connection.password">root</property>        <!--             链接数据库的url         -->        <property name="connection.url">            jdbc:mysql://localhost:3306/itcastsh08_hibernate        </property>        <!--             方言                 告诉hibernate用什么样的数据库         -->        <property name="dialect">            org.hibernate.dialect.MySQLDialect        </property>     <!--             validate  默认值                根据持久化类和映射文件检查表的结构            update            hibernate容器在启动的时候,会根据持久化类和映射文件检查表的结构            如果不存在,则创建,如果存在,则更新            create            每次启动hibernate容器,不管表是否存在,都会创建            create-drop            当启动hibernate容器时创建表,当hibernate容器销毁时,删除表        -->        <property name="hbm2ddl.auto">update</property>引入映射文件    <mapping resource="cn/itcast/sh08/hibernate/domain/Person.hbm.xml"/>    </session-factory></hibernate-configuration>

4、写代码

public void testSavePerson(){        //创建配置文件        Configuration configuration = new Configuration();        //加载配置文件        configuration.configure();        //创建session        SessionFactory sessionFactory = configuration.buildSessionFactory();        //打开session        Session session = sessionFactory.openSession();        //创建事务        Transaction transaction = session.beginTransaction();        Person person = new Person();        person.setName("zx3");        person.setSex("man");        //保存持久化对象        session.save(person);        transaction.commit();        session.close();    }

三大要素
Hibernate的配置文件
链接数据库
其他的配置

自动创建表

 <property name="hbm2ddl.auto">update</property>

引入映射文件
Hibernate的映射文件
完成了从持久化类到数据库表的映射
Hibernate的持久化类
一个持久化类必须和数据库的一张表对应

Hibernate的执行流程
Hibernate的执行流程

1、在配置文件中引入了映射文件,在映射文件中指明了持久化类,所以hibernate识别Person是一个持久化类
Hibernate容器-hibernate.cfg.xml->*.hbm.xml->class元素的name属性加在持久化类
通过这种方式识别持久化类

2、关于sessionFactory
1、sessionFactory是存放配置文件、映射文件、持久化类的信息的
2、sessionFactory只有一个
3、一个sessionFactory代表一个数据库的链接
4、sessionFactory类本身是线程安全的
3、关于session
1、一个session代表数据库的一个链接
2、Crud操作是通过session来完成的
4、transaction是由session来创建的,因为先有链接,后有事务
5、完成操作以后,事务必须提交,session必须关闭

CRUD

@Test    public void testSavePerson(){        //创建配置文件        Configuration configuration = new Configuration();        //加载配置文件        configuration.configure();        //创建session        SessionFactory sessionFactory = configuration.buildSessionFactory();        //打开session        Session session = sessionFactory.openSession();        //创建事务        Transaction transaction = session.beginTransaction();        Person person = new Person();        person.setName("zx3");        person.setSex("man");        //保存持久化对象        session.save(person);        transaction.commit();        session.close();    }    @Test    public void testfindAll(){        Session session = sessionFactory.openSession();        List<Person> list = session.createQuery("from Person").list();        for(Person p:list){            System.out.println(p);        }    }    @Test    public void textDeletePerson(){        Session session=sessionFactory.openSession();        Transaction transaction = session.beginTransaction();        //第二个参数能接受所有类型        Person p = (Person)session.get(Person.class, 1L);        session.delete(p);        transaction.commit();        session.close();    }    public void testUpdate(){        Session session=sessionFactory.openSession();        Transaction transaction = session.beginTransaction();        //得到        Person person=(Person)session.get(Person.class, 3L);        //改变        person.setName("3zx");        session.update(person);        transaction.commit();        session.close();    }

显示sql语句

 <property name="show_sql">true</property> <property name="format_sql">true</property>

主键的生成器
Identity
1、表必须支持自动增长机制
2、数据库生成主键
3、不需要在程序中设置主键

Assigned
1、必须通过程序的方式给值才可以

Increment
1、如果选择该主键的生成方式,则必须是数字类型
2、先获取主键的最大值,在最大值的基础上加1,形成新的主键
4、效率比较低
5、主键的生成是由hibernate内部实现的

Native
1、会根据不同的数据库的表选择不同的生成策略

Uuid
1、主键必须是varchar类型
2、主键是由hibernate内部生成的
3、在持久化类中必须是String类型
在程序中是不需要设置的

方向工程:将一个项目变为Hibernate项目

对象的状态

对象的状态

说明:
1、把1,2,3步这样的对象称为临时状态的对象
2、临时状态的对象没有和hibernate发生交互
3、当执行第四步的时候,pereson对象和hibernate容器发生了交互
如果主键的生成机制为increment,这个时候会发出
Select max(pid) 查找主键的最大值
如果主键的生成机制为identity,则会发出insert语句
4、把第四部完成以后的对象称为持久化对象,因为和hibernate发生交互了
5、当执行第5步,完成事务提交,这个时候hibernate中的session并没有关闭,所以该对象还是持久化对象,在数据库里有对应的值了
6、当执行第6步的时候,session关闭了,该对象变成脱管对象
脱离hibernate管理了

变为持久化状态:

session.get();session.update();session.save();
0 0
原创粉丝点击