Hibernate查询数据的几种方式以及HQL用法及参数的几种使用方法

Hibernate查询数据的几种方式

/**
* 第一种查询:利用query接口与HQL语句
* 1.如果查询所有字段,则list中的元素为POJO对象
* 2.如果查询若干字段,则list中的元素为对象数组
* 3.如果想返回若干字段的对象,则需要通过 new TUser(username,email)方式
*/

Java代码
  1. public void query(){  
  2.     Session session = null;  
  3.     Query query = null;  
  4.     String hql = null;  
  5.     try {  
  6.         session = HibernateSessionFactory.getSession();  
  7.         hql="From TUser user where user.suser='admin'";  
  8.         query = session.createQuery(hql);  
  9.         List list = query.list();  
  10.         for (Iterator iter = list.iterator(); iter.hasNext();) {  
  11.             TUser user = (TUser) iter.next();  
  12.             System.out.println("----Query---");  
  13.             System.out.println("-Suser-="+user.getSuser());  
  14.             System.out.println("-Name--="+user.getName());  
  15.             System.out.println("-Email-="+user.getEmail());  
  16.         }  
  17.     } catch (Exception e) {  
  18.         e.printStackTrace();  
  19.     }finally{  
  20.         HibernateSessionFactory.closeSession();  
  21.     }  
  22. }  
public void query(){ 	Session session = null; 	Query query = null; 	String hql = null; 	try { 		session = HibernateSessionFactory.getSession(); 		hql="From TUser user where user.suser='admin'"; 		query = session.createQuery(hql); 		List list = query.list(); 		for (Iterator iter = list.iterator(); iter.hasNext();) { 			TUser user = (TUser) iter.next(); 			System.out.println("----Query---"); 			System.out.println("-Suser-="+user.getSuser()); 			System.out.println("-Name--="+user.getName()); 			System.out.println("-Email-="+user.getEmail()); 		} 	} catch (Exception e) { 		e.printStackTrace(); 	}finally{ 		HibernateSessionFactory.closeSession(); 	} } 


/**
* 第二种查询:利用criteria接口
*/

Java代码
  1. public void criteria(){  
  2.     Session session = null;  
  3.     Criteria criteria = null;  
  4.     try {  
  5.         session = HibernateSessionFactory.getSession();  
  6.         criteria = session.createCriteria(TUser.class);  
  7.         criteria.add(Restrictions.isNotNull("suser"));  
  8.         criteria.add(Restrictions.ilike("name", "%T%"));  
  9.         criteria.addOrder(Order.desc("nation"));  
  10.         List list = criteria.list();  
  11.         for (Iterator iter = list.iterator(); iter.hasNext();) {  
  12.             TUser user = (TUser) iter.next();  
  13.             System.out.println("----Criteria---");  
  14.             System.out.println("-Suser-="+user.getSuser());  
  15.             System.out.println("-Name--="+user.getName());  
  16.             System.out.println("-Email-="+user.getEmail());  
  17.             System.out.println("-Nation="+user.getNation());  
  18.         }  
  19.     } catch (Exception e) {  
  20.         e.printStackTrace();  
  21.     }finally{  
  22.         HibernateSessionFactory.closeSession();  
  23.     }  
  24. }  
public void criteria(){ 	Session session = null; 	Criteria criteria = null; 	try { 		session = HibernateSessionFactory.getSession(); 		criteria = session.createCriteria(TUser.class); 		criteria.add(Restrictions.isNotNull("suser")); 		criteria.add(Restrictions.ilike("name", "%T%")); 		criteria.addOrder(Order.desc("nation")); 		List list = criteria.list(); 		for (Iterator iter = list.iterator(); iter.hasNext();) { 			TUser user = (TUser) iter.next(); 			System.out.println("----Criteria---"); 			System.out.println("-Suser-="+user.getSuser()); 			System.out.println("-Name--="+user.getName()); 			System.out.println("-Email-="+user.getEmail()); 			System.out.println("-Nation="+user.getNation()); 		} 	} catch (Exception e) { 		e.printStackTrace(); 	}finally{ 		HibernateSessionFactory.closeSession(); 	} } 


/**
* 第三种查询:利用SQLQuery接口,即原生SQL
* 1.如果查询所有字段,要加入标量设置query.addEntity(TUser.class);
* 2.如果查询若干字段,则集合中数据为对象数组
*/

Java代码
  1. public void nativeSql() {  
  2.     Session session =null;  
  3.     Query query = null;  
  4.     String sql = null;  
  5.     try {  
  6.         session = HibernateSessionFactory.getSession();  
  7.         sql = "select suser,name,email,nation from t_user";  
  8.         query = session.createQuery(sql);  
  9.         List list = query.list();  
  10.         for (Iterator iter = list.iterator(); iter.hasNext();) {  
  11.             Object[] obj = (Object[]) iter.next();  
  12.             System.out.println("-Suser-="+obj[0]);  
  13.             System.out.println("-Name--="+obj[1]);  
  14.             System.out.println("-Email-="+obj[2]);  
  15.             System.out.println("-Nation="+obj[3]);  
  16.         }  
  17.     } catch (Exception e) {  
  18.         e.printStackTrace();  
  19.     }finally{  
  20.         HibernateSessionFactory.closeSession();  
  21.     }  
  22. }  
public void nativeSql() { 	Session session =null; 	Query query = null; 	String sql = null; 	try { 		session = HibernateSessionFactory.getSession(); 		sql = "select suser,name,email,nation from t_user"; 		query = session.createQuery(sql); 		List list = query.list(); 		for (Iterator iter = list.iterator(); iter.hasNext();) { 			Object[] obj = (Object[]) iter.next(); 			System.out.println("-Suser-="+obj[0]); 			System.out.println("-Name--="+obj[1]); 			System.out.println("-Email-="+obj[2]); 			System.out.println("-Nation="+obj[3]); 		} 	} catch (Exception e) { 		e.printStackTrace(); 	}finally{ 		HibernateSessionFactory.closeSession(); 	} }


