当前位置:首页 » 编程语言 » hibernate的sql条件查询
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

hibernate的sql条件查询

发布时间: 2022-08-05 10:19:38

Ⅰ 关于Hibernate的三种查询方式。

hql提供了更接近于传统sql语句的查询语法,容易过度吧,官方推荐使用,它提供强大的查询功能。一般使用这种查询。
Criteria查询,可移植性好,用起来也很方便,当你的查询条件很多时,可用Example创建一查询依据,查出属性与之类似的对象,代码简洁运用很方便。
Native SQL一般在遇到很复杂的查询条件时使用。
说白了,灵活运用就行,在不同情况下使用不同的解决方案,建议你多看些实例,见得多了用起来就比较容易判断该用哪一个。
如果问题解决,请点赞采纳,谢谢!!!

Ⅱ Hibernate的几种查询方式

HQL 通过Hibernate提供的查询语言进行查询。Hibernate Query lanague EJBQL(JPQL 1.0) 是EJB提供的查询语言 QBC(query by cretira)通过Cretira接口进行查询 QBE(query by Example) 通过Example编程接口进行查询 从功能强弱上排序:NativeSQL > HQL > EJBQL(JPQL 1.0) >QBC(query by cretira) >QBE(query by Example) 1: QBE (Query By Example) QBC查询方式 QBC(Query By Criteria)查询方式是 Hibernate 提供的“ 更加面向对象”的一种检索方式。 QBC 在条件查询上比 HQL 查询更为灵活,而且支持运行时动态天生查询语句。 在Hibernate 应用中使用 QBC 查询通常经过 3 个步骤 (1)使用 Session 实例的 createCriteria() 方法创建 Criteria 对象 (2)使用工具类 Restrictions 的相关方法为 Criteria 对象设置查询对象 (3)使用 Criteria 对象的 list() 方法执行查询,返回查询结果QBE查询QBE查询就是检索与指定样本对象具有相同属性值的对象。因此QBE 查询的关键就是样本对象的创建,样本对象中的所有非空属性均将作为查询条件。 QBE 查询的功能子集,固然 QBE 没有 QBC 功能大,但是有些场合 QBE 使用起来更为方便。 工具类Example 为 Criteria 对象指定样本对象作为查询条件Java代码1 Session session = HibernateSessionFactory.getSessionFactory().openSe ssion(); 2 Transaction ts = session.beginTransaction(); 3 Customer c = new Customer(); 4 c.setCname("Hibernate"); 5 Criteria criteria = session.createCriteria(Customer. class ); 6 Criteria.add(Example.create(c)); 7 Iterator it = criteria.list().iterator(); 8 ts.commit(); 9 HibernateSessionFactory.closeSession();

Ⅲ 如何用hibernate直接进行SQL语句查询

如何用hibernate直接进行SQL语句查询
public int getLogin(String username,String password)
{ String sql="select * from user_table where username=? and password=?";
SQLQuery query=getSession().createSQLQuery(sql).addEntity(UserTable.class);
query.setString(0,username);
query.setString(1,password);
return Integer.parseInt(query.uniqueResult().toString());

}
public static void main(String[] args) {
new UserTableDAO().getLogin("zhang","yuan");
}

Ⅳ 关于hibernate多条件查询

你这典型的将hibernate+标准sql混合使用

xsxk."+propertyNameO+"="+valueO+"andxsxk."+propertyNameT+"="+valueT

xsxk.属性名="+valueO+"

Ⅳ 在hibernate运用sql查询

