Hibernate查询

1.简单查询

//简单查询
		public void testQueryUser1() {
			Configuration cfg = null;
			SessionFactory sf = null;
			Session session = null;
			Transaction ts = null;
			try {
				sf=HibernateUtil.getSessionFactory();//sessionfactory单态模式
				session=sf.getCurrentSession();//one thread ,one session
				ts=session.beginTransaction();
				
				Query query = session.createQuery("from java.lang.Object");
				List<User> users = query.list();
				for(int i=0;i<users.size();i++)
				{
					User u = users.get(i); 
			    	System.out.println(u.getName() +" " + u.getAge());
				}

			    ts.commit();
			} catch (HibernateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				if(ts !=null) {
					ts.rollback();
				}
			}finally{
				
			}
}

 Hibernate查询

2.属性查询(单个&&多个)

//单个属性查询
		public void testQueryUserOneAttribute(){
			Configuration cfg =null;
			SessionFactory sf=null;
			Session session=null;
			Transaction ts=null;
			try {
				sf=HibernateUtil.getSessionFactory();//SessionFactory单态模式
				session=sf.getCurrentSession();//保证每个读写线程有唯一的session实例
				ts=session.beginTransaction();
				Query query=session.createQuery("select u.name from User u");
				List names=query.list();
				for(int i=0;i<names.size();i++)
				{
					String name =(String)names.get(i);
					System.out.println(name);
				}
				ts.commit();
			} catch (HibernateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				if(ts !=null) {
					ts.rollback();
				}
			}finally {
			}
		}

 Hibernate查询

 

//多个属性查询
		public void  testQueryUserSeveralAttributes() {
			Configuration cfg = null;
			SessionFactory sf = null;
			Session session = null;
			Transaction ts = null;
			try {
				sf=HibernateUtil.getSessionFactory();//sessionfactory单态模式
				session=sf.getCurrentSession();//one thread ,one session
				ts=session.beginTransaction();
				
				Query query=session.createQuery("select u.name,u.age from User u");
				List<Object[]> attributes=query.list();//加了类型
				for(int i=0;i<attributes.size();i++)
				{
					Object obj[] =attributes.get(i);
					System.out.println(obj[0]+"的年龄"+obj[1]);
				}

			    ts.commit();
			} catch (HibernateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				if(ts !=null) {
					ts.rollback();
				}
			}finally{
				
			}
}

 Hibernate查询

3.分页查询

//分页查询
				public void testQueryPage(){
					Configuration cfg =null;
					SessionFactory sf=null;
					Session session=null;
					Transaction ts=null;
					try {
						sf=HibernateUtil.getSessionFactory();
						session=sf.getCurrentSession();
						ts=session.beginTransaction();
						Query query=session.createQuery("from User u");
						query.setFirstResult(0);
						query.setMaxResults(5);
						List<User> users=query.getResultList();
						for(int i=0;i<users.size();i++)
						{
							User u =(User)users.get(i);
							System.out.println(u.toString());
						}
						ts.commit();
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						if(ts !=null) {
							ts.rollback();
						}
					}finally {
					}
				}

 Hibernate查询

 第三页,pageSize=10:

 

//14、分页查询
	public void testQueryByPage() {
		QueryPage(3);
	}
	public void QueryPage(int pageSize){
		Configuration cfg =null;
		SessionFactory sf=null;
		Session session=null;
		Transaction ts=null;
		try {
			sf=HibernateUtil.getSessionFactory();
			session=sf.getCurrentSession();
			ts=session.beginTransaction();
			Query query=session.createQuery("select count(*) from User u");
			Number cnts = (Number)query.uniqueResult();
			int userCount=cnts.intValue();
			int pageCount=0;
			pageCount=userCount/pageSize;
			if(userCount%pageSize!=0) {
				pageCount++;
			}
			Query query1=session.createQuery("from User u");
			for(int i=0;i<pageCount;i++)
			{
				System.out.println("第"+i+"页");
				System.out.println("---------------------------------------------------------------");
				query1.setFirstResult(i*pageSize);
				query1.setMaxResults(pageSize);
				List<User> us=query1.list();
				for(User u : us) {
					System.out.println(u.toString());
				}
			}
			ts.commit();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			if(ts !=null) {
				ts.rollback();
			}
		}finally {
		}
	}

 Hibernate查询

 

 

 

 

上一篇:springboot配置jpa提示Unable to resolve name [mysql] as strategy


下一篇:java 反射结合hibernate-validator 注解校验对象数据合法性