原文出处: http://kuangbaoxu.iteye.com/blog/193076
1. 查询整个映射对象所有字段
-
- String hql = "from Users";
- Query query = session.createQuery(hql);
-
- List users = query.list();
- for(Users user : users){
- System.out.println(user.getName() + " : " + user.getPasswd() + " : " + user.getId());
- }
输出结果为:
name1 : password1 : 1
name2 : password2 : 2
name3 : password3 : 3
2.查询单个字段
-
- String hql = " select name from Users";
- Query query = session.createQuery(hql);
-
- List list = query.list();
- for(String str : list){
- System.out.println(str);
- }
输出结果为:
name1
name2
name3
-
- String hql = " select name from Users";
- Query query = session.createQuery(hql);
-
- List list = query.list();
- for(String str : list){
- System.out.println(str);
- }
-
- 输出结果为:
- name1
- name2
- name3
3.查询其中几个字段
-
- String hql = " select name,passwd from Users";
- Query query = session.createQuery(hql);
-
- List list = query.list();
- for(Object[] object : list){
- String name = (String)object[0];
- String passwd = (String)object[1];
-
- System.out.println(name + " : " + passwd);
- }
输出结果为:
name1 : password1
name2 : password2
name3 : password3
4.修改默认查询结果(query.list())不以Object[]数组形式返回,以List形式返回
- 引用
-
- String hql = " select new list(name,passwd) from Users";
- Query query = session.createQuery(hql);
-
- 的Object数组了,而是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);
- }
/**
输出结果为:
name1 : password1
name2 : password2
name3 : password3
*/
5.修改默认查询结果(query.list())不以Object[]数组形式返回,以Map形式返回
Java代码
-
- String hql = " select new map(name,passwd) from Users";
- Query query = session.createQuery(hql);
-
- List list = query.list();
- for(Map user : list){
-
-
- String name = (String)user.get("0");
- String passwd = (String)user.get("1");
-
- System.out.println(name + " : " + passwd);
- }
-
-
-
-
-
-
-
-
- String hql = " select new map(name,passwd) from Users";
- Query query = session.createQuery(hql);
-
- List list = query.list();
- for(Map user : list){
-
-
- String name = (String)user.get("0");
- String passwd = (String)user.get("1");
-
- System.out.println(name + " : " + passwd);
- }
/**
输出结果为:
name1 : password1
name2 : password2
name3 : password3
*/
6.修改默认查询结果(query.list())不以Object[]数组形式返回,以Set形式返回,但是因为Set里是不允许有重复的元素,所以:username和password的值不能相同。只需将hql改为:String hql = " select new set(name,passwd) from Users";
7.修改默认查询结果(query.list())不以Object[]数组形式返回,以自定义类型返回
自定义类:
- package com.domain;
-
- public class MyUser {
-
- private String username;
- private String password;
-
- 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;
- }
-
-
- }
-
- package com.domain;
-
- public class MyUser {
-
- private String username;
- private String password;
-
- 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;
- }
-
-
- }
-
- String hql = " select new com.domain.MyUser(name,passwd) from Users";
- Query query = session.createQuery(hql);
-
- List myUsers = query.list();
- for(MyUser myUser : myUsers){
- String name = myUser.getUsername();
- String passwd = myUser.getPassword();
- System.out.println(name + " : " + passwd);
- }
/**
输出结果为:
name1 : password1
name2 : password2
name3 : password3
*/
//通过query.list()出来的list里存放的不再是默认的Object数组了,而是自定义的类MyUser,必须加包名,String hql = "from Users";中的Users类也是必须加包名的,但是因为再Users.hbm.xml里 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 myUsers = query.list();
for(MyUser myUser : myUsers){
String name = myUser.getUsername();
String passwd = myUser.getPassword();
System.out.println(name + " : " + passwd);
}
/**
输出结果为:
name1 : password1
name2 : password2
name3 : password3
*/
8:条件查询
-
- String hql = "from Users where name=? and passwd=?";
- Query query = session.createQuery(hql);
-
-
-
-
- query.setParameter(0, "name1",Hibernate.STRING);
- query.setParameter(1, "password1",Hibernate.STRING);
- List list = query.list();
- for(Users users : list){
- System.out.println(users.getId());
- }
-
-
- String hql = "from Users where name=? and passwd=?";
- Query query = session.createQuery(hql);
-
-
-
-
- query.setParameter(0, "name1",Hibernate.STRING);
- query.setParameter(1, "password1",Hibernate.STRING);
- List list = query.list();
- for(Users users : list){
- System.out.println(users.getId());
- }
-
- String hql = "from Users where name=:username and passwd=:password";
- Query query = session.createQuery(hql);
-
-
-
-
- query.setParameter("username", "name1",Hibernate.STRING);
- query.setParameter("password", "password1",Hibernate.STRING);
- List list = query.list();
- for(Users users : list){
- System.out.println(users.getId());
- }
-
-
- String hql = "from Users where name=:username and passwd=:password";
- Query query = session.createQuery(hql);
-
-
-
-
- query.setParameter("username", "name1",Hibernate.STRING);
- query.setParameter("password", "password1",Hibernate.STRING);
- List list = query.list();
- for(Users users : list){
- System.out.println(users.getId());
- }
-
- String hql = "from Users where name=:username and passwd=:password";
- Query query = session.createQuery(hql);
-
- MyUser myUser = new MyUser("name1","password1");
- query.setProperties(myUser);
- List list = query.list();
- for(Users users : list){
- System.out.println(users.getId());
- }