本文介绍了Java类 - 如何将通用对象传递给函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



  public static void main(String [] a)throws Exception {

Session sessione = HibernateUtil.getSessionFactory()。openSession();
Query query = sessione.createSQLQuery(select * from User)。addEntity(User.class);
列表<用户> rows = query.list();
Iterator it = rows.iterator(); (it.hasNext()){
User usr =(User)it.next();
System.out.println(usr.getEmail());
System.out.println(usr.getName());
System.out.println(usr.getIdUser());
System.out.println(usr.getUser());




$ b $ p
$ b

这个函数能够连接并执行一个查询DB ...



我想创建相同的函数,但更一般...以前只针对一个表( User ),新的必须能够接受查询的 String 参数作为输入,以及将执行查询的类类型。这将允许我只使用一行来执行查询。



它应该是这样的:

  public static void queryResult(String query,< ClassOfTable>){

Session sessione = HibernateUtil.getSessionFactory()。openSession();
Query qy = sessione.createSQLQuery(query).addEntity(< ClassOfTable> .class);
列表<< ClassOfTable>> rows = qy.list();
Iterator it = rows.iterator();
while(it.hasNext()){
< ClassOfTable> obj =(< ClassOfTable>)it.next();
}

}

在哪里找到 ClassOfTable 我不知道如何推广代码...



我希望已经清楚...



PS ClassOfTable 应该是DB中一个表的类( Hibernate )。


$

解决方案
  public static< T> void queryResult(String query,Class< ;? extends T> clazz){
Session session = HibernateUtil.getSessionFactory()。openSession();
Query q = session.createSQLQuery(query).addEntity(clazz);
列出行= q.list();
Iterator it = rows.iterator(); (it.hasNext()){
T t =(T)it.next();
//做你的工作对象t
}
}

如果您的意图是返回结果集,请使用:

  public static< T>列表与LT; T> queryResult(String query,Class< ;? extends T> clazz){
Session session = HibernateUtil.getSessionFactory()。openSession();
Query q = session.createSQLQuery(query).addEntity(clazz);
列表< T> rows =(List< T>)q.list();
返回Collections.unmodifiableList(rows);
}

//现在调用泛型方法
List< User> users = queryResult(select * from User,User.class);
users.forEach(usr - > {
System.out.println(usr.getEmail());
System.out.println(usr.getName());
System.out.println(usr.getIdUser());
System.out.println(usr.getUser());
});


This is my basic function:

public static void main(String[] a) throws Exception {

    Session sessione = HibernateUtil.getSessionFactory().openSession();
    Query query = sessione.createSQLQuery("select * from User").addEntity(User.class);
    List<User> rows = query.list();
    Iterator it = rows.iterator();
    while (it.hasNext()) {
        User usr = (User) it.next();
        System.out.println(usr.getEmail());
        System.out.println(usr.getName());
        System.out.println(usr.getIdUser());
        System.out.println(usr.getUser());

    }

This function is capable to connect and perform a query on my DB...

I want to create the same function but more general... The previous was specific for only one table (User), the new one must be able to accept as input a String parameter for the query, and the class type where the query will be executed. This will allow me to use only one row in order to perform a query.

It should be something like this:

public static void queryResult(String query, <ClassOfTable>) {

    Session sessione = HibernateUtil.getSessionFactory().openSession();
    Query qy = sessione.createSQLQuery(query).addEntity(<ClassOfTable>.class);
    List<<ClassOfTable>> rows = qy.list();
    Iterator it = rows.iterator();
    while (it.hasNext()) {
        <ClassOfTable> obj = (<ClassOfTable>) it.next();
    }

}

Where you find ClassOfTable I don't know how to "generalize" the code...

I hope to have been clear...

P.S. ClassOfTable should be the class rappresentative of a table in DB (Hibernate).

Thanks.

解决方案
public static <T> void queryResult(String query, Class<? extends T> clazz) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    Query q = session.createSQLQuery(query).addEntity(clazz);
    List rows = q.list();
    Iterator it = rows.iterator();
    while (it.hasNext()) {
        T t = (T) it.next();
        // do your work on object t
    }
}

If your intention is to return resultset, use:

public static <T> List<T> queryResult(String query, Class<? extends T> clazz) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    Query q = session.createSQLQuery(query).addEntity(clazz);
    List<T> rows = (List<T>) q.list();
    return Collections.unmodifiableList(rows);
}

// now call generic method
List<User> users = queryResult("select * from User", User.class);
users.forEach(usr -> {
    System.out.println(usr.getEmail());
    System.out.println(usr.getName());
    System.out.println(usr.getIdUser());
    System.out.println(usr.getUser());
});

这篇关于Java类 - 如何将通用对象传递给函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 01:47