关键字: hibernate------hql总结
以以个普通Users表为例:
建表语句:
1. CREATE DATABASE `interceptor`
2. CHARACTER SET 'utf8';
3.
4. USE `interceptor`;
5.
6. DROP TABLE IF EXISTS `users`;
7. create table users(
8. id int auto_increment primary key,
9. name varchar(10) not null,
10. passwd varchar(10) not null
11. )ENGINE=InnoDB DEFAULT CHARSET=utf8;
User.java:
1. package com.domain;
2.
3. public class Users implements java.io.Serializable {
4.
5. private Integer id;
6.
7. private String name;
8.
9. private String passwd;
10.
11. public Users() {
12. }
13.
14. public Users(String name, String passwd) {
15. this.name = name;
16. this.passwd = passwd;
17. }
18.
19. public Integer getId() {
20. return this.id;
21. }
22.
23. public void setId(Integer id) {
24. this.id = id;
25. }
26.
27. public String getName() {
28. return this.name;
29. }
30.
31. public void setName(String name) {
32. this.name = name;
33. }
34.
35. public String getPasswd() {
36. return this.passwd;
37. }
38.
39. public void setPasswd(String passwd) {
40. this.passwd = passwd;
41. }
42.
43. }
User.hbm.xml:
1.
2. <?xml version="1.0" encoding="utf-8"?>
3. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
4. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
5. <hibernate-mapping>
6. <class name="com.domain.Users" table="users" catalog="interceptor">
7. <id name="id" type="java.lang.Integer">
8. <column name="id" />
9. <generator class="native" />
10. </id>
11. <property name="name" type="java.lang.String">
12. <column name="name" length="10" not-null="true" />
13. </property>
14. <property name="passwd" type="java.lang.String">
15. <column name="passwd" length="10" not-null="true" />
16. </property>
17. </class>
18. </hibernate-mapping>
1. 查询整个映射对象所有字段
1. //直接from查询出来的是一个映射对象,即:查询整个映射对象所有字段
2. String hql = "from Users";
3. Query query = session.createQuery(hql);
4.
5. List<Users> users = query.list();
6. for(Users user : users){
7. System.out.println(user.getName() + " : " + user.getPasswd() + " : " + user.getId());
8. }
9.
10. 输出结果为:
11. name1 : password1 : 1
12. name2 : password2 : 2
13. name3 : password3 : 3
2.查询单个字段
1.
2. //查询单个字段
3. String hql = " select name from Users";
4. Query query = session.createQuery(hql);
5.
6. List<String> list = query.list();
7. for(String str : list){
8. System.out.println(str);
9. }
10. 输出结果为:
11. name1
12. name2
13. name3
3.查询其中几个字段
1.
2. //查询其中几个字段
3. String hql = " select name,passwd from Users";
4. Query query = session.createQuery(hql);
5. //默认查询出来的list里存放的是一个Object数组
6. List<Object[]> list = query.list();
7. for(Object[] object : list){
8. String name = (String)object[0];
9. String passwd = (String)object[1];
10.
11. System.out.println(name + " : " + passwd);
12. }
13. 输出结果为:
14. name1 : password1
15. name2 : password2
16. name3 : password3
4.修改默认查询结果(query.list())不以Object[]数组形式返回,以List形式返回
1. //查询其中几个字段,添加new list(),注意list里的l是小写的。也不需要导入包,这样通过query.list()出来的list里存放的不再是默认的Object数组了,而是List集合了
2. String hql = " select new list(name,passwd) from Users";
3. Query query = session.createQuery(hql);
4. //默认查询出来的list里存放的是一个Object数组,但是在这里list里存放的不再是默认的Object数组了,而是List集合了
5. List<List> list = query.list();
6. for(List user : list){
7. String name = (String)user.get(0);
8. String passwd = (String)user.get(1);
9.
10. System.out.println(name + " : " + passwd);
11. }
12. /**
13. 输出结果为:
14. name1 : password1
15. name2 : password2
16. name3 : password3
17. */
5.修改默认查询结果(query.list())不以Object[]数组形式返回,以Map形式返回
1.
2. //查询其中几个字段,添加new map(),注意map里的m是小写的。也不需要导入包,这样通过query.list()出来的list里存放的不再是默认的Object数组了,而是map集合了
3. String hql = " select new map(name,passwd) from Users";
4. Query query = session.createQuery(hql);
5. //默认查询出来的list里存放的是一个Object数组,但是在这里list里存放的不再是默认的Object数组了,而是Map集合了
6. List<Map> list = query.list();
7. for(Map user : list){
8. //一条记录里所有的字段值都是map里的一个元素,key是字符串0,1,2,3....,value是字段值
9. //如果将hql改为:String hql = " select new map(name as username,passwd as password) from Users";,那么key将不是字符串0,1,2...了,而是"username","password"了
10. String name = (String)user.get("0");//get("0");是get(key),注意:0,1,2...是字符串,而不是整形
11. String passwd = (String)user.get("1");
12.
13. System.out.println(name + " : " + passwd);
14. }
15. /**
16. 输出结果为:
17. name1 : password1
18. name2 : password2
19. name3 : password3
20. */
6.修改默认查询结果(query.list())不以Object[]数组形式返回,以Set形式返回,但是因为Set里是不允许有重复的元素,所以:username和password的值不能相同。只需将hql改为:String hql = " select new set(name,passwd) from Users";
7.修改默认查询结果(query.list())不以Object[]数组形式返回,以自定义类型返回
自定义类:
1.
2. package com.domain;
3.
4. public class MyUser {
5.
6. private String username;
7. private String password;
8. //因为:String hql = " select new com.domain.MyUser(name,passwd) from Users";所以必须要有接受2个参数的构造函数
9. public MyUser(String username,String password){
10. this.username = username;
11. this.password = password;
12. }
13.
14. public String getUsername() {
15. return username;
16. }
17. public void setUsername(String username) {
18. this.username = username;
19. }
20. public String getPassword() {
21. return password;
22. }
23. public void setPassword(String password) {
24. this.password = password;
25. }
26.
27.
28. }
1.
2. //通过query.list()出来的list里存放的不再是默认的Object数组了,而是自定义的类MyUser,必须加包名,String hql = "from Users";中的Users类也是必须加包名的,但是因为再Users.hbm.xml里<hibernate-mapping auto-import="true"> auto-import默认值为true(所以auto-import属性也可以不写),自动导入了
3. String hql = " select new com.domain.MyUser(name,passwd) from Users";
4. Query query = session.createQuery(hql);
5. //默认查询出来的list里存放的是一个Object数组,但是在这里list里存放的不再是默认的Object数组了,而是MyUser对象了
6. List<MyUser> myUsers = query.list();
7. for(MyUser myUser : myUsers){
8. String name = myUser.getUsername();
9. String passwd = myUser.getPassword();
10. System.out.println(name + " : " + passwd);
11. }
12. /**
13. 输出结果为:
14. name1 : password1
15. name2 : password2
16. name3 : password3
17. */
8:条件查询
1. //条件查询,参数索引值从0开始,索引位置。通过setString,setParameter设置参数
2. String hql = "from Users where name=? and passwd=?";
3. Query query = session.createQuery(hql);
4. //第1种方式
5. // query.setString(0, "name1");
6. // query.setString(1, "password1");
7. //第2种方式
8. query.setParameter(0, "name1",Hibernate.STRING);
9. query.setParameter(1, "password1",Hibernate.STRING);
10. List<Users> list = query.list();
11. for(Users users : list){
12. System.out.println(users.getId());
13. }
14.
1. //条件查询,自定义索引名(参数名):username,:password.通过setString,setParameter设置参数
2. String hql = "from Users where name=:username and passwd=:password";
3. Query query = session.createQuery(hql);
4. //第1种方式
5. // query.setString("username", "name1");
6. // query.setString("password", "password1");
7. //第2种方式,第3个参数确定类型
8. query.setParameter("username", "name1",Hibernate.STRING);
9. query.setParameter("password", "password1",Hibernate.STRING);
10. List<Users> list = query.list();
11. for(Users users : list){
12. System.out.println(users.getId());
13. }
1. //条件查询,通过setProperties设置参数
2. String hql = "from Users where name=:username and passwd=:password";
3. Query query = session.createQuery(hql);
4. //MyUser类的2个属性必须和:username和:password对应
5. MyUser myUser = new MyUser("name1","password1");
6. query.setProperties(myUser);
7. List<Users> list = query.list();
8. for(Users users : list){
9. System.out.println(users.getId());
10. }