一、通过oid查询
@Test
public void func1(){
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
// ----------------------------------------
Customer cs = session.get/load(Customer.class, 1L); // load是,等到用的时候才知晓
System.out.println(cs);
// ----------------------------------------
tx.commit();
session.close();
}
二、HQL查询
1、基本查询
@Test
public void func1(){
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
// ----------------------------------------
String hql = "from Customer";
Query query = session.createQuery(hql);
List<Customer> list = query.list(); // 获取所有数据
//Customer cs = (Customer) query.uniqueResult(); // 获取单条数据(只有一条才能使用)
System.out.println(list);
// ----------------------------------------
tx.commit();
session.close();
}
2、条件查询
@Test
public void func1(){
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
// ----------------------------------------
String hql1 = "from Customer where cust_id=?"; // ?占位符
String hql2 = "from Customer where cust_id=:id"; // 命名占位符
Query query = session.createQuery(hql2);
// query.setParameter(0,2L);
query.setParameter("id",2L);
// List<Customer> list = query.list(); // 获取所有数据
Customer cs = (Customer) query.uniqueResult(); // 获取单条数据(只有一条才能使用)
System.out.println(cs);
// ----------------------------------------
tx.commit();
session.close();
}
3、排序
@Test
public void func1(){
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
// ----------------------------------------
String hql1 = "from Customer order by cust_id asc"; // 正序排
String hql2 = "from Customer order by cust_id desc"; // 逆序排
Query query = session.createQuery(hql1);
List<Customer> list = query.list();
System.out.println(list);
// ----------------------------------------
tx.commit();
session.close();
}
4、分页查询
@Test
public void func1(){
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
// ----------------------------------------
String hql = "from Customer";
Query query = session.createQuery(hql);
query.setFirstResult(1);
query.setMaxResults(3);
List<Customer> li = query.list();
System.out.println(li);
// ----------------------------------------
tx.commit();
session.close();
}
5、聚合查询
@Test
public void func1(){
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
// ----------------------------------------
String hql1 = "select count (*) from Customer "; // 返回影响数(row)
String hql2 = "select max (cust_id) from Customer "; // 最大值
String hql3 = "select min (cust_id) from Customer "; // 最小值
String hql4 = "select avg (cust_id) from Customer "; // 平均值
String hql5 = "select sum (cust_id) from Customer "; // 求和
Query query = session.createQuery(hql4);
Number num = (Number) query.uniqueResult();
System.out.println(num);
// ----------------------------------------
tx.commit();
session.close();
}
6、投影查询
@Test
public void func1(){
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
// ----------------------------------------
String hql = "select cust_id,cust_name from Customer ";
Query query = session.createQuery(hql);
List li = query.list();
System.out.println(li);
// ----------------------------------------
tx.commit();
session.close();
}
7、多表查询
SQL的多表查询
交叉连接-笛卡尔积(最好不要用,会查出重复数据)
内连接
|-隐式内连接
select * from A,B where b.aid=a.id
|-显示内连接
select * from A inner join B on b.aid=a.id
外连接
|-左外
select * from A left [outer] join B on b.aid=a.id
|-右外
select * from A right [outer] join B on b.aid=a.id
HQL的多表查询(一般不用,还不如用原生SQL呢)
1、内连接
@Test
public void func1(){
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
// ----------------------------------------
String hql = "from Customer c inner join c.linkMens";
Query query = session.createQuery(hql);
List<Object[]> li = query.list();
for(Object[] o : li){
System.out.println(Arrays.toString(o));
}
// ----------------------------------------
tx.commit();
session.close();
}
[Customer [cust_id=1, cust_name=鑫哥论文], LinkMan{lkm_id=5, lkm_name='tom'}]
[Customer [cust_id=1, cust_name=鑫哥论文], LinkMan{lkm_id=6, lkm_name='计éå®'}]
[Customer [cust_id=1, cust_name=鑫哥论文], LinkMan{lkm_id=7, lkm_name='计震宇'}]
2、内连接迫切
查询语句上多了个fetch,返回的是Customer对象
public void func1(){
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
// ----------------------------------------
String hql = "from Customer c inner join fetch c.linkMens";
Query query = session.createQuery(hql);
List<Customer> li = query.list();
System.out.println(li);
// ----------------------------------------
tx.commit();
session.close();
}
[Customer{cust_id=1, cust_name='百度公司', linkMens=[LinkMan{lkm_id=1, lkm_name='于佳鑫'}, LinkMan{lkm_id=6, lkm_name='计éå®'}]}, Customer{cust_id=2, cust_name='谷歌公司', linkMens=[LinkMan{lkm_id=3, lkm_name='张宝岩'}]}, Customer{cust_id=3, cust_name='360', linkMens=[LinkMan{lkm_id=4, lkm_name='tom'}]}, Customer{cust_id=4, cust_name='快播', linkMens=[LinkMan{lkm_id=5, lkm_name='tom'}]}, Customer{cust_id=1, cust_name='百度公司', linkMens=[LinkMan{lkm_id=1, lkm_name='于佳鑫'}, LinkMan{lkm_id=6, lkm_name='计éå®'}]}]
3、左/右 外连接(迫切)
@Test
public void func1(){
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
// ----------------------------------------
String hql = "from Customer c left join c.linkMens";
Query query = session.createQuery(hql);
List<Object []> li = query.list();
for (Object [] o : li){
System.out.println(Arrays.toString(o));
}
// ----------------------------------------
tx.commit();
session.close();
}
三、Criteria查询
1、基本查询
public void func1(){
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
// ----------------------------------------
Criteria criteria = session.createCriteria(Customer.class);
List list = criteria.list();
System.out.println(list);
// ----------------------------------------
tx.commit();
session.close();
}
2、条件查询
public void func1(){
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
// ----------------------------------------
Criteria criteria = session.createCriteria(Customer.class);
criteria.add(Restrictions.eq("cust_id",2L)); // 添加查询条件
//List list = criteria.list();
Customer result = (Customer) criteria.uniqueResult();
System.out.println(result);
// ----------------------------------------
tx.commit();
session.close();
}
3、分页查询
public void func1(){
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
// ----------------------------------------
Criteria criteria = session.createCriteria(Customer.class);
criteria.setFirstResult(2);
criteria.setMaxResults(3);
List<Customer> list = criteria.list();
System.out.println(list);
// ----------------------------------------
tx.commit();
session.close();
}
4、排序
public void func1(){
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
// ----------------------------------------
Criteria criteria = session.createCriteria(Customer.class);
criteria.addOrder(Order.asc("cust_id")); // 正序
criteria.addOrder(Order.desc("cust_id")); // 逆序
List<Customer> list = criteria.list();
System.out.println(list);
// ----------------------------------------
tx.commit();
session.close();
}
5、聚合运算
public void func1(){
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
// ----------------------------------------
Criteria criteria = session.createCriteria(Customer.class);
criteria.setProjection(Projections.rowCount()); // 查询总和
Number num = (Number) criteria.uniqueResult();
System.out.println(num);
// ----------------------------------------
tx.commit();
session.close();
}
离线查询
非离线
离线(dao层可以不用变,我们在前面构造好查询条件就行了)
代码实现
四、查询优化
load方法,当使用时,才进行SQL查询
实际上,load方法是将Customer对象变成超级Customer对象,可以查询数据库,当调用后(查询数据库后),变成了普通的Customer对象
public void func1(){
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
// ----------------------------------------
Customer load = session.load(Customer.class, 2L);
System.out.println(load);
// ----------------------------------------
tx.commit();
session.close();
}
它实际上也是依赖session的,所以session.close()后,就会报错
所以我们可以使用 filter 来解决
动态代理:(打印对象时有$就是代理对象)
给对象搞代理的目的:对方法改造或增强
连接池:
目的:改变连接对象的关闭方法(不能让连接真的关闭 应该放回连接池),对connection进行代理
解决中文乱码:
对request对象进行代理,改造getParameterMap()...方法,变成没有乱码的
类级别 延迟加载:
将Customer对象变成超级Customer对象,可以查询数据库