Save
// 新增名為"Emma"的用戶記錄
Tuser user = new Tuser();
user.setName("Emma");
session.save(user);
Get
// 假設T_User表中存在ID = 1的記錄
Tuser user = (Tuser)session.get(Tuser.class, new Integer(1));
Delete
// 假設T_User表中存在ID = 1的記錄
Tuser user = (Tuser)session.get(Tuser.class, new Integer(1));
session.delete(user);
// 也可以通過HQL指定刪除條件
Session.delete(" from Tuser where id = 1");
// 通過Query接口進於基於HQL的刪除操作
Stirng hql = "delete Tuser where id = 1";
Query query = session.createQuery(hql);
query.executeUpdate();
Find
// 通過Query接口進行數據查詢
String hql="from Tuser user where user.name like ?";
Query query = session.createQuery(hql);
query.setParameter(0, "Cartier");
List list = query.list();
Iterator it = list.iterator();
while(it.hasNext()){
Tuser user = (Tuser)it.next();
System.out.println(user.getName());
}
// 通過Criteria接口進行數據查詢
Criteria criteria = session.createCriteria(Tuser.class);
criteria.add(Expression.eq("name","Cariter"));
List list = criteria.list();
Iterator it = list.iterator();
while(it.hasNext()){
Tuser user = (Tuser)it.next();
System.out.println(user.getName());
}