三套查询之HQL查询
hql语句(面向):类 对象 属性
1 package com.rong.entity.hql;
2
3 public class User {
4
5 public User(int id, String name) {
6 super();
7 this.id = id;
8 this.name = name;
9 }
10 public User() {
11 super();
12 }
13 private int id;
14 private String name;
15 public int getId() {
16 return id;
17 }
18 public void setId(int id) {
19 this.id = id;
20 }
21 public String getName() {
22 return name;
23 }
24 public void setName(String name) {
25 this.name = name;
26 }
27 }
1 package com.rong.entity.hql;
2
3 import javax.persistence.Entity;
4 import javax.persistence.GeneratedValue;
5 import javax.persistence.GenerationType;
6 import javax.persistence.Id;
7
8 @Entity
9 public class Student {
10 @Id
11 @GeneratedValue(strategy=GenerationType.AUTO)
12 private int id;
13 private String name;
14 private int age;
15 public int getId() {
16 return id;
17 }
18 public void setId(int id) {
19 this.id = id;
20 }
21 public String getName() {
22 return name;
23 }
24 public void setName(String name) {
25 this.name = name;
26 }
27 public int getAge() {
28 return age;
29 }
30 public void setAge(int age) {
31 this.age = age;
32 }
33 }
1 package com.rong.entity.hql;
2
3 import java.util.Iterator;
4 import java.util.List;
5 import java.util.Map;
6 import java.util.Map.Entry;
7 import java.util.Set;
8
9 import org.hibernate.Query;
10 import org.hibernate.Session;
11 import org.hibernate.SessionFactory;
12 import org.hibernate.Transaction;
13 import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
14 import org.hibernate.cfg.Configuration;
15 import org.hibernate.service.ServiceRegistry;
16 import org.junit.After;
17 import org.junit.Before;
18 import org.junit.Test;
19 //hql语句(面向):类 对象 属性
20 public class TestHQL {
21 SessionFactory sessionFactory;
22 Session session;
23 Transaction transaction;
24 @Before
25 public void init() {
26 Configuration configuration = new Configuration().configure();
27 ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
28 .applySettings(configuration.getProperties()).build();
29 sessionFactory=configuration.buildSessionFactory(serviceRegistry);
30 session = sessionFactory.openSession();
31 transaction = session.beginTransaction();
32
33 }
34 //插入数据
35 @Test
36 public void addData(){
37 Student student=null;
38 for(int i=1;i<=100;i++){
39 student=new Student();
40 student.setAge(i);
41 student.setName("先生"+i);
42 session.save(student);
43 }
44 }
45 //查询所有的学生 (所有列)
46 @Test
47 public void test1(){
48 Query query = session.createQuery("from Student");//Student是类名
49 List<Student> list = query.list();
50 System.out.println(list.size());
51 for (Student student : list) {
52 System.out.println(student.getAge()+student.getName());
53 }
54 }
55 //查询所有的学生当中的某一列
56 @Test
57 public void test2(){
58 //Student是类名,name是Student类的成员变量名
59 Query query = session.createQuery("select s.name from Student s");
60 List<Student> list = query.list();
61 System.out.println(list);
62 }
63 //查询所有学生中的多列
64 @Test
65 public void test3(){
66 Query query = session.createQuery("select s.name,s.age from Student s");
67 //Query query = session.createQuery("select name,age from Student s");
68 List<Object[]> list = query.list();
69 //list集合的每一个元素都是数组,而每一个数组都是由name和age构成
70 for (Object[] objects : list) {
71 for (Object object : objects) {
72 System.out.print(object);
73 }
74 System.out.println();
75 }
76 }
77 //分页查询
78 @Test
79 public void test4(){
80 //Query query = session.createQuery("select s from Student s");
81 Query query = session.createQuery("from Student");
82 query.setFirstResult(0);//设置从哪里开始
83 query.setMaxResults(3);//分页的条目
84 List<Student> list = query.list();
85 for (Student student : list) {
86 System.out.println(student.getAge()+student.getName());
87 }
88 }
89 //查询所有的学生的某些字段 返回值是map类型,即获取List<Map<String,Object>>结果
90 @Test
91 public void test5(){
92 //这个是没有别名的
93 //Query query = session.createQuery("select new map(s.name,s.age) from Student s");
94 //这个有别名的(修改key的值 就需要增加别名)
95 Query query = session.createQuery("select new map(s.name as name,s.age as age) from Student s");
96 List<Map<String,Object>> list = query.list();
97 //结果 key是 从0 开始的
98 //System.out.println(list);//[{0=先生1, 1=1}, {0=先生2, 1=2}, {0=先生3, 1=3}......]
99 System.out.println(list);//[{name=先生1, age=1}, {name=先生2, age=2}......]
100 for (Map<String, Object> map : list) {
101 Set<Entry<String, Object>> set = map.entrySet();
102 Iterator<Entry<String, Object>> iterator = set.iterator();
103 while(iterator.hasNext()){
104 Entry<String, Object> entry = iterator.next();
105 System.out.println(entry.getKey()+entry.getValue());
106 }
107 }
108 }
109 //查询所有的学生的某些字段,返回值是list类型,即获取List<List<Object>>结果
110 @Test
111 public void test6(){
112 Query query = session.createQuery("select new list(s.name,s.age) from Student s");
113 List<List<Object>> list = query.list();
114 for (List<Object> li : list) {
115 for (Object object : li) {
116 System.out.print(object);
117 }
118 System.out.println();
119 }
120 }
121 //查询student表,返回值是User,User类必须要有public User(int id, String name)的构造方法!!!
122 @Test
123 public void test7(){
124 Query query = session.createQuery("select new com.rong.entity.hql.User(s.id,s.name) from Student s");
125 List<User> list = query.list();
126 for (User user : list) {
127 System.out.println(user.getId()+user.getName());
128 }
129 }
130
131
132 @After
133 public void destroy(){
134 transaction.commit();
135 session.close();
136 sessionFactory.close();
137 }
138 }
关联(持久化类)与连接(数据库表)