您的位置:首页 > 其它

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

cjunj 2010-11-15 14:10 120 查看
Hibernate查询数据的几种方式

/**
* 第一种查询:利用query接口与HQL语句
* 1.如果查询所有字段,则list中的元素为POJO对象
* 2.如果查询若干字段,则list中的元素为对象数组
* 3.如果想返回若干字段的对象,则需要通过 new TUser(username,email)方式
*/
Java代码 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();
}
}
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代码 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();
}
}
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代码 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();
}
}
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代码 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();
}
}
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代码 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();
}
}
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代码 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();
}
}
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代码 public void queryUsers() {
Session session = null;
Transaction transaction = null;
Query query = null;
String hql =null;
try {
session = HibernateSessionFactory.getSession();
hql = "from TUser where suser=:user";
transaction = session.beginTransaction();
query = session.createQuery(hql);
TUser user = new TUser();
user.setSuser("test1");
query.setProperties(user);
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();
transaction.rollback();
}finally{
HibernateSessionFactory.closeSession();
}
}
标签: 
相关文章推荐