什么是持久化?
- 瞬时状态:
保存在内存的程序数据,程序退出后,数据就消失了,称为瞬时状态 - 持久化:
将程序数据在瞬时状态和持久状态之间转换的机制 - 持久状态:
保存在磁盘上的程序数据,程序退出后依然存在,称为程序数据的持久状态
什么是ORM?
ORM(对象关系映射):
完成对象数据到关系型数据映射的机制称为对象关系映射,简称ORM。
hibernate简介
Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,它将POJO与数据库表建立映射关系,是一个全自动的orm框架,hibernate可以自动生成SQL语句,自动执行,使得Java程序员可以随心所欲的使用对象编程思维来操纵数据库。 Hibernate可以应用在任何使用JDBC的场合,既可以在Java的客户端程序使用,也可以在Servlet/JSP的Web应用中使用,最具革命意义的是,Hibernate可以在应用EJB的JavaEE架构中取代CMP,完成数据持久化的重任。
hibernate添加用户步骤(3个步骤7个准备)
- 准备
1.导入hibernate库
2.添加配置文件
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hellohibernate</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123456</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="hibernate.show_sql">true</property>
<mapping resource="hrbust/ydl/pojo/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
3.实体类和映射文件
User
package hrbust.ydl.pojo;
import java.util.Date;
public class User {
private int id;
private String name;
private String age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
映射文件:
<?xml version="1.0"?>
<!--
~ Hibernate, Relational Persistence for Idiomatic Java
~
~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
-->
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping default-lazy="false">
<class name="hrbust.ydl.pojo.User" table="T_USER">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="name"/>
<property name="age"/>
</class>
</hibernate-mapping>
- 步骤
- 使用hibernate实现用户添加
package hrbust.ydl.dao;
import hrbust.ydl.pojo.User;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import java.sql.Date;
public class managerUser {
public static void main(String[] args){
Configuration cfg = null;
SessionFactory sf = null;
Session session = null;
Transaction ts = null;
User u = new User();
u.setName("zhangsan");
u.setAge("18");
try{
cfg = new Configuration().configure();
sf = cfg.buildSessionFactory();
session = sf.openSession();
ts = session.beginTransaction();
session.save(u);
ts.commit();
}catch(HibernateException e){
e.printStackTrace();
if(null != ts){
ts.rollback();
}
}finally {
sf.close();
session.close();
}
}
}
hibernate执行流程
效果图