HQL中的select子句

一、select子句简介:



二、以对象数组形式返回选择的属性:

public void testSelectClauseObjectArray(){
           String hql= "select country.ccode,country.cname from Country as country" ;
           Session session=CustomSessionFactory. getSession();
           Query query=session.createQuery(hql);
           List<Object[]> countries=query.list();
            for (Object[] objects:countries){
                System. out .println( "编码:" +objects[0]+ "   名称:" +objects[1]);
           }
           CustomSessionFactory. closeSession();
     }
 


三、以list形式返回选择的属性:

public void testSelectClauseList(){
           String hql= "select new list(country.ccode,country.cname) from Country as country" ;
           Session session=CustomSessionFactory. getSession();
           Query query=session.createQuery(hql);
           List<List> lists=query.list();
            for (List list:lists){
                System. out .println( "编码:" +list.get(0)+ "  名称:" +list.get(1));
           }
           CustomSessionFactory. closeSession();
     }
 


四、以Map的形式返回选择的属性:


public void testSelectClauseMap(){
           String hql= "select new map(country.ccode as ccode,country.cname as cname) from Country as country" ;
           Session session=CustomSessionFactory. getSession();
           Query query=session.createQuery(hql);
           List<Map> maps=query.list();
            for (Map map:maps){
                System. out .println( "编码:" +map.get( "ccode" )+ "     名称:" +map.get( "cname" ));
           }
           CustomSessionFactory. closeSession();
     }
                                  


五、以自定义类型返回选择的属性:


public void testSelectClauseSelf(){
           String hql= "select new Country(country.ccode,country.cname) from Country as country" ;
           Session session=CustomSessionFactory. getSession();
           Query query=session.createQuery(hql);
           List<Country> countries=query.list();
            for (Country country:countries){
                System. out .println( "编码:" +country.getCcode()+ "     名称:" +country.getCname());
           }
           CustomSessionFactory. closeSession();
     }




六、使用distinct关键字去除重复的记录:


public void testDistinct(){
           String hql= "select distinct country.cname from Country as country" ;
           Session session=CustomSessionFactory. getSession();
           Query query=session.createQuery(hql);
           List<Object> list=query.list();
            for (Object country:list){
                System. out .println( "国家:" +country);
           }
           CustomSessionFactory. closeSession();
     }

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值