/**
* 第四种查询:查询单个对象,注意get和load方法的区别
*/

Java代码
  1. public void getOrLoad(){  
  2.     Session session = null;  
  3.     try {  
  4.         session = HibernateSessionFactory.getSession();  
  5.         TUser user = (TUser)session.load(TUser.class, "test1");  
  6.         System.out.println("-Suser-="+user.getSuser());  
  7.         System.out.println("-Name--="+user.getName());  
  8.         System.out.println("-Email-="+user.getEmail());  
  9.         System.out.println("-Nation="+user.getNation());  
  10.     } catch (Exception e) {  
  11.         e.printStackTrace();  
  12.     }finally{  
  13.         HibernateSessionFactory.closeSession();  
  14.     }  
  15. }  
public void getOrLoad(){ 	Session session = null; 	try { 		session = HibernateSessionFactory.getSession(); 		TUser user = (TUser)session.load(TUser.class, "test1"); 		System.out.println("-Suser-="+user.getSuser()); 		System.out.println("-Name--="+user.getName()); 		System.out.println("-Email-="+user.getEmail()); 		System.out.println("-Nation="+user.getNation()); 	} catch (Exception e) { 		e.printStackTrace(); 	}finally{ 		HibernateSessionFactory.closeSession(); 	} }

HQL用法及参数的几种使用方法

/**
*按参数位置绑定
*/

Java代码
  1. public void batchUpdate() {  
  2.     Session session = null;  
  3.     Transaction transaction = null;  
  4.     Query query = null;  
  5.     String hql ="update TUser set name='阿德' where suser=:user";  
  6.     try {  
  7.         session = HibernateSessionFactory.getSession();  
  8.         transaction =session.beginTransaction();  
  9.         query = session.createQuery(hql);  
  10.         query.setString("user", "test9");  
  11.         int i = query.executeUpdate();  
  12.         transaction.commit();  
  13.         System.out.println("--update--"+i);  
  14.     } catch (Exception e) {  
  15.         e.printStackTrace();  
  16.         transaction.rollback();  
  17.     }finally{  
  18.         HibernateSessionFactory.closeSession();  
  19.     }  
  20. }  
public void batchUpdate() { 	Session session = null; 	Transaction transaction = null; 	Query query = null; 	String hql ="update TUser set name='阿德' where suser=:user"; 	try { 		session = HibernateSessionFactory.getSession(); 		transaction =session.beginTransaction(); 		query = session.createQuery(hql); 		query.setString("user", "test9"); 		int i = query.executeUpdate(); 		transaction.commit(); 		System.out.println("--update--"+i); 	} catch (Exception e) { 		e.printStackTrace(); 		transaction.rollback(); 	}finally{ 		HibernateSessionFactory.closeSession(); 	} } 


/**
*按参数位置绑定
*/

Java代码
  1. public void batchDelete() {  
  2.     Session session = null;  
  3.     Transaction transaction = null;  
  4.     Query query = null;  
  5.     String hql = "delete from t_user suser=:user";  
  6.     try {  
  7.         session = HibernateSessionFactory.getSession();  
  8.         transaction = session.beginTransaction();  
  9.         query = session.createQuery(hql);  
  10.         query.setString("user", "test11");  
  11.         int i = query.executeUpdate();  
  12.         transaction.commit();  
  13.         System.out.println("--delete--"+i);  
  14.     } catch (Exception e) {  
  15.         e.printStackTrace();  
  16.         transaction.rollback();  
  17.     }finally{  
  18.         HibernateSessionFactory.closeSession();  
  19.     }  
  20. }  
public void batchDelete() { 	Session session = null; 	Transaction transaction = null; 	Query query = null; 	String hql = "delete from t_user suser=:user"; 	try { 		session = HibernateSessionFactory.getSession(); 		transaction = session.beginTransaction(); 		query = session.createQuery(hql); 		query.setString("user", "test11"); 		int i = query.executeUpdate(); 		transaction.commit(); 		System.out.println("--delete--"+i); 	} catch (Exception e) { 		e.printStackTrace(); 		transaction.rollback(); 	}finally{ 		HibernateSessionFactory.closeSession(); 	} } 


/*
*使用setProperties方法
*/

Java代码
  1. public void queryUsers() {  
  2.     Session session = null;  
  3.     Transaction transaction = null;  
  4.     Query query = null;  
  5.     String hql =null;  
  6.     try {  
  7.         session = HibernateSessionFactory.getSession();  
  8.         hql = "from TUser where suser=:user";  
  9.         transaction = session.beginTransaction();  
  10.         query = session.createQuery(hql);  
  11.         TUser user = new TUser();  
  12.         user.setSuser("test1");  
  13.         query.setProperties(user);  
  14.         List list = query.list();  
  15.         for (Iterator iter = list.iterator(); iter.hasNext();) {  
  16.             Object[] obj = (Object[]) iter.next();  
  17.             System.out.println("-Suser-="+obj[0]);  
  18.             System.out.println("-Name--="+obj[1]);  
  19.             System.out.println("-Email-="+obj[2]);  
  20.             System.out.println("-Nation="+obj[3]);  
  21.         }  
  22.     } catch (Exception e) {  
  23.         e.printStackTrace();  
  24.         transaction.rollback();  
  25.     }finally{  
  26.         HibernateSessionFactory.closeSession();  
  27.     }  

posted @ 2010-11-15 14:10  Jet J.J.Cheng  阅读(986)  评论(0编辑  收藏  举报