hibernate系列五:HQL查询(二)

一  动态条件查询

------------------------------------------------条件实体类--------------------------------------------

package com.obtk.entitys;

public class ConditionEntity {
	private String theName;
	private Integer minAge;
	private Integer maxAge;
	private String theGender;
	
	public ConditionEntity() {
	}
	public ConditionEntity(String theName, Integer minAge, Integer maxAge,
			String theGender) {
		super();
		this.theName = theName;
		this.minAge = minAge;
		this.maxAge = maxAge;
		this.theGender = theGender;
	}
	public String getTheName() {
		return theName;
	}
	public void setTheName(String theName) {
		this.theName = theName;
	}
	
	public Integer getMinAge() {
		return minAge;
	}
	public void setMinAge(Integer minAge) {
		this.minAge = minAge;
	}
	public Integer getMaxAge() {
		return maxAge;
	}
	public void setMaxAge(Integer maxAge) {
		this.maxAge = maxAge;
	}
	public void setTheGender(String theGender) {
		this.theGender = theGender;
	}
	public String getTheGender() {
		return theGender;
	}
	
	
}
=========================条件查询=================================

package com.obtk.test;

import java.util.List;

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

import com.obtk.entitys.ConditionEntity;
import com.obtk.entitys.StudentEntity;
import com.obtk.utils.HiberUtil;

public class ConditionQuery {
	public static void main(String[] args) {
		Session session=null;
		//绑定一个参数名的形式
		String hqlSql="from StudentEntity where 1=1";
		try {
			session=HiberUtil.getSession();
			
			ConditionEntity con=new ConditionEntity("张", 20, 30, "男");
			//动态的拼接sql
			if(con.getTheName()!=null){
				hqlSql=hqlSql+" and stuName like '%"+con.getTheName()+"%'";
			}
			if(con.getTheGender()!=null){
				hqlSql=hqlSql+" and gender ='"+con.getTheGender()+"'";
			}
			if(con.getMinAge()!=null){
				hqlSql=hqlSql+" and age >="+con.getMinAge();
			}
			if(con.getMaxAge()!=null){
				hqlSql=hqlSql+" and age <="+con.getMaxAge();
			}
			Query qy=session.createQuery(hqlSql);
			List<StudentEntity> stuList=qy.list();
			for(StudentEntity stu : stuList){
				System.out.println(stu.getStuId()+"\t"+stu.getStuName()+"\t"
						+stu.getGender()+"\t"+stu.getAge());
			}
		} catch (HibernateException e) {
			e.printStackTrace();
		}finally{
			HiberUtil.closeSession();
		}
	}
}


二   分页查询

package com.obtk.test2;

import java.util.List;

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

import com.obtk.entitys.StudentEntity;
import com.obtk.utils.HiberUtil;

public class QueryByPage {
	/**
	 * 分页查询
	 * @param pageNo  当前页码
	 * @param pageSize  每页显示多少条
	 */
	static void doQuery(Integer pageNo,Integer pageSize){
		String hqlStr="from StudentEntity order by age desc";
		Session session=null;
		try {
			session=HiberUtil.getSession();
			Query qy=session.createQuery(hqlStr);
			qy.setMaxResults(pageSize);  //设置每页显示多少条
			qy.setFirstResult((pageNo-1)*pageSize); //设置从那一页开始
			List<StudentEntity> stuList=qy.list();
			for(StudentEntity stu : stuList){
				System.out.println(stu.getStuId()+"\t"+stu.getStuName()+"\t"
						+stu.getGender()+"\t"+stu.getAge());
			}
		} catch (HibernateException e) {
			e.printStackTrace();
		}finally{
			HiberUtil.closeSession();
		}
	}
	public static void main(String[] args) {
		doQuery(2,3);
	}
}


三   唯一查询

package com.obtk.test2;

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

import com.obtk.utils.HiberUtil;

//唯一查询是指查一行
public class UniquTest {
	public static void main(String[] args) {
		String hqlStr="select count(*) from StudentEntity";
		Session session=null;
		try {
			session=HiberUtil.getSession();
			Query qy=session.createQuery(hqlStr);
			Integer result=Integer.parseInt(qy.uniqueResult()+"");
			System.out.println("学生人数:"+result);
		} catch (HibernateException e) {
			e.printStackTrace();
		}finally{
			HiberUtil.closeSession();
		}
	}
}

四  投影查询

     投影查询是指查部分列

package com.obtk.test2;

import java.util.List;

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

import com.obtk.utils.HiberUtil;

public class TouYinQuery1 {
	public static void main(String[] args) {
		//查一列
		String hqlStr="select stuName from StudentEntity where gender=?";
		Session session=null;
		try {
			session=HiberUtil.getSession();
			Query qy=session.createQuery(hqlStr);
			qy.setParameter(0, "女");
			//注意返回结果
			List<String> nameList=qy.list();
			for(String str : nameList){
				System.out.println(str);
			}
		} catch (HibernateException e) {
			e.printStackTrace();
		}finally{
			HiberUtil.closeSession();
		}
	}
}
============================

package com.obtk.test2;

import java.util.List;

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

import com.obtk.entitys.StudentEntity;
import com.obtk.utils.HiberUtil;
//查多列
public class TouYinQuery2 {
	public static void main(String[] args) {
		String hqlStr="select stuId,stuName from StudentEntity where gender=?";
		Session session=null;
		try {
			session=HiberUtil.getSession();
			Query qy=session.createQuery(hqlStr);
			qy.setParameter(0, "女");
			//注意返回值
			List<Object[]> stuList=qy.list();
			Object[] theRow=null;
			for(int i=0;i<stuList.size();i++){
				theRow=stuList.get(i);
				System.out.println(theRow[0]+"\t"+theRow[1]);
			}
		} catch (HibernateException e) {
			e.printStackTrace();
		}finally{
			HiberUtil.closeSession();
		}
	}
}


















  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

御前两把刀刀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值