Hibernate实现简单的增删改查

来源:互联网 发布:sql 执行视图 编辑:程序博客网 时间:2024/04/19 21:05

       在hibernate中的增删改查的实现。hibernate是OR框架,也就是对象关系框架,有了 hibernate我们就不用再去写SQL语言,我们只需要操纵对象去进行增删改查。这里今天写的就是在如何应用hibernate实现增删改查。

      第一个我们首先看看增,增在SQL里面就是insert,也就是插入,在hibernate中,我们只需要,操纵一个对象进行sava,然后再commit事务,就能实现插入功能,下面给大家具体看看代码,持久类我就不再写了,里面也就是与数据库中的字段要一一对应的东西,要有set,get方法,我直接就写的怎么调用save方法。
//导入所需的包

import org.hibernate.HibernateException;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;public class UserTest {public static void main(String args[]){Configuration cfg = new Configuration().configure(); //获取hibernate的配置信息SessionFactory sf = cfg.buildSessionFactory(); //根据config建立sessionFactorySession ses = sf.openSession(); //factory用于建立session,开启Session,相当于开启JDBC的ConnectionTransaction ts = ses.beginTransaction(); //创建事务的对象tsUser user = new User(); //持久化对象user.setName("kobe");user.setTel("111111111");try {ses.save(user);ts.commit();}catch (HibernateException he){he.printStackTrace();ts.rollback();}finally{ses.close();sf.close();System.out.println("插入成功");}}} 

第二个我们看看删,删在SQL里面是delete,也就是删除,同样在hibernate中,我们也是只需要调用一个对象,调用delete方法,就能进行删除。

import org.hibernate.HibernateException;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;public class DeleteTest {public static void main(String args[]){Configuration cfg = new Configuration().configure();SessionFactory sf = cfg.buildSessionFactory();Session ses = sf.openSession();Transaction ts = ses.beginTransaction();User user = new User();user.setId("8a8308891e9c3ef3011e9c3ef4aa0001");try {ses.delete(user);ts.commit();}catch (HibernateException he){he.printStackTrace();ts.rollback();}finally{ses.close();sf.close();System.out.println("删除成功");}}} 

具体中间的含义参照sava方法,这里我们要注意一点,我们调用删除的时候,他删除的条件,也就是where后面的条件一定是我们xml中配置id,通过这个来进行查找删除,这里尤其值得注意,也就是,我这里调用的user.setId(" ");这句话,他是通过""中的内容进行删除的。第三个我们看看改,改在SQL中update,在hibernate中,我们同样只需要操作一个对象进行更改信息。

import org.hibernate.HibernateException;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;public class UpdateTest {public static void main(String args[]){Configuration cfg = new Configuration().configure();SessionFactory sf = cfg.buildSessionFactory();Session ses = sf.openSession();Transaction ts = ses.beginTransaction();User user = new User();user.setId("8a8308891e9c3ef3011e9c3ef4aa0001");user.setName("kobe24");try {ses.update(user);ts.commit();}catch (HibernateException he){he.printStackTrace();ts.rollback();}finally{ses.close();sf.close();System.out.println("更改成功");}}} 

但是这里我们有需要注意的地方了,如果有的朋友用过这个update就会发现,调用这个方法的时候他更新的不只是你想更新的数据,你不想更新的数据,他也会随着改变,如果你没有给他set值,他就会出现null,或者表格中什么都没有,这里我们就需要用另一种方法了,去更新你想更新的数据,而你不想改变的数据还会保持原来的状态,这里我们就需要调用一个方法。

Session ses = sf.openSession();Transaction ts = ses.beginTransaction();User user = (User)ses.get(User.class,"8a8308891e9c3ef3011e9c3ef4aa0001");user.setName("kobe24");try {ses.update(user);ts.commit(); 

这样我们就会发现,我们只更新了我们想要更新的数据。ses不光光有这一个get方法,相同功能他还有一个load方法,两个方法功能是相同的但是有什么区别呢,区别就是用load方法时候他是从缓存中查找,而我们调用get方法的时候是从数据库中查找,不过get方法他也是先从缓存中查找,如果没有在去数据库中查找。
第三个我们看看查,查在SQL中是select,在hibernate中我们查询的时候有多种方法,这里我就写一种hibernate比较提倡的方法,就是HQL。用这个方法时候我们尤其需要注意的是他其中的from跟的不是表名,而是类名。

package hibernate;import java.util.Iterator;import java.util.List;import org.hibernate.Query;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;public class SeleteTest {public static void main(String args[]){Configuration cfg=new Configuration().configure();SessionFactory sf=cfg.buildSessionFactory();Session ses=sf.openSession();Transaction tx=ses.beginTransaction();User user = new User();Query query = ses.createQuery("from User");List users = query.list(); //序列化Iterator it = users.iterator(); //迭代while (it.hasNext()){user = (User) it.next();System.out.println(user.getName()+" "+user.getTel()+" ");}ses.close();sf.close();}} 



0 0
原创粉丝点击