zoukankan      html  css  js  c++  java
  • Hibernate的基本功能:对数据库的增删改查(创建对象实例)

    一、通过实例化的对象向数据库添加新记录

    package com.yh.test;
    
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.Transaction;
    import org.hibernate.cfg.Configuration;
    import org.junit.Test;
    
    import com.yh.entity.Buyer;
    
    public class Demo {
    
        @Test
        public void doAdd() {
            SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
            Session session = sessionFactory.openSession();
            Transaction transaction = session.beginTransaction();
            Buyer buyer = new Buyer();
            buyer.setUsername("yehuan");
            buyer.setPassword("123");
            session.save(buyer);
            transaction.commit();
            session.close();
            sessionFactory.close();
        }
    }

    二、通过实例化的对象更新数据库中对应的记录

    代码与添加类似,仅将save()方法改成update()方法,更新时以主键映射的属性为条件。

    注:saveOrUpdate()方法两者都适用。

    三、通过实例化的对象删除数据库中对应的记录

    代码与添加类似,仅将save()方法改成delete()方法,删除时以主键对应属性为条件。

    四、查询数据库中的记录

    1.根据主键对应属性查询(可用于实例化单个对象)

    通过get()和load()方法。

    区别:

    get()方法会直接创建实际对象,查找不到则抛出异常NullPointerException。

    load()方法会返回一个代理对象,里面只保存了主键对应属性的值,其他属性只在用的时候再向数据库查询,查找不到则抛出异常ObjectNotFoundException。

    @Test
    public void doSingleQuery() {
        SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
        Session session = sessionFactory.openSession();
        Buyer buyer = session.load(Buyer.class,"yehuan1");
        System.out.println(buyer.getPassword());
        session.close();
        sessionFactory.close();
    }

    2.根据其他属性查询(可用于实例化多个对象)

    @Test
    public void doSimpleSingleQuery() {
        SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
        Session session = sessionFactory.openSession();
        Transaction transaction = session.beginTransaction();
        Criteria criteria = session.createCriteria(Buyer.class);
        criteria.add(Restrictions.eq("username", "yehuan1"));
        @SuppressWarnings("unchecked")
        List<Buyer> list = criteria.list();
        for(Buyer b:list){
            System.out.println(b); // 调用Buyer类中的toString()方法
        }
        transaction.commit();
        session.close();
        sessionFactory.close();
    }

    3.查询类对应表的所有记录(可用于实例化多个对象)

    通过createQuery()和createSQLQuery()方法。

    区别:

    createQuery()用的hql语句进行查询,以hibernate生成的Bean为对象装入list返回。

    createSQLQuery()用sql语句查询,以对象数组进行存储,通过addEntity()方法可将返回结果改成和createQuery()一样。

    @Test
    public void doAllQuery() {
        SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
        Session session = sessionFactory.openSession();
        Transaction transaction = session.beginTransaction();
        String hql="from Buyer";
        Query query = session.createQuery(hql);
        @SuppressWarnings("unchecked")
        List<Buyer> list = query.list();
        for(Buyer b:list){
            System.out.println(b); // 调用Buyer类中的toString()方法
        }
        transaction.commit();
        session.close();
        sessionFactory.close();
    }
  • 相关阅读:
    Java入门总结
    Java安装JDK
    ExcelPackage 读取、导出excel
    An error occurred while starting the application.
    EF core2.1+MySQL报错'Void Microsoft.EntityFrameworkCore.Storage.Internal.RelationalParameterBuilder..ctor(Microsoft.EntityFrameworkCore.Storage.IRelationalTypeMapper)
    数据表转换类
    The requested URL /xxxx.html was not found on this server
    .htaccess: Invalid command 'Header', perhaps misspelled or defined by a module not included in the server configuration
    B2B多商铺初期权限数据库设计
    数据库持久化比较
  • 原文地址:https://www.cnblogs.com/YeHuan/p/11273082.html
Copyright © 2011-2022 走看看