1、hibernate 中文:冬眠
2、持久化:数据从瞬时状态转化为持久状态
3、Hibernate:是一个轻量级的持久化框架。没有侵入性。是一个 orm 映射框架。简化了 jdbc 操作。极大了提高了开发效率。提供了缓存机制。强大的查询机制。支持多种数据库(数据库移植);
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">com.mysql.jdbc.Driverproperty >
<property name="connection.url">jdbc:mysql://localhost:3306/hibernate4property>
<property name="connection.username">rootproperty>
<property name="connection.password">rootproperty>
<property name="dialect">org.hibernate.dialect.MySQLDialectproperty>
session-factory>
hibernate-configuration>
public class User {
private int id;
private String name;
private int age;
public User() {
}
}
<hibernate-mapping package="cn.sxt.pojo">
<class name="User" table="t_user">
<id name="id">
<generator class="native"/>
id>
<property name="name"/>
<property name="age"/>
class>
hibernate-mapping>
public class Test {
public static void main(String[] args) {
//读取 src 下 hibernate.cfg.xml
Configuration cfg = new Configuration().configure();
//获取注册对象 4.3 的创建办法
ServiceRegistry registry = new StandardServiceRegistryBuilder()
.applySettings(cfg.getProperties())
.build();
SessionFactory factory = cfg.buildSessionFactory(registry);
Session session = factory.openSession();
//保存数据
Transaction tx = session.beginTransaction();
User u = new User("张三",22);
session.save(u);
tx.commit();
session.close();
}
}
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
property>
<property name="connection.url">
jdbc:mysql:///hibernate4
property>
<property name="connection.username">rootproperty>
<property name="connection.password">rootproperty>
<property name="dialect">
org.hibernate.dialect.MySQL5Dialect
property>
<property name="show_sql">trueproperty>
<property name="format_sql">trueproperty>
<property name="hbm2ddl.auto">updateproperty>
<mapping resource="cn/sxt/pojo/User.hbm.xml"/>
session-factory>
hibernate-configuration>
<hibernate-mapping>
<class name="cn.sxt.pojo.User" table="t_user">
<id name="id" column="id">
<generator class="identity">
generator>
id>
<property name="name" length="40"/>
<property name="age" />
class>
hibernate-mapping>
package cn.sxt.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import cn.sxt.pojo.User;
public class Test {
public static void main(String[] args) {
//1、读取src下hibernate.cfg.xml 如果不为configure指明参数 默认读取hibernate.cfg.xml
Configuration cfg = new Configuration().configure();
//3.x版本不需要ServiceRegistry
//4.0 ServiceRegistryBuilder
//获取注册对象4.3的创建办法
ServiceRegistry registry = new StandardServiceRegistryBuilder()
.applySettings(cfg.getProperties())
.build();
//SessionFactory是一个重量级对象 session的工厂 生命周期是进程级别的 支持集群 线程安全的
SessionFactory factory = cfg.buildSessionFactory(registry);
/*Session (org.hibernate.Session)
表示应用程序与持久储存层之间交互操作的一个单线程对象,
此对象生存期很短。 其隐藏了JDBC连接,也是Transaction的工厂。
其会持有一个针对持久化对象的必选(第一级)缓存,在遍历对象图或者根据持久化标识查找对象时会用到
session支持数据库操作
* */
Session session = null;
//事务对象
Transaction tx =null;
try{
session = factory.openSession();
tx = session.beginTransaction();
User u = new User("小红",22);
//保存数据
session.save(u);
//提交事务
tx.commit();
}catch(Exception e){
if(tx!=null)
//回滚事务
tx.rollback();
}finally{
if(session!=null)
session.close();
}
factory.close();
}
}
package cn.sxt.util;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
public class HibernateUtil {
private static Configuration cfg=null;
private static ServiceRegistry registry=null;
private static SessionFactory factory=null;
static{
cfg = new Configuration().configure();
registry = new StandardServiceRegistryBuilder()
.applySettings(cfg.getProperties())
.build();
factory = cfg.buildSessionFactory(registry);
}
public static Session getSession(){
return factory.openSession();
}
}
package cn.sxt.vo;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;
import cn.sxt.pojo.User;
import cn.sxt.util.HibernateUtil;
public class HibernateTest {
//向数据库添加数据
@Test
public void testSave(){
Session session = null;
Transaction tx = null;
try{
session = HibernateUtil.getSession();
//获取事务对象
tx = session.getTransaction();
//开启事务
tx.begin();
User u = new User("张学友",55);
session.save(u);
tx.commit();
}catch(Exception e){
e.printStackTrace();
if(tx!=null)
tx.rollback();
}finally{
if(session!=null)
session.close();
}
}
//获取数据
@Test
public void testGetById(){
Session session = null;
Transaction tx = null;
try{
session = HibernateUtil.getSession();
//获取事务对象
tx = session.getTransaction();
//开启事务
tx.begin();
//获取对象 第一参数 是对象的类型 第二个参数是 主键值
//User u = (User)session.get(User.class, 2);
User u = (User)session.load(User.class, 2);
System.out.println(u);
tx.commit();
}catch(Exception e){
e.printStackTrace();
if(tx!=null)
tx.rollback();
}finally{
if(session!=null)
session.close();
}
}
//获取所有对象
@Test
public void testGetAll(){
Session session = null;
Transaction tx = null;
try{
session = HibernateUtil.getSession();
//获取事务对象
tx = session.getTransaction();
//开启事务
tx.begin();
//查询所有
List<User> list = session.createCriteria(User.class).list();
for(User u:list){
System.out.println(u);
}
tx.commit();
}catch(Exception e){
e.printStackTrace();
if(tx!=null)
tx.rollback();
}finally{
if(session!=null)
session.close();
}
}
//修改数据
@Test
public void testUpdate(){
Session session = null;
Transaction tx = null;
try{
session = HibernateUtil.getSession();
//获取事务对象
tx = session.getTransaction();
//开启事务
tx.begin();
//获取对象 第一参数 是对象的类型 第二个参数是 主键值
//User u = (User)session.get(User.class, 2);
User u = (User)session.load(User.class, 2);
u.setAge(60);
//修改数据
session.update(u);
System.out.println(u);
tx.commit();
}catch(Exception e){
e.printStackTrace();
if(tx!=null)
tx.rollback();
}finally{
if(session!=null)
session.close();
}
}
@Test
public void testDelete(){
Session session = null;
Transaction tx = null;
try{
session = HibernateUtil.getSession();
//获取事务对象
tx = session.getTransaction();
//开启事务
tx.begin();
//获取对象 第一参数 是对象的类型 第二个参数是 主键值
//User u = (User)session.get(User.class, 2);
User u = (User)session.load(User.class, 2);
//删除对象
session.delete(u);
System.out.println(u);
tx.commit();
}catch(Exception e){
e.printStackTrace();
if(tx!=null)
tx.rollback();
}finally{
if(session!=null)
session.close();
}
}
}
@Test
public void testSession(){
Session session = null;
//瞬时状态
User u = new User("西毒",70);
try{
session = HibernateUtil.getSession();
session.beginTransaction();
//持久状态----做脏数据检查 将 session 数据和数据库同步
session.save(u);
u.setAge(80);
//session.update(u);
session.getTransaction().commit();
}catch(Exception e){
e.printStackTrace();
session.getTransaction().rollback();
}finally{
if(session!=null){
session.close();
}
}
//对象 u 是游离状态
System.out.println("======"+u);
try{
session = HibernateUtil.getSession();
session.beginTransaction();
//删除
session.delete(u);
session.getTransaction().commit();
}catch(Exception e){
e.printStackTrace();
session.getTransaction().rollback();
}finally{
if(session!=null){
session.close();
}
}
//瞬时状态
System.out.println("======"+u);
}
@Test
public void testGet(){
Session session = null;
User u =null;
try{
session = HibernateUtil.getSession();
session.beginTransaction();
//持久状态
/**
* get 查询数据如果数据不存在则返回 null
* load 查询数据如果数据不存在则抛出异常
*/
u = (User)session.get(User.class,4);
//u = (User)session.load(User.class, 50);
//瞬时状态 session.delete(u);
session.getTransaction().commit();
}catch(Exception e){
e.printStackTrace();
session.getTransaction().rollback();
}finally{
if(session!=null){
session.close();
}
}
System.out.println("===="+u);
try{
session = HibernateUtil.getSession();
session.beginTransaction();
//持久状态 save 直接添加数据发出一条 insert 语句
session.save(u);
//saveOrUpdate 判断保存的对象是否有 id,如果有则发出 update 语句
//如果没有则发出 insert 语句
//session.saveOrUpdate(u);
session.getTransaction().commit();
}catch(Exception e){
e.printStackTrace();
session.getTransaction().rollback();
}finally{
if(session!=null){
session.close();
}
}
}
@Test
public void testDelete(){
Session session = null;
User u =null;
try{
session = HibernateUtil.getSession();
session.beginTransaction();
u = new User();
u.setId(7);
session.delete(u);
session.getTransaction().commit();
}catch(Exception e){
e.printStackTrace();
session.getTransaction().rollback();
}finally{
if(session!=null){
session.close();
}
}
}
@Test
public void testUpdate(){
Session session = null;
User u =null;
try{
session = HibernateUtil.getSession();
session.beginTransaction();
u = new User();
u.setId(3);
u.setName("南帝");
u.setAge(66);
session.update(u);
session.getTransaction().commit();
}catch(Exception e){
e.printStackTrace();
session.getTransaction().rollback();
}finally{
if(session!=null){
session.close();
}
}
}
临时状态:内存有,数据库没有
持久状态:内存有,session 有,数据库有
游离状态:内存有,数据有