Hibernate 进阶版

在对比之前的hibernate基础中  我们只要新加一个HibernateSessionFcatoryUtil类来帮助我们来建立sessionFactory(单例模式)因为系统为我们每建立一个session对象是很耗系统资源的  用单例的话  系统就只为我们建立一个sessionFactory对象  所有的操作都通过它来生成session对象  我们再把之前hibernate基础中的测试类Test修改下就行了  希望这能帮你进一步的熟悉hibernate操作数据库
 
package com.po;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibrnateSessionFcatoryUtil {
private static SessionFactory sessionFactory;

private HibrnateSessionFcatoryUtil() {
//设置为私有的构造方法是为了防止别人通过构造方法来调用其中的方法
}
public static SessionFactory getSessionFactory() {
if(sessionFactory==null) {
sessionFactory = new Configuration().configure().buildSessionFactory();
}
return sessionFactory;
}
/*方法二
static {
sessionFactory = new Configuration().configure().buildSessionFactory();//确保一定不为空
}
public SessionFactory getSessionFactory() {
return sessionFactory;
}
*/
}


这是我们要修改的测试类
package com.po;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

public class Test {

public static void main(String[] args) {
Test test = new Test();
test.add();
test.load();
test.list();
test.update();
test.delete_d();
}

public void add() {
Session session = HibrnateSessionFcatoryUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Table3 table3 = new Table3();
table3.setName("张飒小");
table3.setSex("男");
table3.setAge(32);

try {
session.save(table3);
session.getTransaction().commit();
System.out.println("数据插入成功");
} catch (Exception e) {
session.getTransaction().rollback();
System.out.println("数据插入失败");
}
}

public void load() {
Session session = HibrnateSessionFcatoryUtil.getSessionFactory().getCurrentSession();

session.beginTransaction();

Table3 tb = (Table3) session.get(Table3.class, new Integer(5));// 由id查找

if (tb != null) {
System.out.println("id--:" + tb.getId());
System.out.println("name--:" + tb.getName());
System.out.println("sex--:" + tb.getSex());
System.out.println("age--:" + tb.getAge());
} else {
System.out.println("找不到你要查的数据!");
}
session.getTransaction().commit();
}

public void list() {
Session session = HibrnateSessionFcatoryUtil.getSessionFactory().getCurrentSession();

session.beginTransaction();

Query query = session.createQuery("from Table3");// HQL
List<Table3> list = query.list();

for (Table3 t : list) {
System.out.print("id--:" + t.getId() + " ");
System.out.print("name--:" + t.getName() + " ");
System.out.print("sex--:" + t.getSex() + " ");
System.out.println("age--:" + t.getAge());
}
session.getTransaction().commit();
}

public void update() {
Session session = HibrnateSessionFcatoryUtil.getSessionFactory().getCurrentSession();

session.beginTransaction();

Table3 t1 = (Table3) session.get(Table3.class, 4);// java的自动拆包打包机制,可以直接写成6

if (t1 != null) {
t1.setName("李连杰");

session.update(t1);
session.getTransaction().commit();

System.out.println("更新成功");
} else {
System.out.println("要更新的数据不存在");
}
}

public void delete_d() {
Session session = HibrnateSessionFcatoryUtil.getSessionFactory().getCurrentSession();

session.beginTransaction();

Table3 t2 = (Table3) session.get(Table3.class, 7);

if (t2 != null) {
session.delete(t2);
session.getTransaction().commit();
System.out.println("删除成功");
} else {
System.out.println("要删除的数据不存在");
}
}
}

程序运行打印的信息
Hibernate: 
    insert 
    into
        mysql.table3
        (name, sex, age) 
    values
        (?, ?, ?)
数据插入成功
Hibernate: 
    select
        table3x0_.id as id0_0_,
        table3x0_.name as name0_0_,
        table3x0_.sex as sex0_0_,
        table3x0_.age as age0_0_ 
    from
        mysql.table3 table3x0_ 
    where
        table3x0_.id=?
id--:5
name--:张飒
sex--:男
age--:22
Hibernate: 
    select
        table3x0_.id as id0_,
        table3x0_.name as name0_,
        table3x0_.sex as sex0_,
        table3x0_.age as age0_ 
    from
        mysql.table3 table3x0_
id--:1 name--:小萌 sex--:男 age--:2
id--:2 name--:小萌 sex--:女 age--:26
id--:3 name--:小萌 sex--:女 age--:21
id--:4 name--:李连杰 sex--:男 age--:21
id--:5 name--:张飒 sex--:男 age--:22
id--:6 name--:李连杰 sex--:男 age--:22
id--:7 name--:李连杰 sex--:男 age--:23
id--:8 name--:张飒 sex--:男 age--:23
id--:10 name--:张飒 sex--:男 age--:23
id--:11 name--:张飒 sex--:男 age--:88
id--:12 name--:小萌萌 sex--:女 age--:21
id--:13 name--:张飒 sex--:男 age--:88
id--:14 name--:张飒小 sex--:男 age--:32
Hibernate: 
    select
        table3x0_.id as id0_0_,
        table3x0_.name as name0_0_,
        table3x0_.sex as sex0_0_,
        table3x0_.age as age0_0_ 
    from
        mysql.table3 table3x0_ 
    where
        table3x0_.id=?
更新成功
Hibernate: 
    select
        table3x0_.id as id0_0_,
        table3x0_.name as name0_0_,
        table3x0_.sex as sex0_0_,
        table3x0_.age as age0_0_ 
    from
        mysql.table3 table3x0_ 
    where
        table3x0_.id=?
Hibernate: 
    delete 
    from
        mysql.table3 
    where
        id=?
删除成功

我们再来看看数据库中的数据(第9个数据是我之前删掉了的)


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值