Hibernate 查询例子

import java.text.SimpleDateFormat;

importjava.util.Iterator;

import java.util.List;

import org.hibernate.Query;import org.hibernate.Session;

import junit.framework.TestCase;

/**

*条件查询

*@authorAdministrator

*

*/

public class SimpleConditionQueryTest extends TestCase {

 public void testQuery1() {

Sessionsession=null;

try{

session=HibernateUtils.getSession();

session.beginTransaction();

//可以拼字符串

Liststudents=session.createQuery("selects.id,s.namefromStudentswheres.namelike'%1%'").list();

for(Iteratoriter=students.iterator();iter.hasNext();){

Object[]obj=(Object[])iter.next();

System.out.println(obj[0]+","+obj[1]);

}

session.getTransaction().commit();

}catch(Exceptione){

e.printStackTrace();

session.getTransaction().rollback();

}finally{

HibernateUtils.closeSession(session);

}

}

publicvoidtestQuery2(){

Sessionsession=null;

try{

session=HibernateUtils.getSession();

session.beginTransaction();

//Queryquery=session.createQuery("selects.id,s.namefromStudentswheres.namelike?");

//query.setParameter(0,"%1%");

//Liststudents=query.list();

//可以使用?方式传递参数

//参数的索引从0开始

//传递的参数值,不用单引号引起来

//注意方法链编程

Liststudents=session.createQuery("selects.id,s.namefromStudentswheres.namelike?")

.setParameter(0,"%1%")

.list();

for(Iteratoriter=students.iterator();iter.hasNext();){

Object[]obj=(Object[])iter.next();

System.out.println(obj[0]+","+obj[1]);

}

session.getTransaction().commit();

}catch(Exceptione){

e.printStackTrace();

session.getTransaction().rollback();

}finally{

HibernateUtils.closeSession(session);

}

 } 

 public void testQuery3() {

Sessionsession=null;

try{

session=HibernateUtils.getSession();

session.beginTransaction();

//使用:参数名称的方式传递参数值

Liststudents=session.createQuery("selects.id,s.namefromStudentswheres.namelike:myname")

.setParameter("myname","%1%")

.list();

for(Iteratoriter=students.iterator();iter.hasNext();){

Object[]obj=(Object[])iter.next();

System.out.println(obj[0]+","+obj[1]);

}

session.getTransaction().commit();

}catch(Exceptione){

e.printStackTrace();

session.getTransaction().rollback();

}finally{

HibernateUtils.closeSession(session);

}

}

publicvoidtestQuery4(){

Sessionsession=null;

try{

session=HibernateUtils.getSession();

session.beginTransaction();

//使用:参数名称的方式传递参数值

Liststudents=session.createQuery("selects.id,s.namefromStudentswheres.namelike:mynameands.id=:myid")

.setParameter("myname","%1%")

.setParameter("myid",12)

.list();

for(Iteratoriter=students.iterator();iter.hasNext();){

Object[]obj=(Object[])iter.next();

System.out.println(obj[0]+","+obj[1]);

}

session.getTransaction().commit();

}catch(Exceptione){

e.printStackTrace();

session.getTransaction().rollback();

}finally{

HibernateUtils.closeSession(session);

}

}

publicvoidtestQuery5(){

Sessionsession=null;

try{

session=HibernateUtils.getSession();

session.beginTransaction();

//支持in,需要使用setParameterList进行参数传递

Liststudents=session.createQuery("selects.id,s.namefromStudentswheres.idin(:myids)")

.setParameterList("myids",newObject[]{1,2,3,4,5})

.list();

for(Iteratoriter=students.iterator();iter.hasNext();){

Object[]obj=(Object[])iter.next();

System.out.println(obj[0]+","+obj[1]);

}

session.getTransaction().commit();

}catch(Exceptione){

e.printStackTrace();

session.getTransaction().rollback();

}finally{

HibernateUtils.closeSession(session);

}

 }   

 public void testQuery6() {

Sessionsession=null;

try{

session=HibernateUtils.getSession();

session.beginTransaction();

//查询2008年2月创建的学生

Liststudents=session.createQuery("selects.id,s.namefromStudentswheredate_format(s.createTime,'%Y-%m')=?")

.setParameter(0,"2008-02")

.list();

for(Iteratoriter=students.iterator();iter.hasNext();){

Object[]obj=(Object[])iter.next();

System.out.println(obj[0]+","+obj[1]);

}

session.getTransaction().commit();

}catch(Exceptione){

e.printStackTrace();

session.getTransaction().rollback();

}finally{

HibernateUtils.closeSession(session);

}

 }   

 public void testQuery7() {

Sessionsession=null;

try{

session=HibernateUtils.getSession();

session.beginTransaction();

SimpleDateFormatsdf=newSimpleDateFormat("yyyy-MM-ddHH:mm:ss");

//查询2008-01-10到2008-02-15创建的学生

Liststudents=session.createQuery("selects.id,s.namefromStudentswheres.createTimebetween?and?")

.setParameter(0,sdf.parse("2008-01-1000:00:00"))

.setParameter(1,sdf.parse("2008-02-1523:59:59"))

.list();

for(Iteratoriter=students.iterator();iter.hasNext();){

Object[]obj=(Object[])iter.next();

System.out.println(obj[0]+","+obj[1]);

}

session.getTransaction().commit();

}catch(Exceptione){

e.printStackTrace();

session.getTransaction().rollback();

}finally{

HibernateUtils.closeSession(session);

}

}

}

相关推荐