package cn.sasa.test;
import org.hibernate.Session;
import org.hibernate.Transaction;
import cn.sasa.domain.account;
import cn.sasa.utils.HibernateUtils;
public class TestHQL {
@org.junit.Test
public void test() {
Session session = HibernateUtils.getCurrentSession();
Transaction tran = session.beginTransaction();
String hql = "from account";//查询所有记录
var query = session.createQuery(hql);
var list = query.list();//返回多个记录
//var list = query.uniqueResult();//返回一条记录
System.out.println(list);
String hql1 = "from account where id=:id";//查找一条记录
var query1 = session.createQuery(hql1);
query1.setParameter("id", 1);
var acObj = (account)query1.uniqueResult();
System.out.println(acObj);
String hql2 = "from account where id=?0";//查找一条记录
var query2 = session.createQuery(hql2);
query2.setParameter(0, 1);
var acObj2 = (account)query2.uniqueResult();
System.out.println(acObj2);
//分页
String hql3 = "from account ";
var query3 = session.createQuery(hql3);
//limit ?,?
query3.setFirstResult(0);
query3.setMaxResults(3);
var list3 = query3.list();
System.out.println(list3);
tran.commit();
session.close();
}
}
Hibernate HQL查询
点赞
收藏