一 动态条件查询
------------------------------------------------条件实体类--------------------------------------------
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();
}
}
}