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" ;

 

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