1 package com.test;
2
3 import java.util.List;
4
5 import org.hibernate.Session;
6 import org.junit.Test;
7
8 import com.beans.Student;
9 import com.utils.HbnUtils;
10
11 public class QueryTest {
12
13 /**
14 * 添加测试数据
15 */
16 @Test
17 public void test_01() {
18 Session session = HbnUtils.getSession();
19 try {
20 session.beginTransaction();
21 Student student1 = new Student("貂蝉", 20, 92);
22 Student student2 = new Student("王昭君", 22, 93);
23 Student student3 = new Student("杨玉环", 23, 94);
24 Student student4 = new Student("李师师", 24, 95);
25 Student student5 = new Student("苏小小", 25, 96);
26 Student student6 = new Student("李师师", 28, 96);
27 Student student7 = new Student("张云云", 20, 91);
28 session.save(student1);
29 session.save(student2);
30 session.save(student3);
31 session.save(student4);
32 session.save(student5);
33 session.save(student6);
34 session.save(student7);
35 session.getTransaction().commit();
36 } catch (Exception e) {
37 // TODO: handle exception
38 session.getTransaction().rollback();
39 e.printStackTrace();
40 }
41 }
42
43 /**
44 * 使用SQL语句查询学生信息(作比较)
45 */
46 @Test
47 public void testQuery_SQL() {
48 Session session = HbnUtils.getSession();
49 try {
50 session.beginTransaction();
51 String sql = "SELECT tid,tname,tage,tscore FROM t_student ";
52 List<Student> list = session.createSQLQuery(sql)
53 .addEntity(Student.class).list();
54 for (Student student : list) {
55 System.out.println(student);
56 }
57 session.getTransaction().commit();
58 } catch (Exception e) {
59 // TODO: handle exception
60 e.printStackTrace();
61 session.getTransaction().rollback();
62 }
63 }
64
65 /**
66 * 使用HQL查询学生信息
67 */
68 @Test
69 public void testQuery_HQL01() {
70 Session session = HbnUtils.getSession();
71 try {
72 session.beginTransaction();
73 String hql = "FROM Student ORDER BY tscore DESC";
74 List<Student> list = session.createQuery(hql).list();
75 for (Student student : list) {
76 System.out.println(student);
77 }
78 session.getTransaction().commit();
79 } catch (Exception e) {
80 // TODO: handle exception
81 e.printStackTrace();
82 session.getTransaction().rollback();
83 }
84 }
85
86 /**
87 * 查询指定字段的信息
88 */
89 @Test
90 public void testQuery_HQL_02() {
91 Session session = HbnUtils.getSession();
92 try {
93 session.beginTransaction();
94 String hql = "FROM Student WHERE id=:id";// :后的id:属性名,Studetn:类名
95 Student student = (Student) session.createQuery(hql)
96 .setInteger("id", 3).uniqueResult();
97 System.out.println(student);
98 session.getTransaction().commit();
99 } catch (Exception e) {
100 // TODO: handle exception
101 e.printStackTrace();
102 session.getTransaction().rollback();
103 }
104 }
105
106 /**
107 * 简单聚合查询
108 */
109 @Test
110 public void testQuery_HQL_03() {
111 Session session = HbnUtils.getSession();
112 try {
113 session.beginTransaction();
114 String hql = "SELECT COUNT(*) from Student";
115 Long count = (Long) session.createQuery(hql).uniqueResult();
116 System.out.println(count);// 打印有多少条数据
117 session.getTransaction().commit();
118 } catch (Exception e) {
119 // TODO: handle exception
120 e.printStackTrace();
121 session.getTransaction().rollback();
122 }
123 }
124
125 /**
126 * 投影查询
127 */
128 @Test
129 public void testQuery_HQL04() {
130 Session session = HbnUtils.getSession();
131 try {
132 session.beginTransaction();
133 String hql = "SELECT new Student(name,age) From Student";// 将查询到的字段通过构造方法封装到对象,然后保存在list中
134 List<Student> list = session.createQuery(hql).list();
135 for (Student student : list) {
136 System.out.println(student);//只查询到name、age信息,其他则为默认
137 }
138 session.getTransaction().commit();
139 } catch (Exception e) {
140 // TODO: handle exception
141 e.printStackTrace();
142 session.getTransaction().rollback();
143 }
144 }
145
146 /**
147 * 查每个年龄段的第一个人
148 */
149 @Test
150 public void testQuery_HQL05() {
151 Session session = HbnUtils.getSession();
152 try {
153 session.beginTransaction();
154 String hql = "FROM Student GROUP BY age";
155 List<Student> list = session.createQuery(hql).list();
156 for (Student student : list) {
157 System.out.println(student);
158 }
159 session.getTransaction().commit();
160 } catch (Exception e) {
161 // TODO: handle exception
162 e.printStackTrace();
163 session.getTransaction().rollback();
164 }
165 }
166
167 /**
168 * 查询有几个年龄段
169 */
170 @Test
171 public void testQuery_HQL06() {
172 Session session = HbnUtils.getSession();
173 try {
174 session.beginTransaction();
175 String hql= "select age from Student group by age";
176 List<Integer> list = session.createQuery(hql).list();
177 System.out.println(list);
178 session.getTransaction().commit();
179 } catch (Exception e) {
180 // TODO: handle exception
181 e.printStackTrace();
182 session.getTransaction().rollback();
183 }
184 }
185
186 /**
187 * 查询年龄大于1人的年龄段
188 */
189 @Test
190 public void testQuery_HQL07() {
191 Session session = HbnUtils.getSession();
192 try {
193 session.beginTransaction();
194 String hql= "select age from Student group by age having count(age) > 1";
195 List<Integer> list = session.createQuery(hql).list();
196 System.out.println(list);
197 session.getTransaction().commit();
198 } catch (Exception e) {
199 // TODO: handle exception
200 e.printStackTrace();
201 session.getTransaction().rollback();
202 }
203 }
204 }