在普通的sql中,为了选取某些列,其实是很简单的,就select 列名就可以了,但在hibernate中,一般 都是sql="from user ....";但这样其实某些时候,效率还是很低的,在hibernate做的时候,要这样了,如下:
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);
}
下面再介绍下相关的知识:
1 查询其中几个字段
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);
System.out.println(name + " : " + passwd);
}
要注意的是,假如这时有些列是为空的话,则String hql = " select new list(name,passwd) from Users"; 是会报错的,要用下面的方法:
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");
System.out.println(name + " : " + passwd);
}
4 修改默认查询结果(query.list())不以Object[]数组形式返回,以自定义类型返回
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,个人觉得这个方式十分麻烦,呵呵
来源:oschina
链接:https://my.oschina.net/u/998146/blog/139133