• 企业400电话
  • 微网小程序
  • AI电话机器人
  • 电商代运营
  • 全 部 栏 目

    企业400电话 网络优化推广 AI电话机器人 呼叫中心 网站建设 商标✡知产 微网小程序 电商运营 彩铃•短信 增值拓展业务
    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建立sessionFactory
    Session ses = sf.openSession(); //factory用于建立session,开启Session,相当于开启JDBC的Connection
    Transaction ts = ses.beginTransaction(); //创建事务的对象ts
    User 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();
    }
    }
    您可能感兴趣的文章:
    • Hibernate中实现增删改查的步骤详解
    • hibernate增删改查操作代码
    上一篇:java Lucene 中自定义排序的实现
    下一篇:jsp 定制标签(Custom Tag)
  • 相关文章
  • 

    © 2016-2020 巨人网络通讯 版权所有

    《增值电信业务经营许可证》 苏ICP备15040257号-8

    hibernate中的增删改查实现代码 hibernate,中的,增,删改,查,