hql语句中的select字句和from 字句

package com.imooc.model;

import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.Session;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.imooc.util.HibernateSessionFactory;

public class SellerTest {
    private Session session = null;
    
    @Test
    public void testSelectClauseSelf(){
        String hql = " select new Seller(s.name,s.tel,s.address) from Seller s ";
        Query query = session.createQuery(hql);
        List<Seller> sellers = query.list();
        
        for(Seller seller : sellers){
            System.out.println("name: "+seller.getName());
            System.out.println("tel:"+seller.getTel());
            System.out.println("address:"+seller.getAddress());
        }
    }
    
    @Test
    public void testSelectClauseMap(){
        String hql = " select new map(s.name,s.tel,s.address) from Seller s ";
        Query query =session.createQuery(hql);
        
        List<Map> maps = query.list();
        for(Map map : maps){
            System.out.println("name:"+map.get("0"));
            System.out.println("tel:"+map.get("1"));
            System.out.println("address:"+map.get("2"));
        }
    }
    
    @Test
    public void testSelectClauseList(){
        String hql = " select new list(s.name,s.tel,s.address) from Seller s ";
        
        Query query = session.createQuery(hql);
        List<List> lists = query.list();
        
        for(List list : lists){
            System.out.println("name : "+list.get(0));
            System.out.println("tel:"+list.get(1));
            System.out.println("address:"+list.get(2));
        }
    }
    
    /*
     * 1.name 2.tel 3.address 4.star
     */
    @Test
    public void testSelectClauseObjectArray(){
        String hql = " select s.name from Seller s ";
        
        Query query = session.createQuery(hql);
        
        List<Object> list = query.list();
        
        for(Object obj : list){
            System.out.println("name:"+obj);
        }
    }
    /**
     * new一个新对象
     * @Description:
     */
    @Test
    public void testSelectMap(){
        String hql="select new map(s.name as name,s.tel as tel) from Seller s";
        Query query = session.createQuery(hql);
        List<Map<String,Object>> list = query.list();
        for (Map<String, Object> map : list) {
            System.out.println(map.get("name")+"  "+map.get("tel"));
        }
    }
/**
 * 测试from字句    
 * @Description:
 */
    @Test
    public void testFromClause(){
        String hql = " from Seller  s ";
        
        Query query = session.createQuery(hql);
        
        List<Seller> sellers = query.list();
        for(Seller seller : sellers){
            System.out.println("name:"+seller.getName());
        }
    }
    /**
     * 使用list进行传递里面的参数
     * @Description:
     */
    @Test
    public void testListFromClause(){
        String hql="select new list(s.name,s.tel) from Seller s";
        Query query=session.createQuery(hql);
        List<List<Object>> list=query.list();
        for (List<Object> list2 : list) {
            for (Object object : list2) {
                System.out.println(object);
            }
        }
    }
/**
 * //当这里的值只能是一个的时候在List的集合中应该设置为Object    
 * @Description:创建的是distinct 关键字的作用
 * distinct这个关键字加入到里面中可以去除重复的内容
 */
    @Test
    public void testListFromDistict(){
        String hql="select distinct c.sex from Customer as c";
        Query query=session.createQuery(hql);
        List<Object> list=query.list();
        for (Object object : list) {
            System.out.println(object);
        }
    }
    @Before
    public void setUp() throws Exception {
        session = HibernateSessionFactory.getCurrentSession();
    }

    @After
    public void tearDown() throws Exception {
        session.close();
    }

}

 

posted @ 2016-08-03 21:05  lonecloud  阅读(2354)  评论(0编辑  收藏  举报
我的博客即将同步至 OSCHINA 社区,这是我的 OSCHINA ID:lonecloud,邀请大家一同入驻:https://www.oschina.net/sharing-plan/apply