查询方式:
简单查询;(全查询)
属性查询;(查询某几个属性)
实例化查询;(定义构造函数,封装某些属性)
统计查询;(分组查询)
子查询;(查询嵌套查询)
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();
}
}