Hibernate查询4种
@Test
public void testNativSQL(){
System.out.println("=======查询所有========原生sql==========");
//查询所有 查询出来的list是数组组成的集合对象,所以先循环集合,拿到的是数组,然后用[]拿到数组中的元素
String sql="select * from user";
Session session=getSession();
SQLQuery sqlQuery=session.createSQLQuery(sql);
List<Object[]> list=sqlQuery.list();
for (Object[] obj : list) {
System.out.println(obj[0]+":"+obj[3]);
}
System.out.println("=======原生sql==========查询指定字段=================");
//查询指定字段 查询出来的list是由realName组成的集合对象,用list.get(0)拿到集合中的第一个
String sql1="select realName from user";
Session session1=getSession();
SQLQuery sqlQuery1=session1.createSQLQuery(sql1);
List<Object[]> list1=sqlQuery1.list();
System.out.println(list1.get(0));
System.out.println("=======查询所有========原生sql==========用addEntity将数组转化为对象=================");
String sql2="select * from user";
Session session2=getSession();
SQLQuery sqlQuery2=session2.createSQLQuery(sql2).addEntity(UserInfo.class);
List<UserInfo> list2=sqlQuery2.list();
System.out.println(list2);
System.out.println(list2.get(0).getRealName());
System.out.println("=======带条件查询所有========原生sql==========用addEntity将数组转化为对象=================");
String sql3="select * from user where userId>:uid";
Session session3=getSession();
SQLQuery sqlQuery3=session3.createSQLQuery(sql3).addEntity(UserInfo.class);
sqlQuery3.setInteger("uid", 4);
List<UserInfo> list3=sqlQuery3.list();
System.out.println(list3.toString());
session.close();
session1.close();
session2.close();
session3.close();
}
@Test
public void testHQL(){
Session session=getSession();
System.out.println("=======HQL查询所有记录====================");
//list出来时一个对象组成的数组
String hql="from UserInfo";
Query query=session.createQuery(hql);
List<UserInfo> list=query.list();
System.out.println(list);
System.out.println("=======HQL查询所有记录指定字段=输出数组===================");
//查询出来时两个属性组成的数组的集合
String hql1="select username,password from UserInfo u";
Query query1=session.createQuery(hql1);
List<UserInfo> list1=query1.list();
System.out.println(list1);
System.out.println("=======HQL查询所有记录指定字段=输出对象===================");
//查询出来时两个属性组成的数组的集合 必须提供构造函数,,,带参数
String hql2="select new UserInfo(username,password) from UserInfo u";
Query query2=session.createQuery(hql2);
List<UserInfo> list2=query2.list();
System.out.println(list2.toString());
System.out.println("=======HQL查询指定记录指定字段=输出对象===================");
String hql3="from UserInfo u where u.role.roleId=:uid";
Query query3=session.createQuery(hql3);
query3.setInteger("uid", 3);
List<UserInfo> list3=query3.list();
System.out.println(list3);
session.close();
}
@Test
public void testCriteria(){
Session session=getSession();
Criteria ct =session.createCriteria(UserInfo.class);
System.out.println(ct.list());
ct =session.createCriteria(UserInfo.class);
//ct.add(Restrictions.eq("username", "张三"));
ct.addOrder(Order.desc("userId"));
System.out.println(ct.list());
session.close();
}
@Test
public void testPagin(){
Session session=getSession();
Criteria ct =session.createCriteria(UserInfo.class);
/*ct.add(Restrictions.between("userId", 3, 5));*/
ct.add(Restrictions.gt("userId", 2));
ct.setMaxResults(2);
ct.setFirstResult((3-1)*2);
List<UserInfo> list=ct.list();
System.out.println(list);
session.close();
}
@Test
public void testNamedQuery(){
Session session=getSession();
//对象
Query query=session.getNamedQuery("alla");
System.out.println(query.list());
//数组
Query query1=session.getNamedQuery("allb");
System.out.println(query1.list());
}