Hibernate 进阶版

来源:互联网 发布:电脑记账软件 编辑:程序博客网 时间:2024/05/16 01:41
在对比之前的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");// HQLList<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的自动拆包打包机制,可以直接写成6if (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--:5name--:张飒sex--:男age--:22Hibernate:     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--:2id--:2 name--:小萌 sex--:女 age--:26id--:3 name--:小萌 sex--:女 age--:21id--:4 name--:李连杰 sex--:男 age--:21id--:5 name--:张飒 sex--:男 age--:22id--:6 name--:李连杰 sex--:男 age--:22id--:7 name--:李连杰 sex--:男 age--:23id--:8 name--:张飒 sex--:男 age--:23id--:10 name--:张飒 sex--:男 age--:23id--:11 name--:张飒 sex--:男 age--:88id--:12 name--:小萌萌 sex--:女 age--:21id--:13 name--:张飒 sex--:男 age--:88id--:14 name--:张飒小 sex--:男 age--:32Hibernate:     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
原创粉丝点击