hibernate中只選取某些列的寫法

在普通的sql中,爲了選取某些列,實際上是很簡單的,就select 列名就能夠了,但在hibernate中,通常 都是sql="from user ....";但這樣其實某些時候,效率仍是很低的,在hibernate作的時候,要這樣了,以下:sql

String hql="select new map(t.title as title,t.id as id,t.link as link,t.linktitle as linktitle) from Article as t where t.category="+categoryid+" order by t.uploadtime desc";數組

Query query= getSessionFactory().getCurrentSession().createQuery(hql); query.setFirstResult((pageNo - 1) * pageSize); query.setMaxResults(pageSize); List<Map> list = query.list();
for(Map article : list) { String title = (String)article.get("title");
System.out.println("文章標題是"+title); }session

下面再介紹下相關的知識:app

1 查詢其中幾個字段dom

String hql = " select name,passwd from Users";      
    Query query = session.createQuery(hql);      
    //默認查詢出來的list裏存放的是一個Object數組      
    List<Object[]> list = query.list();      
    for(Object[] object : list){      
        String name = (String)object[0];      
        String passwd = (String)object[1];      
              
        System.out.println(name + " : " + passwd);      
    }

2 修改默認查詢結果(query.list())不以Object[]數組形式返回,以List形式返回函數

//查詢其中幾個字段,添加new list(),注意list裏的l是小寫的。也不須要導入包,這樣經過query.list()出來的list裏存放的再也不是默認的Object數組了,而是List集合了
String hql = " select new list(name,passwd) from Users";
Query query = session.createQuery(hql);
//默認查詢出來的list裏存放的是一個Object數組,可是在這裏list裏存放的再也不是默認的Object數組了,而是List集合了
List<List> list = query.list();
for(List user : list){
String name = (String)user.get(0);
String passwd = (String)user.get(1);this

System.out.println(name + " : " + passwd);   
    }

要注意的是,假如這時有些列是爲空的話,則String hql = " select new list(name,passwd) from Users"; 是會報錯的,要用下面的方法:hibernate

3 //查詢其中幾個字段,添加new map(),注意map裏的m是小寫的。也不須要導入包,這樣經過query.list()出來的list裏存放的再也不是默認的Object數組了,而是map集合了
String hql = " select new map(name,passwd) from Users";
Query query = session.createQuery(hql);
//默認查詢出來的list裏存放的是一個Object數組,可是在這裏list裏存放的再也不是默認的Object數組了,而是Map集合了
List<Map> list = query.list();
for(Map user : list){
//一條記錄裏全部的字段值都是map裏的一個元素,key是字符串0,1,2,3.,value是字段值
//若是將hql改成:String hql = " select new map(name as username,passwd as password) from Users";,那麼key將不是字符串0,1,2了,而是"username","password"了
String name = (String)user.get("0");//get("0");是get(key),注意:0,1,2是字符串,而不是整形
String passwd = (String)user.get("1");code

System.out.println(name + " : " + passwd);      
    }

4 修改默認查詢結果(query.list())不以Object[]數組形式返回,以自定義類型返回orm

package com.domain;

public class MyUser {

private String username;   
private String password;

//由於:String hql = " select new com.domain.MyUser(name,passwd) from Users";因此必需要有接受2個參數的構造函數
public MyUser(String username,String password){
this.username = username;
this.password = password;
}

public String getUsername() {   
    return username;   
}   
public void setUsername(String username) {   
    this.username = username;   
}   
public String getPassword() {   
    return password;   
}   
public void setPassword(String password) {   
    this.password = password;   
}

}
//經過query.list()出來的list裏存放的再也不是默認的Object數組了,而是自定義的類MyUser,必須加包名,String hql = "from Users";中的Users類也是必須加包名的,可是由於再Users.hbm.xml裏<hibernate-mapping auto-import="true"> auto-import默認值爲true(因此auto-import屬性也能夠不寫),自動導入了
String hql = " select new com.domain.MyUser(name,passwd) from Users";
Query query = session.createQuery(hql);
//默認查詢出來的list裏存放的是一個Object數組,可是在這裏list裏存放的再也不是默認的Object數組了,而是MyUser對象了
List<MyUser> myUsers = query.list();
for(MyUser myUser : myUsers){
String name = myUser.getUsername();
String passwd = myUser.getPassword();
System.out.println(name + " : " + passwd);
}
5 條件查詢

//條件查詢,參數索引值從0開始,索引位置。經過setString,setParameter設置參數
String hql = "from Users where name=? and passwd=?";
Query query = session.createQuery(hql);
//第1種方式
// query.setString(0, "name1");
// query.setString(1, "password1");
//第2種方式
query.setParameter(0, "name1",Hibernate.STRING);
query.setParameter(1, "password1",Hibernate.STRING);
List<Users> list = query.list();
for(Users users : list){
System.out.println(users.getId());
}
//條件查詢,自定義索引名(參數名):username,:password.經過setString,setParameter設置參數
String hql = "from Users where name=:username and passwd=:password";
Query query = session.createQuery(hql);
//第1種方式
// query.setString("username", "name1");
// query.setString("password", "password1");
//第2種方式,第3個參數肯定類型
query.setParameter("username", "name1",Hibernate.STRING);
query.setParameter("password", "password1",Hibernate.STRING);
List<Users> list = query.list();
for(Users users : list){
System.out.println(users.getId());
}

//條件查詢,經過setProperties設置參數
String hql = "from Users where name=:username and passwd=:password";
Query query = session.createQuery(hql);
//MyUser類的2個屬性必須和:username和:password對應
MyUser myUser = new MyUser("name1","password1");
query.setProperties(myUser);
List<Users> list = query.list();
for(Users users : list){
System.out.println(users.getId());
}

假如以爲仍是想用回原生SQL的話,能夠這樣:

Query query = getSessionFactory().getCurrentSession().createSQLQuery(hql).addScalar("title",Hibernate.STRING).addScalar("id",Hibernate.INTEGER).setResultTransformer(Transformers.aliasToBean(Article.class));

就是要注意的話,這裏的createSQLQuery裏面跟的是原生的sql,要讀什麼屬性,就要不斷加addScalar,我的以爲這個方式十分麻煩,呵呵

相關文章
相關標籤/搜索