Hibernate framework-08-01-Hibernate retrieval method


Two previous retrieval methods

Insert picture description here

The navigation object graph must set the association mapping relationship
OID retrieval method must retrieve the data corresponding to this object through the primary key

Three new search methods

Insert picture description here

SQL is generally used when HQL cannot be used

HQL

Introduction

Insert picture description here
HQL is an object-oriented query language, and the fields are all related to the class.
Execute query statements through the Query interface

Insert picture description here

Asterisks cannot appear in HQL statements.
HQL keys are not case-sensitive, but others are case-sensitive

SQL:	select * from user;
HQL:	from User

select A from B basics

package com.hibernate.ui;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.query.Query;

import com.hibernate.entity.Order;
import com.hibernate.entity.User;
import com.hibernate.util.HibernateUtil;

public class Test {
    
    

	public static void main(String[] args) {
    
    
//		saveUserAndOrder();		
//		addOrder();		
//		getUserAndOrders();		
//		deleteUser();
		
		
		testHQL();
		
		HibernateUtil.closeSessionFactory();
	}
	
	
	//HQL检索方式
	private static void testHQL() {
    
    
		Session session = HibernateUtil.openSession();
		//检索表中所有记录的所有字段
		//User不是关键字,是对象类型,所以区分大小写
//		String hql = "from User";
//		String hql = "select u from User u";//与上一句等价,这是起别名
//		import org.hibernate.query.Query;注意引用的包别错了
//		List<User> userList = query.list();
//		System.out.println(userList);
		
		
		
//		//查询某个字段的值
//		String hql = "select u.id from User u";
//		Query query = session.createQuery(hql);
//		//当查询结果中包含多条记录时,使用list方法执行HQL查询
//		List<User> userList = query.list();
//		System.out.println(userList);		
//		//当检索所有记录的某个字段时,结果中List集合元素的类型是该字段类型
//		List<Integer> userIds = query.list();
//		System.out.println(userIds);
		
		
		
		
		
		
		
//		//查询多个字段
//		String hql = "select u.userName, u.password from User u";
//		Query query = session.createQuery(hql);	
//		//当检索多个字段时,结果中List集合元素的类型是Object[]
//		List<Object[]> users = query.list();
//		for(Object[] o : users) {
    
    
//			System.out.println("用户名:" + o[0]);
//			System.out.println("密码:" + o[1]);
//		}
		
		
		//查询多个字段,推荐这种方式
//		String hql = "select u.userName, u.password from User u";
		//在HQL中可以直接调用构造方法(与上一句等价)
		//有条件:在User类里面加上带参数的构造方法
		//定义了带参数的构造方法同时也要定义无参构造方法
		String hql = "select new User(u.userName, u.password) from User u";
		Query query = session.createQuery(hql);	
		List<User> userList = query.list();
		System.out.println(userList);	
	}
}

where clause

Insert picture description here

Insert picture description here

//		//where
//		String hql = "from User where userName = '张三'";
//		Query query = session.createQuery(hql);
//		List<User> users = query.list();
//		System.out.println(users);
		
		
//			//模糊查询
//		String hql = "from User as u where u.userName like '张%'";//起别名使用as关键字,该关键字可以省略
//		String hql = "from User where userName='Tom'";
//		Query query = session.createQuery(hql);
//		List<User> users = query.list();
//		System.out.println(users);
		

HQL alias query

Insert picture description here

HQL polymorphic query

Insert picture description here
Where there is inheritance mapping, the code is up to you.

		//多态查询
		//SQL注入代码
//		String name = "1 or 1 = 1";
//		String password = "1 or 1 = 1";
//		String hql = "from User where userName = " + name + " and password=" + password;
		
		//查询结果中只有一条记录,直接返回单个对象
//		User user = (User) query.uniqueResult();
//		System.out.println(user);

HQL to retrieve a single object

Insert picture description here

		//多态查询
		//SQL注入代码
//		String name = "1 or 1 = 1";
//		String password = "1 or 1 = 1";
//		String hql = "from User where userName = " + name + " and password=" + password;
		
		//查询结果中只有一条记录,直接返回单个对象
//		User user = (User) query.uniqueResult();
//		System.out.println(user);

HQL grouping and sorting

Insert picture description here

		//排序order by,默认升序排列
//		String hql = "from User u order by u.id desc";
//		Query query = session.createQuery(hql);
//		List<User> users = query.list();
//		System.out.println(users);
		
		
		
		
		
//		//group by 分组
//		String hql = "select u.userName, count(u) from User u group by u.userName having u.userName != 'Tom'";
//		Query query = session.createQuery(hql);
//		List<Object[]> users = query.list();
//		for(Object[] o : users) {
    
    
//			System.out.println("用户名:" + o[0]);
//			System.out.println("个数:" + o[1]);
//		}
		
		

HQL parameter binding

Insert picture description here

Insert picture description here

		//HQL传参方式?占位符
//		String hql = "from User where userName = ? and password = ?";
//		Query query = session.createQuery(hql);
//		query.setParameter(0, "张三");
//		query.setParameter(1, "111");
//		System.out.println(query.list());

Insert picture description here

		//按照参数名称进行传参
		//把上面的问号换成冒号+属性 
//		String hql = "from User where userName = :username and password = :password";
//		Query query = session.createQuery(hql);
//		query.setParameter("username", "张三");
//		query.setParameter("password", "111");
//		System.out.println(query.list());

Insert picture description here

	
//		//使用命名参数
//		//HQL语句中命名参数的名称同setProperties方法参数中传入的对象的属性名称要一致
//		String hql = "from User where userName = :userName and password = :password";
//		Query query = session.createQuery(hql);
//		User u = new User();
//		u.setUserName("张三");
//		u.setPassword("111");
//		query.setProperties(u);
//		System.out.println(query.list());
		
		
		
		
		
		
		//setProperties传入Map类型的对象
//		String hql = "from User where userName = :userName and password = :password";
//		Query query = session.createQuery(hql);
//		Map<String, Object> pro = new HashMap<>();
//		pro.put("userName", "张三");
//		pro.put("password", "111");
//		query.setProperties(pro);//HQL语句中命名参数的名称同setProperties方法参数中传入的Map对象的key名称要一致
//		System.out.println(query.list());
		

HQL entity update

Insert picture description here

HQL entity deletion

Insert picture description here

HQL subquery

Insert picture description here

Insert picture description here

Insert picture description here

HQL paging query

Insert picture description here

HQL reference query

Insert picture description here




QBC

Introduction to QBC

Insert picture description here

QBC expression

Insert picture description here

Restrictions class

Insert picture description here

Insert picture description here




Local SQL query

Insert picture description here

Query all user information

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_44627608/article/details/115213049