hibernate基本原理
hibernate是对JDBC的封装:原来使用JDBC开发时,存在很多冗余,如各种JDBC语句,connection管理,所以有了hibernate,不需要操作数据,直接操作hibernate就可。
hibernate是开源的一个ORM(对象关系映射)框架。ORM,即Object-Relational Mapping,作用就是在关系型数据库和对象之间做了一个映射。从对象映射到关系,再从关系映射到对象。这样在操作数据库的时候,不需要再去和复杂sql打交道,只要像操作对象一样操作它就好(把关系数据库的字段在内存中映射成对象的属性)。
hibernate核心
1.Configuration接口:配置并启动hibernate
2.SessionFactory接口:初始化hibernate
3.Session接口:持久化对象的CRUD操作
4.Transaction接口:负责事务
5.Query接口、Criteria接口:执行数据库查询
Configuration是一个启动期间的对象,一旦SessionFactory创建完成就会被丢弃。
hibernate运行过程
1.通过Configuration().configure();读取并解析hibernate.cfg.xml文件
2.由hibernate.cfg.xml中的<mappingresource="com/**/**.hbm.xml"/>读取并解析映射信息
3.config.buildSessionFactory();创建SessionFactory
4.sessionFactory.openSession();打开session
5.session.beginTransaction();创建事务
6.persistent operate持久化操作,一般指Save这个方法
7.session.getTransaction().commit();提交事务
8.关闭session
hibernate基本配置
hibernate.cfg.xml
<span style="font-size:14px;"><!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="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- 设置数据库URL -->
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/***</property>
<!-- 数据库用户名 -->
<property name="hibernate.connection.username">root</property>
<!-- 数据库密码 -->
<property name="hibernate.connection.password">root</property>
<!-- 指定对应数据库的方言,hibernate为了更好适配各种关系数据库,针对每种数据库都指定了一个方言dialect -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 打印sql语句,方便调试 -->
<property name="show_sql">true</property>
<!-- 格式化sql语句,排版 -->
<property name="foramt_sql">true</property>
<!-- 指定hibernate生成数据库表的方式:create每次创建新表,update使用原表 -->
<property name="hbm2ddl.auto">update</property>
<!-- 使用session.getCurrentSession时需要打开该配置 -->
<property name="hibernate.current_session_context_class">true</property>
<!-- 映射文件 -->
<mapping resource="com/xxxx/xxxx/User.hbm.xml"/>
</session-factory>
</hibernate-configuration> </span>
**.hbm.xml
<span style="font-size:14px;"><?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<!--生成默认为user的数据库表-->
<class name="com.pechen.hibernate.User">
<id name="id">
<!-- 算法的核心思想是结合机器的网卡、当地时间、一个随机数来生成GUID -->
<generator class="uuid"></generator>
</id>
<property name="name"></property>
<property name="password"></property>
<property name="createTime" type="date"></property>
<property name="expireTime" type="date"></property>
</class>
</hibernate-mapping></span>
hibernate优缺点
优点:
1.更加对象化: 以对象化的思维操作数据库,我们只需要操作对象就可以了,开发更加对象化
2.可移植性:因为Hibernate做了持久层的封装,你就不知道数据库,你写的所有的代码都具有可复用性
3.Hibernate是一个没有侵入性的框架,没有侵入性的框架我们称为轻量级框架。 对比Struts的Action和ActionForm,都需要继承,离不开Struts。Hibernate不需要继承任何类,不需要实现任何接口。这样的对象叫POJO对象
4.Hibernate代码测试方便
5.提高效率,提高生产力
缺点:
1.使用数据库特性的语句,将很难调优
2.对大批量数据更新存在问题
3.系统中存在大量的攻击查询功能