Hibernate的HQL查詢清單整理

使用HQL一直很慌張,沒有底氣

閒來抽了時間把常用的操作測試一遍,親測可用

1、無條件 查詢所有字段
String hql = "from Customer u " ;
2、添加where語句條件查詢
String hql = "from Customer u where u.customerSource=2" ;
3、多條件 and 查詢
String hql = "from Customer u where u.customerSource=1 and u.customerID=1" ;
4、查詢指定字段
String hql = "select u.customerID,u.customerSource from Customer u where u.customerSource=2" ;
5、查詢TOP
String hql = "select u.customerID,u.customerSource from Customer u " ;
this.getHibernateTemplate().setMaxResults(3);
6、分組彙總
String hql = "select u.customerSource,count(1) from Customer u group by u.customerSource " ;
7、傳參條件查詢
String hql = "from Customer u where u.customerSource=?" ;
List list = this.getHibernateTemplate().find(hql,new Integer(2).longValue());
8、bean中外鍵直接引用查詢
	private Long customerSource; 其實就是 BaseDict 中的 	private Long customerID; 字段
String hql = "select u.customerID,u.customerSource,b.dictTypeCode,b.dictItemName from Customer u, BaseDict b where u.customerSource=b.baseDictID" ;
9、修改bean中的外鍵關係
	private BaseDict customerSource; 改爲對象引用 <many-to-one name="customerSource" class="com.awf.crm.domain.BaseDict" column="customerSource"></many-to-one>
String hql = "select u.customerID,u.customerSource,b.dictTypeCode,b.dictItemName from Customer u, BaseDict b where u.customerSource=b.baseDictID" ;

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章