@Test
public void demo01(){
//1 查询所有
Session session = factory.openSession();
session.beginTransaction();
//1 使用简单类名 , 存在自动导包
// * Customer.hbm.xml
// Query query = session.createQuery("from Customer");
//2 使用全限定类名
Query query = session.createQuery("from com.itheima.a_init.Customer");
List allCustomer = query.list();
for (Customer customer : allCustomer) {
System.out.println(customer);
}
session.getTransaction().commit();
session.close();
}
@Test
public void demo02(){
//2 简单条件查询
Session session = factory.openSession();
session.beginTransaction();
//1 指定数据,cid OID名称
// Query query = session.createQuery("from Customer where cid = 1");
//2 如果使用id,也可以(了解)
// Query query = session.createQuery("from Customer where id = 1");
//3 对象别名 ,格式: 类 [as] 别名
// Query query = session.createQuery("from Customer as c where c.cid = 1");
//4 查询所有项,mysql--> select * from...
Query query = session.createQuery("select c from Customer as c where c.cid = 1");
Customer customer = (Customer) query.uniqueResult();
System.out.println(customer);
session.getTransaction().commit();
session.close();
}
@Test
public void demo04(){
//4 投影
Session session = factory.openSession();
session.beginTransaction();
//1 默认
//如果单列 ,select c.cname from,需要List
@Test
public void demo03(){
//3排序 ,mysql--> select... order by 字段 [asc]|desc ,....
Session session = factory.openSession();
session.beginTransaction();
Query query = session.createQuery("from Customer order by cid desc");
List allCustomer = query.list();
for (Customer customer : allCustomer) {
System.out.println(customer.getCid());
}
session.getTransaction().commit();
session.close();
}
@Test
public void demo05(){
//分页
Session session = factory.openSession();
session.beginTransaction();
Query query = session.createQuery("from Customer");
// * 开始索引 , startIndex 算法: startIndex = (pageNum - 1) * pageSize;
// *** pageNum 当前页(之前的 pageCode)
query.setFirstResult(0);
// * 每页显示个数 , pageSize
query.setMaxResults(2);
List allCustomer = query.list();
for (Customer customer : allCustomer) {
System.out.println(customer.getCid());
}
session.getTransaction().commit();
session.close();
}
@Test
public void demo06(){
/* 6 绑定参数
* 方式1:占位符,使用? 在hql语句替换具体参数
* 设置参数 query.setXxx(int , object)
* 参数1:?位置,从0开始。
* 参数2:实际参数
* 例如:String --> query.setString(int,String)
* 方式2:别名 , 格式 “属性= :别名 ”
* 设置参数 query.setXxx(String,object)
* 参数1:别名
* 参数2:实际参数
* 例如:Integer --> query.setInteger(String,Integer)
* 提供公共设置方法
* setParameter(int|string , Object)
*/
Session session = factory.openSession();
session.beginTransaction();
Integer cid = 1;
//方式1
// Query query = session.createQuery("from Customer where cid = ?");
// query.setInteger(0, cid);
//方式2
Query query = session.createQuery("from Customer where cid = :xxx");
// query.setInteger("xxx", cid);
query.setParameter("xxx", cid);
Customer customer = (Customer) query.uniqueResult();
System.out.println(customer);
session.getTransaction().commit();
session.close();
}
@Test
public void demo07(){
/* 7 聚合函数
*/
Session session = factory.openSession();
session.beginTransaction();
//1
// Query query = session.createQuery("select count(*) from Customer");
//2 别名
// Query query = session.createQuery("select count(c) from Customer c");
//3 oid
Query query = session.createQuery("select count(cid) from Customer");
Long numLong = (Long) query.uniqueResult();
int num = numLong.intValue();
System.out.println(num);
session.getTransaction().commit();
session.close();
}
package com.itheima.c_hql;
import java.util.Arrays;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.junit.Test;
import com.itheima.domain.Customer;
import com.itheima.utils.HibernateUtils;
//HQL详解
public class Demo {
@Test
// Hql查询所有 查询所有
public void fun1(){
Session session = HibernateUtils.openSession();
session.beginTransaction();
//------------------------------------------------
//Query query = session.createQuery("from Customer");
//Query query = session.createQuery("from Customer c");
Query query = session.createQuery("select c from Customer c");
List list = query.list();
System.out.println(list);
//------------------------------------------------
session.getTransaction().commit();
session.close(); // 游离状态
}
@Test
// Hql查询对象的某几个属性 选择查询
public void fun2(){
Session session = HibernateUtils.openSession();
session.beginTransaction();
//------------------------------------------------
//Query query = session.createQuery("select c.name from Customer c"); c.name的name是属性名
Query query = session.createQuery("select c.id , c.name from Customer c");
List list = query.list();
for(Object[] objs : list){
System.out.println(Arrays.toString(objs));
}
//------------------------------------------------
session.getTransaction().commit();
session.close(); // 游离状态
}
@Test
// 投影查询
// 选择查询的基础上,想把查询结果封装到对象中
public void fun3(){
Session session = HibernateUtils.openSession();
session.beginTransaction();
//------------------------------------------------
//Query query = session.createQuery("select c.name from Customer c");
Query query = session.createQuery("select new Customer(c.id,c.name) from Customer c");
//Customer需要一个对应的构造函数
List list = query.list();
System.out.println(list);
//------------------------------------------------
session.getTransaction().commit();
session.close(); // 游离状态
}
@Test
// 投影查询
// 选择查询的基础上,想把查询结果封装到对象中
public void fun4(){
Session session = HibernateUtils.openSession();
session.beginTransaction();
//------------------------------------------------
//Query query = session.createQuery("select c.name from Customer c");
Query query = session.createQuery("select new Customer(c.id,c.name) from Customer c");
List list = query.list();
System.out.println(list);
//------------------------------------------------
session.getTransaction().commit();
session.close(); // 游离状态
}
@Test
// 排序
public void fun5(){
Session session = HibernateUtils.openSession();
session.beginTransaction();
//------------------------------------------------
//Query query = session.createQuery("from Customer c order by c.id desc ");
Query query = session.createQuery("from Customer c order by c.id asc ");
List list = query.list();
System.out.println(list);
//------------------------------------------------
session.getTransaction().commit();
session.close(); // 游离状态
}
@Test
// 分页
public void fun6(){
Session session = HibernateUtils.openSession();
session.beginTransaction();
//------------------------------------------------
Query query = session.createQuery("from Customer c order by c.id asc ");
// limit ?,? setFirstResult,setMaxResults
//setFirstResult: (当前页数-1)*每页最大记录数
query.setFirstResult(1);//从哪个索引开始取数据.包括索引本身的记录
query.setMaxResults(1);//查询出多少条数据 一页取多少条
List list = query.list();
System.out.println(list);
//------------------------------------------------
session.getTransaction().commit();
session.close(); // 游离状态
}
@Test
// 绑定参数
public void fun7(){
Session session = HibernateUtils.openSession();
session.beginTransaction();
//------------------------------------------------
/*Query query = session.createQuery("from Customer c where c.id = ? ");
//参数1:?占位符的索引 ,第一个问号索引为0
query.setInteger(0,2);*/
Query query = session.createQuery("from Customer c where c.id = :haha ");
//参数1: 参数占位符的名称
query.setInteger("haha",2);
Customer c = (Customer) query.uniqueResult();
System.out.println(c);
//------------------------------------------------
session.getTransaction().commit();
session.close(); // 游离状态
}
@Test
// 聚合函数
public void fun8(){
Session session = HibernateUtils.openSession();
session.beginTransaction();
//------------------------------------------------
//Query query = session.createQuery(" select count(*) from Customer c ");
//Query query = session.createQuery(" select avg(c.id) from Customer c ");
//Query query = session.createQuery(" select sum(c.id) from Customer c ");
//Query query = session.createQuery(" select max(c.id) from Customer c ");
Query query = session.createQuery(" select min(c.id) from Customer c ");
Object count = query.uniqueResult();
System.out.println(count);
//------------------------------------------------
session.getTransaction().commit();
session.close(); // 游离状态
}
@Test
// 分组
// group by .. having..
public void fun9(){
Session session = HibernateUtils.openSession();
session.beginTransaction();
//------------------------------------------------
Query query = session.createQuery(" select o.customer, count(o) " +
" from Order o " +
" group by o.customer " +
" having count(o) > 2 ");
List list= query.list();
for(Object[] objs : list){
System.out.println(Arrays.toString(objs));
}
//------------------------------------------------
session.getTransaction().commit();
session.close(); // 游离状态
}
}
1.交叉连接,等效 sql 笛卡尔积
2.隐式内连接,等效 sql 隐式内连接
3.内连接,等效sql内连接
4.迫切内连接,hibernate底层使用 内连接。
5.左外连接,等效sql左外连接
6.迫切左外连接,hibernate底层使用 左外连接
7.右外连接,等效sql右外连接
内连接和迫切内连接?
左外连接和迫切左外连接?
@Test
public void demo08(){
/* 8 链接查询 : 左外连接和迫切左外连接?
* * 左外连接 , left outer join
* 底层使用sql的左外连接,hibernate进行数据自动封装,将一条记录,封装给两个对象(Customer,Order)
* 将两个对象添加到一个对象数组中Object[Customer,Order]
* * 迫切左外链接 left outer join fetch
* 底层使用sql的左外连接,hibernate将一条记录封装给Customer,讲order数据封装Order,并将order关联到Customer
* customer.getOrderSet().add(order)
* 默认查询的数据重复
*/
Session session = factory.openSession();
session.beginTransaction();
//左外连接
// List list = session.createQuery("from Customer c left outer join c.orderSet ").list();
//迫切左外链接 (默认数据重复)
// List list = session.createQuery("from Customer c left outer join fetch c.orderSet ").list();
//迫切左外链接 (去重复)
List list = session.createQuery("select distinct c from Customer c left outer join fetch c.orderSet ").list();
session.getTransaction().commit();
session.close();
}
package com.itheima.c_hql;
import java.util.Arrays;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.junit.Test;
import com.itheima.domain.Customer;
import com.itheima.utils.HibernateUtils;
//HQL详解2-表连接
//内连接 =>
//左外连接 =>
//右外连接 =>
public class Demo2 {
@Test
//交叉连接 => 笛卡尔积
//开发时要避免出现笛卡尔积
public void fun1(){
Session session = HibernateUtils.openSession();
session.beginTransaction();
//------------------------------------------------
Query query = session.createQuery("from Customer c,Order o");
List list = query.list();
for(Object[] objs : list){
System.out.println(Arrays.toString(objs));
}
//------------------------------------------------
session.getTransaction().commit();
session.close(); // 游离状态
}
@Test
//内连接
//隐式内连接 => 在笛卡尔积基础上过滤无效数据
public void fun2(){
Session session = HibernateUtils.openSession();
session.beginTransaction();
//------------------------------------------------
Query query = session.createQuery("from Customer c,Order o where o.customer = c");
List list = query.list();
for(Object[] objs : list){
System.out.println(Arrays.toString(objs));
}
//------------------------------------------------
session.getTransaction().commit();
session.close(); // 游离状态
}
@Test
//内连接
//显式内连接( 非迫切)=> inner join
// List
// Object[] => [Customer,Order]
// 将父 与 子 对象装入数组中分别 返回
public void fun3(){
Session session = HibernateUtils.openSession();
session.beginTransaction();
//------------------------------------------------
//Query query = session.createQuery("from Customer c inner join c.orders ");
Query query = session.createQuery("from Customer c inner join c.orders ");
List list = query.list();
for(Object[] objs : list){
System.out.println(Arrays.toString(objs));
}
//------------------------------------------------
session.getTransaction().commit();
session.close(); // 游离状态
}
@Test
//内连接
//显式内连接(迫切)=> inner join
// List
// 迫切连接会将 子装入父中,组装成一个对象
public void fun4(){
Session session = HibernateUtils.openSession();
session.beginTransaction();
//------------------------------------------------
Query query = session.createQuery("from Customer c inner join fetch c.orders ");
List list = query.list();
for(Object obj : list){
System.out.println(obj);
}
//------------------------------------------------
session.getTransaction().commit();
session.close(); // 游离状态
}
@Test
//左外连接
//left [outer] join
public void fun5(){
Session session = HibernateUtils.openSession();
session.beginTransaction();
//------------------------------------------------
Query query = session.createQuery("from Customer c left outer join c.orders ");
List list = query.list();
for(Object[] objs : list){
System.out.println(Arrays.toString(objs));
}
//------------------------------------------------
session.getTransaction().commit();
session.close(); // 游离状态
}
@Test
//左外连接 迫切
//left [outer] join fetch
public void fun6(){
Session session = HibernateUtils.openSession();
session.beginTransaction();
//------------------------------------------------
Query query = session.createQuery("from Customer c left outer join fetch c.orders ");
List list = query.list();
for(Object obj : list){
System.out.println(obj);
}
//------------------------------------------------
session.getTransaction().commit();
session.close(); // 游离状态
}
@Test
//右外连接
//right [outer] join
public void fun7(){
Session session = HibernateUtils.openSession();
session.beginTransaction();
//------------------------------------------------
Query query = session.createQuery("from Customer c right outer join c.orders ");
List list = query.list();
for(Object[] objs : list){
System.out.println(Arrays.toString(objs));
}
//------------------------------------------------
session.getTransaction().commit();
session.close(); // 游离状态
}
@Test
//右外连接 迫切
//right [outer] join fetch
public void fun8(){
Session session = HibernateUtils.openSession();
session.beginTransaction();
//------------------------------------------------
Query query = session.createQuery("from Customer c right outer join fetch c.orders ");
List list = query.list();
for(Object obj : list){
System.out.println(obj);
}
//------------------------------------------------
session.getTransaction().commit();
session.close(); // 游离状态
}
}
l 获得
全局:
session.getNamedQuery("queryName")
局部:
session.getNamedQuery("className.queryName") 需要使用类的全限定名称
@Test
public void demo09(){
/* 9 命名查询
*/
Session session = factory.openSession();
session.beginTransaction();
//全局
//List list = session.getNamedQuery("findAll").list();
//局部
List list = session.getNamedQuery("com.itheima.a_init.Customer.findAll").list();
System.out.println(list.size());
session.getTransaction().commit();
session.close();
}
package com.itheima.c_hql;
import java.util.Arrays;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.junit.Test;
import com.itheima.domain.Customer;
import com.itheima.utils.HibernateUtils;
//HQL详解2--命名查询
public class Demo3 {
@Test
//找到局部配置的HQL语句
public void fun1(){
Session session = HibernateUtils.openSession();
session.beginTransaction();
//------------------------------------------------
//从com.itheima.domain.Customer 的 hbm文件中找
Query query = session.getNamedQuery("com.itheima.domain.Customer.bcd");
List list = query.list();
System.out.println(list);
//------------------------------------------------
session.getTransaction().commit();
session.close(); // 游离状态
}
@Test
//找到全局配置的HQL语句
public void fun2(){
Session session = HibernateUtils.openSession();
session.beginTransaction();
//------------------------------------------------
//从com.itheima.domain.Customer 的 hbm文件中找
Query query = session.getNamedQuery("abc");
List list = query.list();
System.out.println(list);
//------------------------------------------------
session.getTransaction().commit();
session.close(); // 游离状态
}
}