返回单个对象
public static void test1(){
Session session = SessionFactoryUtils.openSession();
Transaction transaction = session.beginTransaction();
String hql="from Student where sid=1";
Query query = session.createQuery(hql);
Object obj = query.getSingleResult();
System.out.println(obj.getClass().getName());
System.out.println(obj);
transaction.commit();
SessionFactoryUtils.closeSession();
}
返回多个对象
/**
* hql语句结果集处理情况1
* 返回多个对象 注意数据库字段不能为null
*/
public static void test2(){
Session session = SessionFactoryUtils.openSession();
Transaction transaction = session.beginTransaction();
String hql="from Student";
Query query = session.createQuery(hql);
List list=query.list();
for (Object o : list) {
System.out.println(o);
}
transaction.commit();
SessionFactoryUtils.closeSession();
}
返回字符串
public static void test3(){
Session session = SessionFactoryUtils.openSession();
Transaction transaction = session.beginTransaction();
String hql="select sname from Student where sid=1";
Query query = session.createQuery(hql);
Object obj=query.getSingleResult();
System.out.println(obj);
transaction.commit();
SessionFactoryUtils.closeSession();
}
返回数组
public static void test4(){
Session session = SessionFactoryUtils.openSession();
Transaction transaction = session.beginTransaction();
String hql="select sid,sname,version from Student where sid=1";
Query query = session.createQuery(hql);
Object obj=query.getSingleResult();
System.out.println(Arrays.toString((Object[])obj));
transaction.commit();
SessionFactoryUtils.closeSession();
}
返回Map
public static void test5(){
Session session = SessionFactoryUtils.openSession();
Transaction transaction = session.beginTransaction();
String hql="select new map(sid as sid,sname as sname,version as version) from Student where sid=1";
Query query = session.createQuery(hql);
Object obj=query.getSingleResult();
System.out.println(obj);
transaction.commit();
SessionFactoryUtils.closeSession();
}
查询多列返回对象(对象需要有有参和无参的构造函数)
public static void test6(){
Session session = SessionFactoryUtils.openSession();
Transaction transaction = session.beginTransaction();
String hql="select new Student (sid,sname)from Student where sid=1";
Query query = session.createQuery(hql);
Object obj=query.getSingleResult();
System.out.println(obj.getClass().getName());
System.out.println(obj);
transaction.commit();
SessionFactoryUtils.closeSession();
}
:命名参数
public static void test7(){
Session session = SessionFactoryUtils.openSession();
Transaction transaction = session.beginTransaction();
String hql="from Student where sid in :ids";
Query query = session.createQuery(hql);
//这里使用 query.setParameter(0,1)报错
// query.setParameter("id",1);//这个是传单个参数
// 如果使用in的话
List ids=new ArrayList();
ids.add(1);
ids.add(2);
query.setParameterList("ids",ids);
Object obj = query.getSingleResult();
System.out.println(obj.getClass().getName());
System.out.println(obj);
transaction.commit();
SessionFactoryUtils.closeSession();
}
连表查询
public static void test8(){
Session session = SessionFactoryUtils.openSession();
Transaction transaction = session.beginTransaction();
String hql="select new map(oi.order_item_id,o.order_id) from Order o,OrderItem oi where o.order_id=oi.oid ";
Query query = session.createQuery(hql);
List list=query.list();
for (Object o : list) {
System.out.println(o);
}
transaction.commit();
SessionFactoryUtils.closeSession();
}
聚合函数 其余的sum,avg,min,max用法都一样
public static void test9(){
Session session = SessionFactoryUtils.openSession();
Transaction transaction = session.beginTransaction();
String hql="select count(sid)from Student ";
Query query = session.createQuery(hql);
Object singleResult = query.getSingleResult();
System.out.println(singleResult);
transaction.commit();
SessionFactoryUtils.closeSession();
}
hql分页
public static void test10() {
Session session = SessionFactoryUtils.openSession();
Transaction transaction = session.beginTransaction();
String hql = "from Book ";
Query query = session.createQuery(hql);
int page = 2;
int offeset = 3;
query.setFirstResult(page * offeset);
query.setMaxResults((page - 1) * offeset);
List list = query.list();
for (Object o : list) {
System.out.println(o);
}
transaction.commit();
SessionFactoryUtils.closeSession();
}
标签:Hibernate,obj,示例,transaction,session,hql,query,SessionFactoryUtils From: https://blog.51cto.com/u_15897407/5899816