HQL总结
以个普通Users表为例:
建表语句:
CREATE DATABASE `interceptor`
CHARACTER SET 'utf8';
USE `interceptor`;
DROP TABLE IF EXISTS `users`;
create table users(
id int auto_increment primary key,
name varchar(10) not null,
passwd varchar(10) not null
)
ENGINE=InnoDB DEFAULT CHARSET=utf8;
User.java:
package com.domain;
public class Users implements java.io.Serializable {
private Integer id;
private String name;
private String passwd;
public Users() {
}
public Users(String name, String passwd) {
this.name = name;
this.passwd = passwd;
}
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getPasswd() {
return this.passwd;
}
public void setPasswd(String passwd) {
this.passwd = passwd;
}
User.hbm.xml:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.domain.Users" table="users" catalog="interceptor">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="native" />
</id>
<property name="name" type="java.lang.String">
<column name="name" length="10" not-null="true" />
</property>
<property name="passwd" type="java.lang.String">
<column name="passwd" length="10" not-null="true" />
</property>
</class>
</hibernate-mapping>
1. 查询整个映射对象所有字段
//直接from查询出来的是一个映射对象,即:查询整个映射对象所有字段
String hql = "from Users";
Query query = session.createQuery(hql);
List<Users> 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<String> 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里存放的是一个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);
}
输出结果为:
name1 : password1
name2 : password2
name3 : password3
4.修改默认查询结果(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);
}
/**
输出结果为:
name1 : password1
name2 : password2
name3 : password3
*/
5.修改默认查询结果(query.list())不以Object[]数组形式返回,以Map形式返回
//查询其中几个字段,添加new map(),注意map里的m是小写的。也不需要导入包,这样通过query.list()出来的list里存放的不再是默认的Object数组了,而是map集合了
String hql = "