6.0、Hibernate-HQL
HQL 全称(Hibernate Query Language),是 Hibernate 框架提供的一种查询机制,他和 SQL 类似,不同的是 HQL 是面向对象的查询语句,让开发者能够以面向对象的思想来编写查询语句,对 Java 编程是一种友好的方式;HQL 不能直接参与数据库的交互,中间层语言 -> Java ---> HQL ---> Hibernate ---> SQL ---> DB;H
6.0、Hibernate-HQL
HQL 全称(Hibernate Query Language),是 Hibernate 框架提供的一种查询机制,他和 SQL 类似,不同的是 HQL 是面向对象的查询语句,让开发者能够以面向对象的思想来编写查询语句,对 Java 编程是一种友好的方式;
HQL 不能直接参与数据库的交互,中间层语言 -> Java ---> HQL ---> Hibernate ---> SQL ---> DB;
HQL 只能完成查询、修改、删除,新增是无法操作的;
1、查询对象
查询表中所有数据,自动完成对象的封装,返回 List 集合;
HQL 进行查询,from 关键字后面不能写表名,必须写对应的实体类名;
参考代码,如下所示->
import com.hkl.pojo.People;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query;
import java.util.List;
public class Test9 {
public static void main(String[] args) {
// 创建configuration
Configuration configuration = new Configuration().configure();
// 获取sessionFactory
SessionFactory sessionFactory = configuration.buildSessionFactory();
// 获取Session
Session session = sessionFactory.openSession();
String hql = "from People";
Query query = session.createQuery(hql);
List<People> list = query.list();
for(People people:list) {
System.out.println(people);
}
session.close();
}
}
2、分页查询
HQL 分页查询可以通过调用 query 的方法来完成;
1、setFirstResult() -设置起始下标;
2、setMaxResult() -设置截取长度;
参考代码,如下所示->
import com.hkl.pojo.People;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query;
import java.util.List;
public class Test9 {
public static void main(String[] args) {
// 创建configuration
Configuration configuration = new Configuration().configure();
// 获取sessionFactory
SessionFactory sessionFactory = configuration.buildSessionFactory();
// 获取Session
Session session = sessionFactory.openSession();
String hql = "from People";
Query query = session.createQuery(hql);
query.setFirstResult(1);
query.setMaxResults(3);
List<People> list = query.list();
for(People people:list) {
System.out.println(people);
}
session.close();
}
}
3、where 条件查询
HQL 直接追加 where 关键字作为查询条件,与 SQL 没有区别;
参考代码,如下所示 ->
import com.hkl.pojo.People;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query;
import java.util.List;
public class Test9 {
public static void main(String[] args) {
// 创建configuration
Configuration configuration = new Configuration().configure();
// 获取sessionFactory
SessionFactory sessionFactory = configuration.buildSessionFactory();
// 获取Session
Session session = sessionFactory.openSession();
String hql = "from People where id = 6";
Query query = session.createQuery(hql);
People people = (People) query.list().get(0);
System.out.println(people);
session.close();
}
}
query.list() 返回一个集合,此时集合中只有一个对象,通过下标 0 取出该对象;
4、模糊查询
查询名称包含 " 澜 " 的所有记录;
参考代码,如下所示 ->
import com.hkl.pojo.People;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query;
import java.util.List;
public class Test9 {
public static void main(String[] args) {
// 创建configuration
Configuration configuration = new Configuration().configure();
// 获取sessionFactory
SessionFactory sessionFactory = configuration.buildSessionFactory();
// 获取Session
Session session = sessionFactory.openSession();
String hql = "from People where name like '%澜%'";
Query query = session.createQuery(hql);
List<People> list = query.list();
for(People people:list) {
System.out.println(people);
}
session.close();
}
}
5、order by
按照 id 进行排序;
参考代码,如下所示 ->
import com.hkl.pojo.People;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query;
import java.util.List;
public class Test9 {
public static void main(String[] args) {
// 创建configuration
Configuration configuration = new Configuration().configure();
// 获取sessionFactory
SessionFactory sessionFactory = configuration.buildSessionFactory();
// 获取Session
Session session = sessionFactory.openSession();
String hql = "from People order by id desc";
Query query = session.createQuery(hql);
List<People> list = query.list();
for(People people:list) {
System.out.println(people);
}
session.close();
}
}
asc 是升序排列,desc 是降序排列;
6、查询实体对象的属性
参考代码,如下所示 ->
import com.hkl.pojo.People;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query;
import java.util.List;
public class Test9 {
public static void main(String[] args) {
// 创建configuration
Configuration configuration = new Configuration().configure();
// 获取sessionFactory
SessionFactory sessionFactory = configuration.buildSessionFactory();
// 获取Session
Session session = sessionFactory.openSession();
String hql = "select name from People where id = 6";
Query query = session.createQuery(hql);
List<People> list = query.list();
System.out.println(people);
session.close();
}
}
7、占位符
参考代码,如下所示 ->
import com.hkl.pojo.People;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query;
import java.util.List;
public class Test9 {
public static void main(String[] args) {
// 创建configuration
Configuration configuration = new Configuration().configure();
// 获取sessionFactory
SessionFactory sessionFactory = configuration.buildSessionFactory();
// 获取Session
Session session = sessionFactory.openSession();
String hql = "from People where name = :name";
Query query = session.createQuery(hql);
query.setString("name","小澜");
List<People> list = query.list();
for(People people:list) {
System.out.println(people);
}
session.close();
}
}
8、级联查询
参考代码,如下所示 ->
import com.hkl.pojo.Customer;
import com.hkl.pojo.Order;
import com.hkl.pojo.People;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query;
import java.util.List;
public class Test9 {
public static void main(String[] args) {
// 创建configuration
Configuration configuration = new Configuration().configure();
// 获取sessionFactory
SessionFactory sessionFactory = configuration.buildSessionFactory();
// 获取Session
Session session = sessionFactory.openSession();
String hql1 = "from Customer where name = :name";
Query query1 = session.createQuery(hql1);
query1.setString("name","小澜");
Customer customer = (Customer) query1.uniqueResult();
String hql2 = "from Order where customer = :customer";
Query query2 = session.createQuery(hql2);
query2.setString("customer",customer);
List<Order> list = query2.list();
for(Order orders:list) {
System.out.println(orders);
}
session.close();
}
}
更多推荐
所有评论(0)