给你一个我写的例子:
public List<Statistics> statistics(){
String sql="select "+
"sum(case when (t.type_code=3) then 1 else 0 end),"+
"sum(case when (t.type_code=5) then 1 else 0 end),"+
"sum(case when (t.type_code=8) then 1 else 0 end),"+
"sum(case when (t.type_code=0) then 1 else 0 end),"+
"sum(case when (t.type_code=6) then 1 else 0 end),"+
"sum(case when (t.type_code=9) then 1 else 0 end),"+
"count(*)"+
"from cs_cust_complaint_data t where t.from_flag='2'";
List<Statistics> stal = new ArrayList<Statistics>();
SQLQuery query = getSession().createSQLQuery(sql);
Object[] obj = (Object[])query.uniqueResult();
BigDecimal b0 = (BigDecimal)obj[0];
BigDecimal b1 = (BigDecimal)obj[1];
BigDecimal b2 = (BigDecimal)obj[2];
BigDecimal b3 = (BigDecimal)obj[3];
BigDecimal b4 = (BigDecimal)obj[4];
BigDecimal b5 = (BigDecimal)obj[5];
BigDecimal b6 = (BigDecimal)obj[6];
Double zs = b6.doubleValue();
DecimalFormat df=new DecimalFormat("#.00");
stal.add(new Statistics("卷烟营销", b0.longValue(),Double.parseDouble(df.format(b0.doubleValue()/zs*100))));
stal.add(new Statistics("专卖法规", b1.longValue(),Double.parseDouble(df.format(b1.doubleValue()/zs*100))));
stal.add(new Statistics("烟叶生产", b2.longValue(),Double.parseDouble(df.format(b2.doubleValue()/zs*100))));
stal.add(new Statistics("政风行风", b3.longValue(),Double.parseDouble(df.format(b3.doubleValue()/zs*100))));
stal.add(new Statistics("人事劳资", b4.longValue(),Double.parseDouble(df.format(b4.doubleValue()/zs*100))));
stal.add(new Statistics("其他咨询", b5.longValue(),Double.parseDouble(df.format(b5.doubleValue()/zs*100))));
return stal;
}

Ⅵ hibernate怎么多个条件查询

1. Hibernate多条件查询通用方法
//value[i]为第i个查询条件propertyName[i]的值 (本方法已通过测试) /*多条件查询,查询条件的值为空时自动除去该条件 * rigor为true时采用精确查询 */ public List searchByPropertys(String model,String[]propertyName,Object[] value,int page,boolean rigor){ StringBuffer sqlBuffer = new StringBuffer(); String ralation=" like "; if(rigor){ ralation=" = "; } sqlBuffer.append("from "+model+" as model\n"); int len=propertyName.length; List list=new ArrayList(); boolean first=true; for(int i=0;i< len;i++){ if(value[i]!=null){ if(first){ sqlBuffer.append(" where "+ "model."+ propertyName[i] + ralation+" ?\n"); list.add(value[i]); first=false; }else{ sqlBuffer.append(" and "+ "model."+ propertyName[i] +ralation+ " ?\n"); list.add(value[i]); } } } try { Session session=getSession(); Query queryObject = session.createQuery(sqlBuffer.toString()); for(int i=0;i< list.size();i++){ if(rigor){ queryObject.setParameter(i, list.get(i)); }else{ queryObject.setParameter(i, "%"+list.get(i)+"%"); } } list=queryObject.list(); closeSession(session); return list; } catch (RuntimeException re) { log.error("find by property name failed", re); throw re; } }

