ssh框架 hibernate 多表查询出的vo对象如何处理

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ssh框架 hibernate 多表查询出的vo对象如何处理相关的知识,希望对你有一定的参考价值。

网上关于多表查询结果集的处理五花八门,没有一个比较完整的版本,希望各位大侠给个完整的代码,或者我可以贴出代码,哪位大侠帮我调一下,比较急,在线等!
public List queryByUserNameOrdStatus(String userName)

return hibernateTemplate.find("select os.ordStatusId, os.ordDate, os.status, os.orderTotal, os.orderCost,ru.userName from OrdStatus os inner join RegUser ru on os.regUserId=ru.regUserId where ru.userName=?", userName);

返回的是对象时一个新的对象,如何处理!QQ923192899

参考技术A 把返回值该成 List<OrdStatus> 然后通过两个表的关联,输出数据!你现在的返回值是一个object类型,需要遍历两次数组才能输出! 参考技术B List list=queryByUserNameOrdStatus("username"); 
for (Object obj : list) 
Object[] o = (Object[]) obj;
System.out.println(o[0]);
...

差不多就这样。

本回答被提问者采纳

hibernate框架学习之数据查询(HQL)helloworld

package cn.itcast.h3.hql;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import cn.itcast.h3.hql.vo.StudentModel;
import cn.itcast.h3.hql.vo.TeacherModel;
import cn.itcast.h3.util.HibernateUtil;

public class HQLApp {
    //测试简单查询
    void testSimple(){
        Session s = HibernateUtil.getSession();
        
        //1.HQL
        String hql = "from TeacherModel";
        //2.Query
        Query q = s.createQuery(hql);
        //3.获取查询结果
        List<TeacherModel> queryList = q.list();
        for(TeacherModel tm:queryList){
            System.out.println(tm);
        }
        
        s.close();
    }
    //链式格式
    void testLinked(){
        Session s = HibernateUtil.getSession();
        
        //3.获取查询结果
        List<TeacherModel> queryList = s.createQuery( "from TeacherModel where uuid <=:uuid").setFirstResult(1).setMaxResults(3).setLong("uuid", 3L).list();
        
        for(TeacherModel tm:queryList){
            System.out.println(tm);
        }
        
        s.close();
    }
    //别名格式
    void testAlias(){
        Session s = HibernateUtil.getSession();
        
        //1.HQL
        String hql = "select um.nick from TeacherModel um";
        //2.Query
        Query q = s.createQuery(hql);
        //3.获取查询结果
        List<String> queryList = q.list();
        for(String tm:queryList){
            System.out.println(tm);
        }
        
        s.close();
    }
    //聚合函数
    void testFunctoin(){
        Session s = HibernateUtil.getSession();
        //1.HQL
        String hql = "select sum(age) from StudentModel";
        //2.Query
        Query q = s.createQuery(hql);
        Long avgAge = (Long) q.uniqueResult();
        System.out.println(avgAge);
        s.close();
    }
    //按条件查询
    void testEntityQuery(){
        Session s = HibernateUtil.getSession();
        //1.HQL
        //编号为4的老师有哪些学生
//        String hql = "from StudentModel where  teacher.uuid= :uuid";
//        //2.Query
//        Query q = s.createQuery(hql);
//        q.setLong("uuid",4L);
        
        String hql = "from StudentModel where teacher = :teacher";
        Query q = s.createQuery(hql);
        TeacherModel tm = new TeacherModel();
        tm.setUuid(4L);
//        tm.setTeacherName("李若亮");
        //为hql赋值对象参数
        q.setEntity("teacher", tm);
        List<StudentModel> queryList = q.list();
        for(StudentModel sm:queryList){
            System.out.println(sm);
        }
    }
    //无条件查询
    void testNoCondQuery(){
        Session s = HibernateUtil.getSession();
        
        String hql = "from TeacherModel where teacherName = nick";
        Query q = s.createQuery(hql);
        List<TeacherModel> queryList = q.list();
        for(TeacherModel sm:queryList){
            System.out.println(sm);
        }
    }
    //投影查询
    void testOtherQuery(){
        Session s = HibernateUtil.getSession();
        
        String hql = "select new TeacherModel(teacherName,nick) from TeacherModel";
        Query q = s.createQuery(hql);
        List<TeacherModel> queryList = q.list();
        for(TeacherModel sm:queryList){
            System.out.println(sm);
        }
    }
    //投影查询list
    void testOtherQuery2(){
        Session s = HibernateUtil.getSession();
        
        String hql = "select new list(teacherName,nick) from TeacherModel";
        Query q = s.createQuery(hql);
        List<List<String>> queryList = q.list();
        for(List<String> sm:queryList){
            for(String sa:sm){
                System.out.println(sa);
            }
        }
    }
    //查询分组
    void testGroupQuery(){
        Session s = HibernateUtil.getSession();
        
        String hql = "select skill,count(skill) from StudentModel group by skill";
        Query q = s.createQuery(hql);
        List<Object[]> queryList = q.list();
        for(Object[] objs:queryList){
            for(Object obj:objs){
                System.out.print(obj+"	");
            }
            System.out.println();
        }
    }
    //查询分组
    void testObejcts(){
        Session s = HibernateUtil.getSession();
        
        String hql = "from java.lang.Object";
        Query q = s.createQuery(hql);
        List<Object> queryList = q.list();
        for(Object obj:queryList){
            System.out.println(obj);
        }
    }
    //配置格式的查询
    void testConfQuery(){
        Session s = HibernateUtil.getSession();
        
        Query q = s.getNamedQuery("getAll");
        q.setString("name", "李若亮");
        List<Object> queryList = q.list();
        for(Object obj:queryList){
            System.out.println(obj);
        }
    }
    
    public static void main(String[] args) {
        new HQLApp().testConfQuery();
    }
}

 

以上是关于ssh框架 hibernate 多表查询出的vo对象如何处理的主要内容,如果未能解决你的问题,请参考以下文章

[ SSH框架 ] Hibernate框架学习之三

Hibernate多表联合纯sql查询返回结果集的问题?

ssh中多表查询及Path expected for join的异常处理

SSH的配置文件

hibernate框架学习之数据查询(HQL)helloworld

Maven搭建SSH(Struts2+Spring+Hibernate)框架入门教程_2