hibernate需要配置的xml(二)

2014-11-24 09:01:35 · 作者: · 浏览: 1


146
tr.commit();
147
s.close();
148
}
149

150
/**通过id进行修改*/
151
http://my.oschina.net/test45" target="_blank" rel="nofollow">@Test
152
public void testUpdateCustomer(){
153
Session s = sf.openSession();
154
Transaction tr = s.beginTransaction();
155

156
Customer c = new Customer();
157
c.setId(3);
158
c.setName("小四");
159
s.update(c);
160

161
tr.commit();
162
s.close();
163
}
164

165
/**通过id删除客户信息*/
166
http://my.oschina.net/test45" target="_blank" rel="nofollow">@Test
167
public void testDeleteCustomer(){
168
Session s = sf.openSession();
169
Transaction tr = s.beginTransaction();
170

171
Customer c = new Customer();
172
c.setId(2);
173
s.delete(c);
174

175
tr.commit();
176
s.close();
177
}
178

179
/**通过id查询客户信息*/
180
http://my.oschina.net/test45" target="_blank" rel="nofollow">@Test
181
public void testQueryCustomer(){
182
Session s = sf.openSession();
183
Transaction tr = s.beginTransaction();
184

185
Customer c = (Customer) s.get(Customer.class, 1);
186
// Customer c = (Customer) s.load(Customer.class, 1);
187
System.out.println(c.getName()+" "+c.getAge()+" "+c.getDes());
188

189
tr.commit();
190
s.close();
191
}
192
/**查询所有的客户信息*/
193
http://my.oschina.net/test45" target="_blank" rel="nofollow">@Test
194
public void testQueryAllCustomer(){
195
Session s = sf.openSession();
196
Transaction tr = s.beginTransaction();
197

198
/**
199
* s.createQuery("HQL语句"):使用HQL语句查询 数据库,返回Query对象
200
* SQL语句:针对数据库、数据库表、数据库字段
201
* HQL语句:针对持久化对象、持久化对象的属性
202
*/
203
Query query = s.createQuery("from Customer");
204
List list = query.list();
205
for(Customer c:list){
206
System.out.println(c.getName()+" "+c.getAge()+" "+c.getDes());
207
}
208

209
tr.commit();
210
s.close();
211
}
212
}


213


214


215


作者:Aypak