• 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();
        }
    
    }
  • 相关阅读:
    转载: Ubuntu 在命令下,安装中文环境的方法。
    java复制文件范例代码
    Cesium-entiy闪烁范例
    转载:贝塞尔曲线计算公式
    转载: utm坐标和经纬度相互转换
    arcgis 地图如何转到supermap平台
    Linux查看修改文件句柄数
    转载:Linux目录文件的权限查看与修改
    欧拉系统-登陆 SSH 出现 Access Denied 错误
    关于 nodejs sequelize 事务批量拆分
  • 原文地址:https://www.cnblogs.com/lonecloud/p/5734506.html
Copyright © 2020-2023  润新知