2:hibernate多条件组合查询 之 sql 拼接
这个方法与上面第一节中的相同,只不过上面的方法是将搜索的多个条件在外部(即调用方)封装在了数组中。
public static void main(String[] args) { Session session = null; Transaction tx = null; List list = null; Criteria criteria = null; try { session = HibernateSessionFactory.getSession(); tx = session.beginTransaction(); DetachedCriteria detachedCriteria = DetachedCriteria .forClass(InfoTab.class); String sql=" 1=1 "; Integer pareaId = 0; // 父地区; Integer careaId = 0; // 子地区; Integer categoryId = 0; // 类别; String infoPrivider = "中介"; // 来源; String houseType= "地下室"; // 房屋类型; Integer hxBedRoom=0; // 室; Integer hxLivingRoom=0; // 厅; String hzHouseStatus="有房出租"; // 合租类型; String hzRequestSex="男"; // 性别要求; String fixUp="尚未"; // 装修程度; Integer lcHeightMolecuse=0; // 楼层; String orientation="东南"; // 朝向要求; Integer buildArea=2000; // 建筑面积; Integer useArea=80; // 使用面积; Integer rentalDigit=2000; // 租金/价格; String title= "出租"; // 标题; if(pareaId!=0) { sql+="pareaId=" + pareaId; } if(careaId!=0) { sql+=" and careaId=" + careaId; } if(categoryId!=0) { sql+=" and categoryId=" + categoryId; } if(!infoPrivider.equals("")) { sql+=" and infoPrivider='" + infoPrivider + "'"; } if(!houseType.equals("")) { sql+=" and houseType='" + houseType +"'"; } if(hxBedRoom!=0) { sql+=" and hxBedRoom=" + hxBedRoom; } if(hxLivingRoom!=0) { sql+=" and hxLivingRoom=" + hxLivingRoom; } if(!hzHouseStatus.equals("")) { sql+=" and hzHouseStatus='" + hzHouseStatus + "'"; } if(!hzRequestSex.equals("")) { sql+=" and hzRequestSex='" + hzRequestSex +"'"; } if(!fixUp.equals("")) { sql+=" and fixUp='" + fixUp + "'"; } if(lcHeightMolecuse!=0) { sql+=" and lcHeightMolecuse=" + lcHeightMolecuse; } if(!orientation.equals("")) { sql+=" and orientation='" + orientation + "'"; } if(buildArea!=0) { sql+=" and buildArea=" + buildArea; } if(useArea!=0) { sql+=" and useArea=" + useArea; } if(rentalDigit!=0) { sql+=" and rentalDigit=" + rentalDigit; } if(!title.equals("")) { sql+=" and title like '%" + title + "%'"; } sql+=" order by id desc"; System.out.println(sql); detachedCriteria.add(Restrictions.sqlRestriction(sql)); criteria = detachedCriteria.getExecutableCriteria(session); list = criteria.list(); for(int i=0;i< list.size();i++) { InfoTab infoTab = (InfoTab)list.get(i); System.out.println(infoTab.getTitle() +" "+ infoTab.getCategoryId() +" "+ infoTab.getPareaName() +" "+ infoTab.getCareaName() +" " + infoTab.getHouseType() +" " + infoTab.getInfoPrivider()); } tx.commit(); } catch (HibernateException he) { he.printStackTrace(); } }

Ⅶ hibernate sql 条件查询问题。

可以如下进行查询:
Criteria criteria = session.createCriteria(User.class);
List users = criteria.list();
for(Iterator it = users.iterator(); it.hasNext(); ) {
User user = (User) it.next();
System.out.println(user.getId() +
" /t " + user.getName() +
"/" + user.getAge());
}

Ⅷ hibernate使用sql查询

看了下你的 SQL ,里面打的是中文的问号,这个应该是英文的咧

Ⅸ hibernate怎么用查询

1:hibernate数据查询方式:有HQL方式,QBC方式,原生SQL方式。HQL适合静态查询,QBC则适合较多的动态查询。
A:HQL方式,支持条件查询,连接查询,分页查询,分组查询,内置函数和自定义函数查询(SUN(),MIN(),MAX()),子查询,动态绑定参数查。
HQL语句定义如下:
String hql="from book";
Query query=session.createQuery(hql);
B:QBC方式,也就是QBC检索方式。QBC通过Session类创建Criteria实例,通过不同方法进行检索,实际上Criteria是用来装载查询条件的容器。QBC有很多条件函数,如:Resstictions.eq(),Resstictions.gt(),Resstictions.ge(),
Resstictions.le(),Resstictions.and(),Resstictions.or()等。
Criteria容器使用方法如下:
Criteria criteria=session.createCriteria(book.class);
criteria.add(Restrications.It("id",new Integer(4)));
List list=criteria.list();
C:原生SQL方式。不管是HQL还是QBC最终都要通过Hibernate来解析,把他们转换成SQL语句进行对数据库的操作。因为我们知道SQL可以在多平台之间使用。
使用原生SQL方式如下:
String sql="select {b.*} from book b"
SQLQuery squery=session.createSQLQuery(sql);
squery.addEntity("b",book.class);
List list=squery.list();
2:hibernate的关联查询
A:一对一关联:
B:一对多,多对一关联
C:多对多关联
最后,要学hibernate,平时要多动手,慢慢积累经验,成就感就会也大,这样才能学而不厌。祝你学习进步。

Ⅹ Hibernate SQL查询

如果你的hbm.xml配置文件配置恰当,可以用HQL:
Query query=....."from MessagePO mpo where mpo.clent=?";
ClentPO cpo=session.get(ClentPO.class,主键值);
query.setParameter(1,cpo);
query.list();