熟悉HQL语句的基本语法; 掌握Query接口的使用; 掌握各种查询结果的访问方法。

查询方式:

    简单查询;(全查询)

    属性查询;(查询某几个属性)

    实例化查询;(定义构造函数,封装某些属性)

    统计查询;(分组查询)

    子查询;(查询嵌套查询)


1、简单查询

@Test  

public void testQueryGoods(){

SessionFactory sf = null;
Session session = null;
Transaction ts = null;
try {

sf = HibernateUtil.getSessionFactory();
session = sf.getCurrentSession();
ts = session.beginTransaction();
Query query = session.createQuery("from Goods");
List goods = query.list();
System.out.println("序号-产品名字-产品价格");
for(int i = 0; i < goods.size();i++){
Goods good = (Goods)goods.get(i);	//重点
System.out.println(" "+i+"----"+good.getName()+"----"+good.getPrice());
}
ts.commit();
} catch (HibernateException e) {
if(ts != null)
{
ts.rollback();
}
e.printStackTrace();
}
}


 

2、属性查询(查询商品名)

@Test  

public void testQueryGoodsFollowByT_name(){
SessionFactory sf = null;
Session session = null;
Transaction ts = null;
try {
sf = HibernateUtil.getSessionFactory();
session = sf.getCurrentSession();
ts = session.beginTransaction();
Query query = session.createQuery("select name from Goods ");
List t_names = query.list();
System.out.println("序号-产品名字");
for(int i = 0; i < t_names.size();i++){
String t_name = (String)t_names.get(i);
System.out.println(" "+i+"----"+t_name);
}
ts.commit();
} catch (HibernateException e) {
if(ts != null)
{
ts.rollback();
}
e.printStackTrace();
}
}


 

3、实例化查询

@Test
public void testQueryObjectGoods(){
SessionFactory sf = null;  掌握各种查询结果的访问方法
Session session = null;
Transaction ts = null;
try {
sf = HibernateUtil.getSessionFactory();
session = sf.getCurrentSession();
ts = session.beginTransaction();
Query query = session.createQuery("select new Goods(id,name,price) from Goods");
List goods = query.list();
for(int i = 0; i < goods.size();i++){
Goods good = (Goods)goods.get(i);
System.out.println(good.getName()+"价格为:"+good.getPrice());
}
ts.commit();
} catch (HibernateException e) {
if(ts != null)
{
ts.rollback();
}
e.printStackTrace();
}
} 

 

4、统计查询

@Test  
//统计查询
public void testCountQueryGoods(){
SessionFactory sf = null;
Session session = null;
Transaction ts = null;
try {
sf = HibernateUtil.getSessionFactory();
session = sf.getCurrentSession();
ts = session.beginTransaction();
//取得记录集的大小
Query query = session.createQuery("select count(*) from Goods");
Object count = (Object)query.uniqueResult();
System.out.println("共有"+count+"条记录");
//取得某些属性的平均值
query = session.createQuery("select avg(price) from Goods");
//Object average =(Object)query.uniqueResult();
Number average =(Number)query.uniqueResult();
System.out.println("平均价格为:"+average);
ts.commit();
} catch (HibernateException e) {
if(ts != null)
{
ts.rollback();
}
e.printStackTrace();
}
}


 

5、子查询

@Test  
public void testQuery1Goods(){
SessionFactory sf = null;
Session session = null;
Transaction ts = null;
try {
sf = HibernateUtil.getSessionFactory();
session = sf.getCurrentSession();
ts = session.beginTransaction();
Query query = session.createQuery("from Goods where price < (select avg(price) from Goods)");
List goods = query.list();
System.out.println("序号-产品名字-产品价格");
for(int i = 0; i < goods.size();i++){
Goods good = (Goods)goods.get(i);
System.out.println(" "+i+"----"+good.getName()+"----"+good.getPrice());
}
ts.commit();
} catch (HibernateException e) {
if(ts != null)
{
ts.rollback();
}
e.printStackTrace();
}
}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值