zoukankan      html  css  js  c++  java
  • 24.Hibernate-各种类型的查询.md


    目录

    1.查询的类别

    按照查询的方式分为面向对象和非面向对象

    • 面向对象

      • HQL查询:面向对象方式,可以跨数据库,但是需要SQL基础。最常用的方式。查询的对象是类和类的属性,不是表和字段!

      • Criteria查询:面向对象方式,可以跨数据库,不需要SQL基础

    • 非面向对象方式

      • SQL查询:非面向对象方式,不可以跨数据库,可以执行复杂的SQL语句

    2.实例

    上个例子中的App类改造后:

    package per.liyue.code.hibernatehello;
    
    
    import java.util.List;
    
    
    import org.hibernate.Criteria;
    import org.hibernate.Query;
    import org.hibernate.SQLQuery;
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.Transaction;
    import org.hibernate.cfg.Configuration;
    import org.hibernate.criterion.Restrictions;
    import org.junit.Test;
    
    
    public class AppQuery {
    	private static SessionFactory sessionFactory;
    
    
    	static {
    		sessionFactory = new Configuration().configure().buildSessionFactory();
    	}
    
    
    	/*
    	* HQL查询
    	*/
    	@Test
    	public void HQLDemo() {
    		Session session = sessionFactory.openSession();
    		Transaction transcation = session.beginTransaction();
    
    
    		// 面向对象方式:使用SQL方式的查询,但是查询的对象不是表,而是类,查询的条件不是字段,而是类的属性
    		Query query = session.createQuery("from Employee where empName = '李四'");
    		List<Employee> list = query.list();
    		System.out.println("**********使用HQL查询");
    		System.out.println(list);
    
    
    		transcation.commit();
    		session.close();
    		sessionFactory.close();
    	}
    
    
    	/*
    	* Criteria查询
    	*/
    	@Test
    	public void CriteriaDemo() {
    		Session session = sessionFactory.openSession();
    		Transaction transcation = session.beginTransaction();
    
    
    		// 面向对象方式:使用Criteria查询,和SQL无关
    		Criteria criteria = session.createCriteria(Employee.class);
    		// 条件查询类的属性
    		criteria.add(Restrictions.eq("empId", 2));
    		List<Employee> list = criteria.list();
    		System.out.println("**********使用Criteria查询");
    		System.out.println(list);
    
    
    		transcation.commit();
    		session.close();
    		sessionFactory.close();
    	}
    
    
    	/*
    	* SQL查询
    	*/
    	@Test
    	public void SqlDemo() {
    		Session session = sessionFactory.openSession();
    		Transaction transcation = session.beginTransaction();
    
    
    		//非面向对象查询:适合复杂的SQL语句,但是不跨数据库!!!
    		String sql = "select * from employee as e where e.EmpId = 6";
    		//如果不加封装,那么查询出来的list不是想要得到的值
    		//SQLQuery query = session.createSQLQuery(sql);
    		SQLQuery query = session.createSQLQuery(sql).addEntity(Employee.class);
    		List list = query.list();
    		System.out.println("**********使用SQL查询");
    		System.out.println(list);
    
    
    		transcation.commit();
    		session.close();
    		sessionFactory.close();
    	}
    }
    
    
    
  • 相关阅读:
    Delphi XE5 android 蓝牙通讯传输
    Delphi XE5 android toast
    Delphi XE5 android openurl(转)
    Delphi XE5 如何设计并使用FireMonkeyStyle(转)
    Delphi XE5 android 捕获几个事件
    Delphi XE5 android listview
    Delphi XE5 android 黑屏的临时解决办法
    Delphi XE5 android popumenu
    Delphi XE5 android 获取网络状态
    Delphi XE5 android 获取电池电量
  • 原文地址:https://www.cnblogs.com/bugstar/p/8512830.html
Copyright © 2011-2022 走看看